3 ms·
This is basically the Lisp program. See below. Does not seem to be that verbose: (defun run (&optional (stream *standard-input*)) (let ((pc 0)
by lispm 8y ago
This is basically the Lisp program. See below. Does not seem to be that verbose:
(defun run (&optional (stream *standard-input*))
(let ((pc 0)
(mem (make-array 32 :element-type 'bit :initial-element 0))
(halted nil))
(labels ((execute (code &optional a b)
(macrolet ((mref (idx) `(sbit mem ,idx)))
(ecase code
(and (setf (mref a) (logand (mref a) (mref b))))
(or (setf (mref a) (logior (mref a) (mref b))))
(xor (setf (mref a) (logxor (mref a) (mref b))))
(not (setf (mref a) (if (zerop (mref a)) 1 0)))
(mov (setf (mref a) (mref b)))
(set (setf (mref a) b))
(random (setf (mref a) (random 2)))
(jmp (setf pc (1- a)))
(jz (when (zerop (mref b)) (setf pc (1- a))))
(halt (setf halted t)))))
(interpret (codes)
(loop for nil = (apply #'execute (aref codes pc)) and cycles from 1
do (incf pc)
until (or halted (>= cycles 100000))
finally (if halted
(format t "Program halts! ~a instructions executed.~%" cycles)
(format t "Unable to determine if program halts.~%"))))
(read-program ()
(coerce (loop for pc below (parse-integer (read-line stream)) collect
(read-from-string (concatenate 'string "(" (read-line stream) ")")))
'vector)))
(interpret (read-program)))))
Adapted from: https://www.reddit.com/r/dailyprogrammer/comments/1euacb/052213_challenge_125_intermediate_halt_its/ca4ypfn/ https://www.reddit.com/r/dailyprogrammer/comments/1euacb/052...
The rest in the example is an actual compiler.