3 (defmacro while (condition &body body)
8 ;;; simplify me, please
9 (defun concat (&rest strs)
10 (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
14 ;;; Concatenate a list of strings, with a separator
15 (defun join (list separator)
24 (join (cdr list) separator)))))
28 ;;; It is a basic Lisp reader. It does not use advanced stuff
29 ;;; intentionally, because we want to use it to bootstrap a simple
30 ;;; Lisp. The main entry point is the function `ls-read', which
31 ;;; accepts a strings as argument and return the Lisp expression.
32 (defun make-string-stream (string)
35 (defun %peek-char (stream)
37 (peek-char nil stream nil)
38 (and (< (cdr stream) (length (car stream)))
39 (char (car stream) (cdr stream)))))
41 (defun %read-char (stream)
43 (read-char stream nil)
44 (and (< (cdr stream) (length (car stream)))
45 (prog1 (char (car stream) (cdr stream))
46 (incf (cdr stream))))))
48 (defun whitespacep (ch)
49 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
51 (defun skip-whitespaces (stream)
52 (loop for ch = (%peek-char stream)
53 while (and ch (whitespacep ch))
54 do (%read-char stream)))
57 (or (null ch) (whitespacep ch) (char= #\) ch)))
59 (defun read-until (stream func)
61 (loop for ch = (%peek-char stream)
62 until (funcall func ch)
63 do (setq string (concat string (string ch)))
64 do (%read-char stream))
67 (defun skip-whitespaces-and-comments (stream)
69 (skip-whitespaces stream)
70 (setq ch (%peek-char stream))
71 (while (and ch (eql ch #\;))
72 (read-until stream (lambda (x) (eql x #\newline)))
73 (skip-whitespaces stream)
74 (setq ch (%peek-char stream)))))
76 (defun %read-list (stream)
77 (skip-whitespaces-and-comments stream)
78 (let ((ch (%peek-char stream)))
85 (skip-whitespaces-and-comments stream)
86 (prog1 (ls-read stream)
87 (unless (char= (%read-char stream) #\))
88 (error "')' was expected."))))
90 (cons (ls-read stream) (%read-list stream))))))
92 (defvar *eof* (make-symbol "EOF"))
93 (defun ls-read (stream)
94 (skip-whitespaces-and-comments stream)
95 (let ((ch (%peek-char stream)))
104 (list 'quote (ls-read stream)))
107 (list 'backquote (ls-read stream)))
110 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
111 (%read-char stream)))
114 (if (eql (%peek-char stream) #\@)
115 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
116 (list 'unquote (ls-read stream))))
119 (ecase (%read-char stream)
121 (list 'function (ls-read stream)))))
123 (let ((string (read-until stream #'terminalp)))
124 (if (every #'digit-char-p string)
125 (parse-integer string)
126 (intern (string-upcase string))))))))
128 (defun ls-read-from-string (string)
129 (ls-read (make-string-stream string)))
135 (defun make-var-binding (symbol)
136 (cons symbol (format nil "v~d" (incf counter)))))
139 (defun make-func-binding (symbol)
140 (cons symbol (format nil "f~d" (incf counter)))))
142 (defvar *compilations* nil)
144 (defun ls-compile-block (sexps env fenv)
145 (concat (join (mapcar (lambda (x)
146 (concat (ls-compile x env fenv) ";"))
151 (defun extend-env (args env)
152 (append (mapcar #'make-var-binding args) env))
154 (defparameter *env* '())
155 (defparameter *fenv* '())
157 (defun ls-lookup (symbol env)
158 (let ((binding (assoc symbol env)))
159 (and binding (format nil "~a" (cdr binding)))))
161 (defun lookup-variable (symbol env)
162 (or (ls-lookup symbol env)
163 (ls-lookup symbol *env*)
164 (error "Undefined variable `~a'" symbol)))
166 (defun lookup-function (symbol env)
167 (or (ls-lookup symbol env)
168 (ls-lookup symbol *fenv*)
169 (error "Undefined function `~a'" symbol)))
171 (defmacro define-compilation (name args &body body)
172 ;; Creates a new primitive `name' with parameters args and
173 ;; @body. The body can access to the local environment through the
175 `(push (list ',name (lambda (env fenv ,@args) ,@body))
178 (define-compilation if (condition true false)
179 (format nil "((~a)? (~a) : (~a))"
180 (ls-compile condition env fenv)
181 (ls-compile true env fenv)
182 (ls-compile false env fenv)))
184 ;;; Return the required args of a lambda list
185 (defun lambda-list-required-argument (lambda-list)
186 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
188 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
190 (defun lambda-list-rest-argument (lambda-list)
191 (second (member '&rest lambda-list)))
193 (define-compilation lambda (lambda-list &rest body)
194 (let ((required-arguments (lambda-list-required-argument lambda-list))
195 (rest-argument (lambda-list-rest-argument lambda-list)))
196 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
197 (concat "(function ("
198 (join (mapcar (lambda (x) (lookup-variable x new-env))
204 (concat "var " (lookup-variable rest-argument new-env)
205 " = arguments.slice("
206 (prin1-to-string (length required-arguments)) ");
210 (concat (ls-compile-block (butlast body) new-env fenv)
211 "return " (ls-compile (car (last body)) new-env fenv) ";")
215 (define-compilation fsetq (var val)
216 (format nil "~a = ~a" (lookup-function var fenv) (ls-compile val env fenv)))
218 (define-compilation setq (var val)
219 (format nil "~a = ~a" (lookup-variable var env) (ls-compile val env fenv)))
224 (defvar *literals* '())
226 (defun literal->js (sexp)
228 ((null sexp) "undefined")
229 ((integerp sexp) (format nil "~a" sexp))
230 ((stringp sexp) (format nil "\"~a\"" sexp))
231 ((symbolp sexp) (format nil "{name: \"~a\"}" (symbol-name sexp)))
232 ((consp sexp) (concat "{car: "
233 (literal->js (car sexp))
235 (literal->js (cdr sexp)) "}"))))
238 (defun literal (form)
239 (let ((var (format nil "l~d" (incf counter))))
240 (push (cons var (literal->js form)) *literals*)
243 (define-compilation quote (sexp)
246 (define-compilation debug (form)
247 (format nil "console.log(~a)" (ls-compile form env fenv)))
249 (define-compilation while (pred &rest body)
250 (format nil "(function(){while(~a){~a}})() "
251 (ls-compile pred env fenv)
252 (ls-compile-block body env fenv)))
254 (define-compilation function (x)
256 ((and (listp x) (eq (car x) 'lambda))
257 (ls-compile x env fenv))
259 (lookup-function x fenv))))
261 (defmacro eval-when-compile (&body body)
262 `(eval-when (:compile-toplevel :execute)
265 (defvar *eval-when-compilations*)
266 (define-compilation eval-when-compile (&rest body)
267 (setq *eval-when-compilations* "")
268 (eval (cons 'progn body))
269 (if (string= *eval-when-compilations* "")
271 *eval-when-compilations*))
273 (defmacro define-transformation (name args form)
274 `(define-compilation ,name ,args
275 (ls-compile ,form env fenv)))
277 (define-transformation progn (&rest body)
278 `((lambda () ,@body)))
280 (define-transformation let (bindings &rest body)
281 `((lambda ,(mapcar 'car bindings) ,@body)
282 ,@(mapcar 'cadr bindings)))
284 ;;; A little backquote implementation without optimizations of any
285 ;;; kind for lispstrack.
286 (defun backquote-expand-1 (form)
292 ((eq (car form) 'unquote)
294 ((eq (car form) 'backquote)
295 (backquote-expand-1 (backquote-expand-1 (cadr form))))
300 ((and (listp s) (eq (car s) 'unquote))
301 (list 'list (cadr s)))
302 ((and (listp s) (eq (car s) 'unquote-splicing))
305 (list 'list (backquote-expand-1 s)))))
308 (defun backquote-expand (form)
309 (if (and (listp form) (eq (car form) 'backquote))
310 (backquote-expand-1 (cadr form))
313 ;;; Some Tests comparing backquote agains the backquote of the host
314 ;;; Lisp. It is not very exhaustive, but we just want a working
315 ;;; backquotation to be able bootstrap this program.
316 (macrolet ((test (form1 form2)
317 `(assert (equal ,form1 ,form2))))
318 (test (backquote (1 2 3 4))
320 (test (backquote (1 2 (+ 3 4)))
322 (test (backquote (1 2 (unquote (+ 3 4))))
324 (test (backquote (1 2 (unquote-splicing '(3 4))))
326 (test (backquote (backquote x))
330 (backquote (quote (unquote x)))))
333 (eval (backquote (backquote ((unquote (unquote x)))))))))
335 (define-transformation backquote (form)
336 (backquote-expand-1 form))
340 (define-compilation + (x y)
341 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
343 (define-compilation - (x y)
344 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
346 (define-compilation * (x y)
347 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
349 (define-compilation / (x y)
350 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
352 (define-compilation = (x y)
353 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
355 (define-compilation cons (x y)
356 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
358 (define-compilation car (x)
359 (concat "(" (ls-compile x env fenv) ").car"))
361 (define-compilation cdr (x)
362 (concat "(" (ls-compile x env fenv) ").cdr"))
364 (define-compilation symbol-name (x)
365 (concat "(" (ls-compile x env fenv) ").name"))
367 (define-compilation eq (x y)
368 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
372 (defmacro with-eval-when-compilation (&body body)
373 `(setq *eval-when-compilations*
374 (concat *eval-when-compilations* (progn ,@body))))
376 (defun %compile-defvar (name)
377 (push (make-var-binding name) *env*)
378 (with-eval-when-compilation
379 (format nil "var ~a" (lookup-variable name *env*))))
381 (defun %compile-defun (name)
382 (push (make-func-binding name) *fenv*)
383 (with-eval-when-compilation
384 (format nil "var ~a" (lookup-variable name *fenv*))))
386 (defun %compile-defmacro (name lambda)
387 (push (cons name (cons 'macro lambda)) *fenv*))
389 (defun ls-macroexpand-1 (form &optional env fenv)
390 (let ((function (cdr (assoc (car form) *fenv*))))
391 (if (and (listp function) (eq (car function) 'macro))
392 (apply (eval (cdr function)) (cdr form))
395 (defun compile-funcall (function args env fenv)
398 (format nil "~a(~{~a~^, ~})"
399 (lookup-function function fenv)
400 (mapcar (lambda (x) (ls-compile x env fenv)) args)))
401 ((and (listp function) (eq (car function) 'lambda))
402 (format nil "(~a)(~{~a~^, ~})"
403 (ls-compile function env fenv)
404 (mapcar (lambda (x) (ls-compile x env fenv)) args)))
406 (error "Invalid function designator ~a." function))))
408 (defun ls-compile (sexp &optional env fenv)
410 ((symbolp sexp) (lookup-variable sexp env))
411 ((integerp sexp) (format nil "~a" sexp))
412 ((stringp sexp) (format nil "\"~a\"" sexp))
414 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
415 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
417 (apply compiler-func env fenv (cdr sexp))
418 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
420 (defun ls-compile-toplevel (sexp)
421 (setq *literals* nil)
422 (let ((code (ls-compile sexp)))
424 (concat (join (mapcar (lambda (lit)
425 (concat "var " (car lit) " = " (cdr lit) ";
430 (setq *literals* nil))))
432 (defun ls-compile-file (filename output)
433 (with-open-file (in filename)
434 (with-open-file (out output :direction :output :if-exists :supersede)
438 for compilation = (ls-compile-toplevel x)
439 when compilation do (write-line (concat compilation "; ") out)))))
443 (defun compile-test ()
444 (ls-compile-file "test.lisp" "test.js"))