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-var-binding (symbol)
17 (cons symbol (format nil "v~d" (incf counter)))))
20 (defun make-func-binding (symbol)
21 (cons symbol (format nil "f~d" (incf counter)))))
24 ;;; Concatenate a list of strings, with a separator
25 (defun join (list separator)
34 (join (cdr list) separator)))))
38 (defvar *compilations* nil)
40 (defun ls-compile-block (sexps env fenv)
41 (concat (join (mapcar (lambda (x)
42 (concat (ls-compile x env fenv) ";"))
47 (defun extend-env (args env)
48 (append (mapcar #'make-var-binding args) env))
50 (defparameter *env* '())
51 (defparameter *fenv* '())
53 (defun ls-lookup (symbol env)
54 (let ((binding (assoc symbol env)))
55 (and binding (format nil "~a" (cdr binding)))))
57 (defun lookup-variable (symbol env)
58 (or (ls-lookup symbol env)
59 (ls-lookup symbol *env*)
60 (error "Undefined variable `~a'" symbol)))
62 (defun lookup-function (symbol env)
63 (or (ls-lookup symbol env)
64 (ls-lookup symbol *fenv*)
65 (error "Undefined function `~a'" symbol)))
67 (defmacro define-compilation (name args &body body)
68 ;; Creates a new primitive `name' with parameters args and
69 ;; @body. The body can access to the local environment through the
71 `(push (list ',name (lambda (env fenv ,@args) ,@body))
74 (define-compilation if (condition true false)
75 (format nil "((~a)? (~a) : (~a))"
76 (ls-compile condition env fenv)
77 (ls-compile true env fenv)
78 (ls-compile false env fenv)))
80 ;;; Return the required args of a lambda list
81 (defun lambda-list-required-argument (lambda-list)
82 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
84 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
86 (defun lambda-list-rest-argument (lambda-list)
87 (second (member '&rest lambda-list)))
89 (define-compilation lambda (lambda-list &rest body)
90 (let ((required-arguments (lambda-list-required-argument lambda-list))
91 (rest-argument (lambda-list-rest-argument lambda-list)))
92 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
94 (join (mapcar (lambda (x) (lookup-variable x new-env))
100 (concat "var " (lookup-variable rest-argument new-env)
101 " = arguments.slice("
102 (prin1-to-string (length required-arguments)) ");
106 (concat (ls-compile-block (butlast body) new-env fenv)
107 "return " (ls-compile (car (last body)) new-env fenv) ";")
111 (define-compilation fsetq (var val)
112 (format nil "~a = ~a" (lookup-function var fenv) (ls-compile val env fenv)))
114 (define-compilation setq (var val)
115 (format nil "~a = ~a" (lookup-variable var env) (ls-compile val env fenv)))
120 (defvar *literals* '())
122 (defun literal->js (sexp)
124 ((null sexp) "undefined")
125 ((integerp sexp) (format nil "~a" sexp))
126 ((stringp sexp) (format nil "\"~a\"" sexp))
127 ((symbolp sexp) (format nil "{name: \"~a\"}" (symbol-name sexp)))
128 ((consp sexp) (concat "{car: "
129 (literal->js (car sexp))
131 (literal->js (cdr sexp)) "}"))))
134 (defun literal (form)
135 (let ((var (format nil "l~d" (incf counter))))
136 (push (cons var (literal->js form)) *literals*)
139 (define-compilation quote (sexp)
142 (define-compilation debug (form)
143 (format nil "console.log(~a)" (ls-compile form env fenv)))
145 (define-compilation while (pred &rest body)
146 (format nil "(function(){while(~a){~a}})() "
147 (ls-compile pred env fenv)
148 (ls-compile-block body env fenv)))
150 (define-compilation function (x)
152 ((and (listp x) (eq (car x) 'lambda))
153 (ls-compile x env fenv))
155 (lookup-function x fenv))))
157 (defmacro eval-when-compile (&body body)
158 `(eval-when (:compile-toplevel :execute)
161 (defvar *eval-when-compilations*)
162 (define-compilation eval-when-compile (&rest body)
163 (setq *eval-when-compilations* "")
164 (eval (cons 'progn body))
165 (if (string= *eval-when-compilations* "")
167 *eval-when-compilations*))
170 (defmacro define-transformation (name args form)
171 `(define-compilation ,name ,args
172 (ls-compile ,form env fenv)))
174 (define-transformation progn (&rest body)
175 `((lambda () ,@body)))
177 (define-transformation let (bindings &rest body)
178 `((lambda ,(mapcar 'car bindings) ,@body)
179 ,@(mapcar 'cadr bindings)))
181 ;;; aritmetic primitives
182 (define-compilation + (x y)
183 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
185 (define-compilation - (x y)
186 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
188 (define-compilation * (x y)
189 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
191 (define-compilation / (x y)
192 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
194 (define-compilation = (x y)
195 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
197 (define-compilation cons (x y)
198 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
200 (define-compilation car (x)
201 (concat "(" (ls-compile x env fenv) ").car"))
203 (define-compilation cdr (x)
204 (concat "(" (ls-compile x env fenv) ").cdr"))
206 (define-compilation symbol-name (x)
207 (concat "(" (ls-compile x env fenv) ").name"))
209 (define-compilation eq (x y)
210 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
214 (defmacro with-eval-when-compilation (&body body)
215 `(setq *eval-when-compilations*
216 (concat *eval-when-compilations* (progn ,@body))))
218 (defun %compile-defvar (name)
219 (push (make-var-binding name) *env*)
220 (with-eval-when-compilation
221 (format nil "var ~a" (lookup-variable name *env*))))
223 (defun %compile-defun (name)
224 (push (make-func-binding name) *fenv*)
225 (with-eval-when-compilation
226 (format nil "var ~a" (lookup-variable name *fenv*))))
228 (defun %compile-defmacro (name lambda)
229 (push (cons name (cons 'macro lambda)) *fenv*))
231 (defun ls-macroexpand-1 (form &optional env fenv)
232 (let ((function (cdr (assoc (car form) *fenv*))))
233 (if (and (listp function) (eq (car function) 'macro))
234 (apply (eval (cdr function)) (cdr form))
237 (defun compile-funcall (function args env fenv)
240 (format nil "~a(~{~a~^, ~})"
241 (lookup-function function fenv)
242 (mapcar (lambda (x) (ls-compile x env fenv)) args)))
243 ((and (listp function) (eq (car function) 'lambda))
244 (format nil "(~a)(~{~a~^, ~})"
245 (ls-compile function env fenv)
246 (mapcar (lambda (x) (ls-compile x env fenv)) args)))
248 (error "Invalid function designator ~a." function))))
250 (defun ls-compile (sexp &optional env fenv)
252 ((symbolp sexp) (lookup-variable sexp env))
253 ((integerp sexp) (format nil "~a" sexp))
254 ((stringp sexp) (format nil "\"~a\"" sexp))
256 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
257 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
259 (apply compiler-func env fenv (cdr sexp))
260 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
262 (defun ls-compile-toplevel (sexp)
263 (setq *literals* nil)
264 (let ((code (ls-compile sexp)))
266 (concat (join (mapcar (lambda (lit)
267 (concat "var " (car lit) " = " (cdr lit) ";
272 (setq *literals* nil))))
275 (defun ls-compile-file (filename output)
276 (with-open-file (in filename)
277 (with-open-file (out output :direction :output :if-exists :supersede)
279 for x = (read in nil) while x
280 for compilation = (ls-compile-toplevel x)
281 when compilation do (write-line (concat compilation "; ") out)))))
286 (defun compile-test ()
287 (ls-compile-file "test.lisp" "test.js"))