;;; reval.lsp ;;; Demonstrates Lisp in Lisp, but ;;; as a slight twist, uses parenthesized POSTFIX ;;; instead of parenthesized prefix. ;;; ;;; E.g., (3 5 +) in REVLISP means (+ 3 5) in Lisp ;;; ;;; S. Tanimoto, Oct. 17, 2001 ;;; ;;; The "environment" represents the accessible bindings of symbols. ;;; It's represented as an association list. (setq environment '((a . 1)(b . 2)(c . 3))) (defun reval (form) (cond ((member form '(t nil)) form) ((numberp form) form) ((symbolp form) (let ((b (assoc form environment))) (if (null b) (error (format nil "UNBOUND VARIABLE IN REVLISP: ~S" form)) (cdr b) ) ) ) ((consp form) (let ((op (car (last form))) (args (butlast form))) (cond ((eq op 'quote) (first args)) ((eq op 'if) (if (reval (first args)) (reval (second args)) (if (cddr args) (reval (third args))) ) ) ((progn (setq args (mapcar #'reval args)) nil)) ((null (fboundp op)) (error (format nil "UNKNOWN REVLISP SPECIAL FORM: ~S." op) )) (t (apply op args)) ) ) ) (t (error (format nil "UNKNOWN REVLISP FORM: ~S." form))) ) ) (defun error (msg) (print msg) )