Anonymous user
Execute Brain****/Common Lisp: Difference between revisions
The obvious: use local macros to create shorthand references to the slots of the state structure.
m (moved RCBF/Common Lisp to Execute Brain****/Common Lisp) |
(The obvious: use local macros to create shorthand references to the slots of the state structure.) |
||
Line 8:
(memory (make-array 1 :initial-element 0 :adjustable t))
(memory-pointer 0))
(defmacro with-bf-slots ((program-sym program-counter-sym
"Macro to replace cumbersome structure slot references with
local lexical macros resembling local variables."
`(symbol-macrolet ((,program-sym (bf-state-program ,obj-expr))
,@body))
(defun adjust-memory (state)
Line 15 ⟶ 26:
contents are shifted forward and the memory-pointer is incremented,
by an amount to make the memory ."
(
(cond ((>=
(adjust-array mem (
(let ((extent
(let ((
(
▲ (setf (bf-state-memory state)
▲ (setf (subseq (bf-state-memory state) extent)
(defun matching-bracket-for (program bracket-index)
(loop with depth := 0
Line 40 ⟶ 47:
until (zerop depth)
finally (return index)))
(defun brainfuck-eval (state &optional (stream *standard-output*))
(let ((
(with-bf-slots (program pc mem ptr) state
(places nil))▼
(loop while (<
(case (elt program
(#\+ (incf (
(#\- (decf (
(#\> (incf
(#\< (decf
(#\[ (if (/= 0 (
(push (1-
(setf pc (
(#\
▲ (bf-state-memory-pointer state)))
▲ stream)))
▲ (incf (bf-state-program-counter state)))))
(defun bf (program) (brainfuck-eval (make-bf-state :program program)))
(defun bf-repl ()
(loop do (fresh-line)
|