;;;; -*- Mode: Lisp; Package: CL-USER -*- ;;;; TINY0.lisp ;;;; ;;;; The TINY-0 interpreter ;;;; - A 4-function calculator ;;;; ;;;; RDB - 9/03 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; The Interpreter ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; The only literal expressions in TINY0 are numbers (defun literalp (expr) (numberp expr)) ;;; An application is just a list (defun applicationp (expr) (consp expr)) ;;; Apply a TINY function to arguments (defun tapply (fn args) (case fn (+ (apply #'+ args)) (- (apply #'- args)) (* (apply #'* args)) (/ (apply #'/ args)) (t (error "Undefined TINY function: ~S" fn)))) ;;; The TINY evaluator (defun teval (expr) (cond ((literalp expr) expr) ((applicationp expr) (tapply (first expr) (mapcar #'teval (rest expr)))) (t (error "Invalid TINY expression: ~S" expr)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; The Top-Level Loop ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun tiny () (loop (format t "~&>>> ") (print (teval (read)))))