Created
May 7, 2020 10:29
-
-
Save qookei/1d5bcea5608245a52b2e514b16025151 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn do_bf_step [code stdin state] | |
(let [c (first code) | |
mem (get state :mem) | |
ind (get state :ind)] | |
(if (zero? (count code)) | |
state | |
(cond | |
(= c \+) (do_bf_step (rest code) stdin {:mem (assoc mem ind (mod (+ (mem ind) 1) 256)) :ind ind}) | |
(= c \-) (do_bf_step (rest code) stdin {:mem (assoc mem ind (mod (- (mem ind) 1) 256)) :ind ind}) | |
(= c \.) (do (print (char (mem ind))) | |
(do_bf_step (rest code) stdin state)) | |
(= c \,) (do_bf_step (rest code) (rest stdin) {:mem (assoc mem ind (int (first stdin))) :ind ind}) | |
(= c \>) (do_bf_step (rest code) stdin {:mem mem :ind (+ ind 1)}) | |
(= c \<) (do_bf_step (rest code) stdin {:mem mem :ind (- ind 1)}) | |
(= c \[) (loop [s state] | |
(if (zero? ((get s :mem) (get s :ind))) | |
(loop [c (rest code) | |
i 0] | |
(if (and (= (first c) \]) (zero? i)) | |
(do_bf_step (rest c) stdin s) | |
(recur (rest c) | |
(cond | |
(= (first c) \]) (- i 1) | |
(= (first c) \[) (+ i 1) | |
:else i | |
)))) | |
(recur | |
(do_bf_step | |
(loop [c (rest code) | |
nc '() | |
i 0] | |
(if (and (= (first c) \]) (zero? i)) | |
(reverse nc) | |
(recur (rest c) | |
(conj nc (first c)) | |
(cond | |
(= (first c) \]) (- i 1) | |
(= (first c) \[) (+ i 1) | |
:else i | |
)))) | |
stdin | |
s | |
)))) | |
:else (do_bf_step (rest code) stdin state) | |
)))) | |
(do_bf_step (seq "+[-[<<[+[--->]-[<<<]]]>>>-]>-.---.>..>.<<<<-.<+.>>>>>.>.<<.<-.") '() {:mem (vec (repeat 30000 0)) :ind 0}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment