Skip to content

Instantly share code, notes, and snippets.

@ochronus
Created April 2, 2016 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochronus/8b3ffa32eeb6c3d65c330b1322ccd294 to your computer and use it in GitHub Desktop.
Save ochronus/8b3ffa32eeb6c3d65c330b1322ccd294 to your computer and use it in GitHub Desktop.
(defn bf-interpreter [program-code]
(loop [cells [0N], current-cell 0, instruction-pointer 0]
(condp = (get program-code instruction-pointer)
\+ (recur (update-in cells [current-cell] inc) current-cell (inc instruction-pointer))
\- (recur (update-in cells [current-cell] dec) current-cell (inc instruction-pointer))
\. (do
(print (char (nth cells current-cell)))
(recur cells current-cell (inc instruction-pointer)))
\, (let [ch (.read System/in)]
(recur (assoc cells current-cell ch) current-cell (inc instruction-pointer)))
(recur cells current-cell (inc instruction-pointer)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment