Execute Brain****/Common Lisp: Difference between revisions

→‎{{header|Common Lisp}}: explain how adjust-memory works, use length in favor of array-dimension, various other polish (UNTESTED)
m (<lang>)
(→‎{{header|Common Lisp}}: explain how adjust-memory works, use length in favor of array-dimension, various other polish (UNTESTED))
Line 10:
 
(defun adjust-memory (state)
"Modifies memory and memory-pointer such that memory-pointer is a valid index to the memory array.
If it is too large, the array is extended; if it is negative, the array is extended, its contents are shifted forward and the memory-pointer is incremented, by an amount to make the memory ."
(cond ((>= (bf-state-memory-pointer state)
(array-dimensionlength (bf-state-memory state) 0))
(adjust-array (bf-state-memory state)
(1+ (bf-state-memory-pointer state))
:initial-element 0))
((<minusp (bf-state-memory-pointer state) 0)
(let ((extent (- (bf-state-memory-pointer state))))
(incf (bf-state-memory-pointer state) extent)
Line 25 ⟶ 27:
 
(defun matching-bracket-for (program bracket-index)
(letloop with ((depth := 0))
(loop for index := bracket-index then (1+ index)
when (>= index (length program))
do (error 'fail"unmatched bracket")
when (char= #\[ (elt program index))
do (incf depth)
when (char= #\] (elt program index))
do (decf depth)
until (=zerop depth 0)
finally (return index))))
 
(defun brainfuck-eval (state &optional (stream *standard-output*))
Line 60 ⟶ 62:
(loop do (fresh-line)
(princ "BRAINFUCK> ")
(brainfuck-eval (make-bf-state :program (read-line)))))
</lang>