News for hackers. Stuff that matters.
Re:Logic Programming and Compiler Writing (1980) [pdf] by lispm on Friday August 03, 2018 @06:20AM (#17678629)
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/052...

The rest in the example is an actual compiler.

There are no comments on this comment yet.