3 ;;; simplify me, please
4 (defun concat (&rest strs)
5 (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
11 (defun make-binding (symbol)
12 (cons symbol (format nil "V_~d" (incf counter)))))
16 (defvar *compilations* nil)
18 (defun extend-env (args env)
19 (append (mapcar #'make-binding args) env))
21 (defun ls-lookup (symbol env)
22 (let ((binding (assoc symbol env)))
24 (format nil "~a" (cdr binding))
25 (error "Undefined variable `~a'" symbol))))
27 (defmacro define-compilation (name args &body body)
28 "creates a new primitive `name' with parameters args and @body. The
29 body can access to the local environment through the variable env"
30 `(push (list ',name (lambda (env ,@args) ,@body))
33 (define-compilation if (condition true false)
34 (format nil "((~a)? (~a) : (~a))"
35 (ls-compile condition env)
37 (ls-compile false env)))
39 (define-compilation lambda (args &rest body)
40 (let ((new-env (extend-env args env)))
42 (format nil "~{~a~^, ~}" (mapcar
43 (lambda (x) (ls-lookup x new-env))
46 (ls-compile-block body new-env)
50 (define-compilation setq (var val)
51 (format nil "~a = ~a" (ls-lookup var env) (ls-compile val env)))
53 (define-compilation quote (sexp)
55 ((integerp sexp) (format nil "~a" sexp))
56 ((stringp sexp) (format nil "\"~a\"" sexp))
57 ((listp sexp) (format nil "[~{~a~^, ~}]" sexp))))
59 (defparameter *env* '())
60 (defparameter *env-fun* '())
62 (defun ls-compile (sexp &optional env)
64 ((symbolp sexp) (ls-lookup sexp env))
65 ((integerp sexp) (format nil "~a" sexp))
66 ((stringp sexp) (format nil " \"~a\" " sexp))
69 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
71 (apply compiler-func env (cdr sexp))
75 (defun ls-compile-block (sexps env)
77 "~{~#[~; return ~a;~:;~a;~%~]~}"