6 (defun !reduce (func list initial)
11 (funcall func initial (car list)))))
17 (defmacro while (condition &body body)
22 (defun concat-two (s1 s2)
23 (concatenate 'string s1 s2))
25 (defun setcar (cons new)
26 (setf (car cons) new))
27 (defun setcdr (cons new)
28 (setf (cdr cons) new)))
30 (defvar *newline* (string (code-char 10)))
32 (defun concat (&rest strs)
33 (!reduce (lambda (s1 s2) (concat-two s1 s2))
37 ;;; Concatenate a list of strings, with a separator
38 (defun join (list separator)
47 (join (cdr list) separator)))))
49 (defun join-trailing (list separator)
54 (join-trailing (cdr list) separator))
56 (concat (car list) separator (join-trailing (cdr list) separator)))))
58 (defun integer-to-string (x)
63 (push (mod x 10) digits)
64 (setq x (truncate x 10)))
65 (join (mapcar (lambda (d) (string (char "0123456789" d)))
71 ;;; It is a basic Lisp reader. It does not use advanced stuff
72 ;;; intentionally, because we want to use it to bootstrap a simple
73 ;;; Lisp. The main entry point is the function `ls-read', which
74 ;;; accepts a strings as argument and return the Lisp expression.
75 (defun make-string-stream (string)
78 (defun %peek-char (stream)
79 (and (< (cdr stream) (length (car stream)))
80 (char (car stream) (cdr stream))))
82 (defun %read-char (stream)
83 (and (< (cdr stream) (length (car stream)))
84 (prog1 (char (car stream) (cdr stream))
85 (setcdr stream (1+ (cdr stream))))))
87 (defun whitespacep (ch)
88 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
90 (defun skip-whitespaces (stream)
92 (setq ch (%peek-char stream))
93 (while (and ch (whitespacep ch))
95 (setq ch (%peek-char stream)))))
98 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
100 (defun read-until (stream func)
103 (setq ch (%peek-char stream))
104 (while (not (funcall func ch))
105 (setq string (concat string (string ch)))
107 (setq ch (%peek-char stream)))
110 (defun skip-whitespaces-and-comments (stream)
112 (skip-whitespaces stream)
113 (setq ch (%peek-char stream))
114 (while (and ch (char= ch #\;))
115 (read-until stream (lambda (x) (char= x #\newline)))
116 (skip-whitespaces stream)
117 (setq ch (%peek-char stream)))))
119 (defun %read-list (stream)
120 (skip-whitespaces-and-comments stream)
121 (let ((ch (%peek-char stream)))
128 (skip-whitespaces-and-comments stream)
129 (prog1 (ls-read stream)
130 (unless (char= (%read-char stream) #\))
131 (error "')' was expected."))))
133 (cons (ls-read stream) (%read-list stream))))))
135 (defvar *eof* (make-symbol "EOF"))
136 (defun ls-read (stream)
137 (skip-whitespaces-and-comments stream)
138 (let ((ch (%peek-char stream)))
147 (list 'quote (ls-read stream)))
150 (list 'backquote (ls-read stream)))
153 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
154 (%read-char stream)))
157 (if (eql (%peek-char stream) #\@)
158 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
159 (list 'unquote (ls-read stream))))
162 (ecase (%read-char stream)
164 (list 'function (ls-read stream)))
167 (concat (string (%read-char stream))
168 (read-until stream #'terminalp))))
170 ((string= cname "space") (char-code #\space))
171 ((string= cname "newline") (char-code #\newline))
172 (t (char-code (char cname 0))))))
174 (let ((feature (read-until stream #'terminalp)))
176 ((string= feature "common-lisp")
177 (ls-read stream) ;ignore
179 ((string= feature "lispstrack")
182 (error "Unknown reader form.")))))))
184 (let ((string (read-until stream #'terminalp)))
185 (if (every #'digit-char-p string)
186 (parse-integer string)
187 (intern (string-upcase string))))))))
189 (defun ls-read-from-string (string)
190 (ls-read (make-string-stream string)))
195 (defvar *compilation-unit-checks* '())
197 (defparameter *env* '())
198 (defparameter *fenv* '())
200 (defun make-binding (name type js declared)
201 (list name type js declared))
203 (defun binding-name (b) (first b))
204 (defun binding-type (b) (second b))
205 (defun binding-translation (b) (third b))
206 (defun binding-declared (b)
208 (defun mark-binding-as-declared (b)
209 (setcar (cdddr b) t))
212 (defun gvarname (symbol)
213 (concat "v" (integer-to-string (incf counter))))
215 (defun lookup-variable (symbol env)
216 (or (assoc symbol env)
218 (let ((name (symbol-name symbol))
219 (binding (make-binding symbol 'variable (gvarname symbol) nil)))
222 (unless (binding-declared (assoc symbol *env*))
223 (error (concat "Undefined variable `" name "'"))))
224 *compilation-unit-checks*)
227 (defun lookup-variable-translation (symbol env)
228 (binding-translation (lookup-variable symbol env)))
230 (defun extend-local-env (args env)
231 (append (mapcar (lambda (symbol)
232 (make-binding symbol 'variable (gvarname symbol) t))
237 (defun lookup-function (symbol env)
238 (or (assoc symbol env)
239 (assoc symbol *fenv*)
240 (let ((name (symbol-name symbol))
244 (concat "f" (integer-to-string (incf counter)))
246 (push binding *fenv*)
248 (unless (binding-declared (assoc symbol *fenv*))
249 (error (concat "Undefined function `" name "'"))))
250 *compilation-unit-checks*)
253 (defun lookup-function-translation (symbol env)
254 (binding-translation (lookup-function symbol env))))
257 (defvar *toplevel-compilations*)
259 (defun %compile-defvar (name)
260 (let ((b (lookup-variable name *env*)))
261 (mark-binding-as-declared b)
262 (push (concat "var " (binding-translation b)) *toplevel-compilations*)))
264 (defun %compile-defun (name)
265 (let ((b (lookup-function name *env*)))
266 (mark-binding-as-declared b)
267 (push (concat "var " (binding-translation b)) *toplevel-compilations*)))
269 (defun %compile-defmacro (name lambda)
270 (push (make-binding name 'macro lambda t) *fenv*))
273 (defvar *compilations* nil)
275 (defun ls-compile-block (sexps env fenv)
277 (remove nil (mapcar (lambda (x)
278 (ls-compile x env fenv))
282 (defmacro define-compilation (name args &body body)
283 ;; Creates a new primitive `name' with parameters args and
284 ;; @body. The body can access to the local environment through the
286 `(push (list ',name (lambda (env fenv ,@args) ,@body))
289 (define-compilation if (condition true false)
291 (ls-compile condition env fenv)
293 (ls-compile true env fenv)
295 (ls-compile false env fenv)
298 ;;; Return the required args of a lambda list
299 (defun lambda-list-required-argument (lambda-list)
300 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
302 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
304 (defun lambda-list-rest-argument (lambda-list)
305 (second (member '&rest lambda-list)))
307 (define-compilation lambda (lambda-list &rest body)
308 (let ((required-arguments (lambda-list-required-argument lambda-list))
309 (rest-argument (lambda-list-rest-argument lambda-list)))
310 (let ((new-env (extend-local-env
311 (append (if rest-argument (list rest-argument))
314 (concat "(function ("
315 (join (mapcar (lambda (x)
316 (lookup-variable-translation x new-env))
322 (let ((js!rest (lookup-variable-translation rest-argument new-env)))
323 (concat "var " js!rest ";" *newline*
324 "for (var i = arguments.length-1; i>="
325 (integer-to-string (length required-arguments))
328 "{car: arguments[i], cdr: " js!rest "};"
331 (concat (ls-compile-block (butlast body) new-env fenv)
332 "return " (ls-compile (car (last body)) new-env fenv) ";")
336 (define-compilation fsetq (var val)
337 (concat (lookup-function-translation var fenv)
339 (ls-compile val env fenv)))
341 (define-compilation setq (var val)
342 (concat (lookup-variable-translation var env)
344 (ls-compile val env fenv)))
348 (defun literal->js (sexp)
350 ((null sexp) "false")
351 ((integerp sexp) (integer-to-string sexp))
352 ((stringp sexp) (concat "\"" sexp "\""))
353 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
354 ((consp sexp) (concat "{car: "
355 (literal->js (car sexp))
357 (literal->js (cdr sexp)) "}"))))
360 (defun literal (form)
363 (let ((var (concat "l" (integer-to-string (incf counter)))))
364 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
367 (define-compilation quote (sexp)
370 (define-compilation debug (form)
371 (concat "console.log(" (ls-compile form env fenv) ")"))
373 (define-compilation while (pred &rest body)
374 (concat "(function(){ while("
375 (ls-compile pred env fenv)
377 (ls-compile-block body env fenv)
380 (define-compilation function (x)
382 ((and (listp x) (eq (car x) 'lambda))
383 (ls-compile x env fenv))
385 (lookup-function-translation x fenv))))
388 (defmacro eval-when-compile (&body body)
389 `(eval-when (:compile-toplevel :load-toplevel :execute)
392 (define-compilation eval-when-compile (&rest body)
393 (eval (cons 'progn body))
396 (defmacro define-transformation (name args form)
397 `(define-compilation ,name ,args
398 (ls-compile ,form env fenv)))
400 (define-transformation progn (&rest body)
401 `((lambda () ,@body)))
403 (define-transformation let (bindings &rest body)
404 (let ((bindings (mapcar #'ensure-list bindings)))
405 `((lambda ,(mapcar 'car bindings) ,@body)
406 ,@(mapcar 'cadr bindings))))
408 ;;; A little backquote implementation without optimizations of any
409 ;;; kind for lispstrack.
410 (defun backquote-expand-1 (form)
416 ((eq (car form) 'unquote)
418 ((eq (car form) 'backquote)
419 (backquote-expand-1 (backquote-expand-1 (cadr form))))
424 ((and (listp s) (eq (car s) 'unquote))
425 (list 'list (cadr s)))
426 ((and (listp s) (eq (car s) 'unquote-splicing))
429 (list 'list (backquote-expand-1 s)))))
432 (defun backquote-expand (form)
433 (if (and (listp form) (eq (car form) 'backquote))
434 (backquote-expand-1 (cadr form))
437 (defmacro backquote (form)
438 (backquote-expand-1 form))
440 (define-transformation backquote (form)
441 (backquote-expand-1 form))
445 (define-compilation + (x y)
446 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
448 (define-compilation - (x y)
449 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
451 (define-compilation * (x y)
452 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
454 (define-compilation / (x y)
455 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
457 (define-compilation < (x y)
458 (concat "((" (ls-compile x env fenv) ") < (" (ls-compile y env fenv) "))"))
460 (define-compilation = (x y)
461 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
463 (define-compilation mod (x y)
464 (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
466 (define-compilation floor (x)
467 (concat "(Math.floor(" (ls-compile x env fenv) "))"))
469 (define-compilation null (x)
470 (concat "(" (ls-compile x env fenv) "== false)"))
472 (define-compilation cons (x y)
473 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
475 (define-compilation car (x)
476 (concat "(" (ls-compile x env fenv) ").car"))
478 (define-compilation cdr (x)
479 (concat "(" (ls-compile x env fenv) ").cdr"))
481 (define-compilation setcar (x new)
482 (concat "((" (ls-compile x env fenv) ").car = " (ls-compile new env fenv) ")"))
484 (define-compilation setcdr (x new)
485 (concat "((" (ls-compile x env fenv) ").cdr = " (ls-compile new env fenv) ")"))
487 (define-compilation make-symbol (name)
488 (concat "{name: " (ls-compile name env fenv) "}"))
490 (define-compilation symbol-name (x)
491 (concat "(" (ls-compile x env fenv) ").name"))
493 (define-compilation eq (x y)
494 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
496 (define-compilation equal (x y)
497 (concat "(" (ls-compile x env fenv) " == " (ls-compile y env fenv) ")"))
499 (define-compilation string (x)
500 (concat "String.fromCharCode(" (ls-compile x env fenv) ")"))
502 (define-compilation string-upcase (x)
503 (concat "(" (ls-compile x env fenv) ").toUpperCase()"))
505 (define-compilation string-length (x)
506 (concat "(" (ls-compile x env fenv) ").length"))
508 (define-compilation char (string index)
510 (ls-compile string env fenv)
512 (ls-compile index env fenv)
515 (define-compilation concat-two (string1 string2)
517 (ls-compile string1 env fenv)
519 (ls-compile string2 env fenv)
522 (define-compilation funcall (func &rest args)
524 (ls-compile func env fenv)
526 (join (mapcar (lambda (x)
527 (ls-compile x env fenv))
532 (define-compilation error (string)
533 (concat "(function (){ throw " (ls-compile string env fenv) ";" "return 0;})()"))
535 (define-compilation new ()
538 (define-compilation get (object key)
539 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
541 (define-compilation set (object key value)
543 (ls-compile object env fenv)
545 (ls-compile key env fenv) "]"
546 " = " (ls-compile value env fenv) ")"))
549 (and (symbolp x) (eq (binding-type (lookup-function x *fenv*)) 'macro)))
551 (defun ls-macroexpand-1 (form &optional env fenv)
552 (when (macrop (car form))
553 (let ((binding (lookup-function (car form) *env*)))
554 (if (eq (binding-type binding) 'macro)
555 (apply (eval (binding-translation binding)) (cdr form))
558 (defun compile-funcall (function args env fenv)
561 (concat (lookup-function-translation function fenv)
563 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
566 ((and (listp function) (eq (car function) 'lambda))
567 (concat "(" (ls-compile function env fenv) ")("
568 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
572 (error "Invalid function designator ~a." function))))
574 (defun ls-compile (sexp &optional env fenv)
576 ((symbolp sexp) (lookup-variable-translation sexp env))
577 ((integerp sexp) (integer-to-string sexp))
578 ((stringp sexp) (concat "\"" sexp "\""))
580 (if (assoc (car sexp) *compilations*)
581 (let ((comp (second (assoc (car sexp) *compilations*))))
582 (apply comp env fenv (cdr sexp)))
583 (if (macrop (car sexp))
584 (ls-compile (ls-macroexpand-1 sexp env fenv) env fenv)
585 (compile-funcall (car sexp) (cdr sexp) env fenv))))))
587 (defun ls-compile-toplevel (sexp)
588 (setq *toplevel-compilations* nil)
589 (let ((code (ls-compile sexp)))
591 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
592 *toplevel-compilations*)
595 (setq *toplevel-compilations* nil))))
599 (defun read-whole-file (filename)
600 (with-open-file (in filename)
601 (let ((seq (make-array (file-length in) :element-type 'character)))
602 (read-sequence seq in)
605 (defun ls-compile-file (filename output)
606 (setq *env* nil *fenv* nil)
607 (setq *compilation-unit-checks* nil)
608 (with-open-file (out output :direction :output :if-exists :supersede)
609 (let* ((source (read-whole-file filename))
610 (in (make-string-stream source)))
614 for compilation = (ls-compile-toplevel x)
615 when (plusp (length compilation))
616 do (write-line (concat compilation "; ") out))
617 (dolist (check *compilation-unit-checks*)
619 (setq *compilation-unit-checks* nil))))
622 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))