3 (defmacro while (condition &body body)
8 ;;; simplify me, please
9 (defun concat (&rest strs)
10 (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
16 (defun make-binding (symbol)
17 (cons symbol (format nil "V_~d" (incf counter)))))
21 (defvar *compilations* nil)
23 (defun extend-env (args env)
24 (append (mapcar #'make-binding args) env))
26 (defun ls-lookup (symbol env)
27 (let ((binding (assoc symbol env)))
29 (format nil "~a" (cdr binding))
30 (error "Undefined variable `~a'" symbol))))
32 (defmacro define-compilation (name args &body body)
33 "creates a new primitive `name' with parameters args and @body. The
34 body can access to the local environment through the variable env"
35 `(push (list ',name (lambda (env ,@args) ,@body))
38 (define-compilation if (condition true false)
39 (format nil "((~a)? (~a) : (~a))"
40 (ls-compile condition env)
42 (ls-compile false env)))
44 (define-compilation lambda (args &rest body)
45 (let ((new-env (extend-env args env)))
47 (format nil "~{~a~^, ~}" (mapcar
48 (lambda (x) (ls-lookup x new-env))
51 (ls-compile-block body new-env)
55 (define-compilation setq (var val)
56 (format nil "~a = ~a" (ls-lookup var env) (ls-compile val env)))
58 (define-compilation quote (sexp)
60 ((integerp sexp) (format nil "~a" sexp))
61 ((stringp sexp) (format nil "\"~a\"" sexp))
62 ((listp sexp) (format nil "[~{~a~^, ~}]" sexp))))
64 (defparameter *env* '())
65 (defparameter *env-fun* '())
67 (defun ls-compile (sexp &optional env)
69 ((symbolp sexp) (ls-lookup sexp env))
70 ((integerp sexp) (format nil "~a" sexp))
71 ((stringp sexp) (format nil " \"~a\" " sexp))
74 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
76 (apply compiler-func env (cdr sexp))
80 (defun ls-compile-block (sexps env)
82 "~{~#[~; return ~a;~:;~a;~%~]~}"