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 (defun read-string (stream)
138 (setq ch (%read-char stream))
139 (while (not (char= ch #\"))
141 (setq ch (%read-char stream)))
142 (setq string (concat string (string ch)))
143 (setq ch (%read-char stream)))
146 (defvar *eof* (make-symbol "EOF"))
147 (defun ls-read (stream)
148 (skip-whitespaces-and-comments stream)
149 (let ((ch (%peek-char stream)))
158 (list 'quote (ls-read stream)))
161 (list 'backquote (ls-read stream)))
164 (read-string stream))
167 (if (eql (%peek-char stream) #\@)
168 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
169 (list 'unquote (ls-read stream))))
172 (ecase (%read-char stream)
174 (list 'function (ls-read stream)))
177 (concat (string (%read-char stream))
178 (read-until stream #'terminalp))))
180 ((string= cname "space") (char-code #\space))
181 ((string= cname "tab") (char-code #\tab))
182 ((string= cname "newline") (char-code #\newline))
183 (t (char-code (char cname 0))))))
185 (let ((feature (read-until stream #'terminalp)))
187 ((string= feature "common-lisp")
188 (ls-read stream) ;ignore
190 ((string= feature "lispstrack")
193 (error "Unknown reader form.")))))))
195 (let ((string (read-until stream #'terminalp)))
196 (if (every #'digit-char-p string)
197 (parse-integer string)
198 (intern (string-upcase string))))))))
200 (defun ls-read-from-string (string)
201 (ls-read (make-string-stream string)))
206 (defvar *compilation-unit-checks* '())
211 (defun make-binding (name type js declared)
212 (list name type js declared))
214 (defun binding-name (b) (first b))
215 (defun binding-type (b) (second b))
216 (defun binding-translation (b) (third b))
217 (defun binding-declared (b)
219 (defun mark-binding-as-declared (b)
220 (setcar (cdddr b) t))
223 (defun gvarname (symbol)
224 (concat "v" (integer-to-string (incf counter))))
226 (defun lookup-variable (symbol env)
227 (or (assoc symbol env)
229 (let ((name (symbol-name symbol))
230 (binding (make-binding symbol 'variable (gvarname symbol) nil)))
233 (unless (binding-declared (assoc symbol *env*))
234 (error (concat "Undefined variable `" name "'"))))
235 *compilation-unit-checks*)
238 (defun lookup-variable-translation (symbol env)
239 (binding-translation (lookup-variable symbol env)))
241 (defun extend-local-env (args env)
242 (append (mapcar (lambda (symbol)
243 (make-binding symbol 'variable (gvarname symbol) t))
248 (defun lookup-function (symbol env)
249 (or (assoc symbol env)
250 (assoc symbol *fenv*)
251 (let ((name (symbol-name symbol))
255 (concat "f" (integer-to-string (incf counter)))
257 (push binding *fenv*)
259 (unless (binding-declared (assoc symbol *fenv*))
260 (error (concat "Undefined function `" name "'"))))
261 *compilation-unit-checks*)
264 (defun lookup-function-translation (symbol env)
265 (binding-translation (lookup-function symbol env))))
268 (defvar *toplevel-compilations* nil)
270 (defun %compile-defvar (name)
271 (let ((b (lookup-variable name *env*)))
272 (mark-binding-as-declared b)
273 (push (concat "var " (binding-translation b)) *toplevel-compilations*)))
275 (defun %compile-defun (name)
276 (let ((b (lookup-function name *env*)))
277 (mark-binding-as-declared b)
278 (push (concat "var " (binding-translation b)) *toplevel-compilations*)))
280 (defun %compile-defmacro (name lambda)
281 (push (make-binding name 'macro lambda t) *fenv*))
284 (defvar *compilations* nil)
286 (defun ls-compile-block (sexps env fenv)
288 (remove nil (mapcar (lambda (x)
289 (ls-compile x env fenv))
293 (defmacro define-compilation (name args &rest body)
294 ;; Creates a new primitive `name' with parameters args and
295 ;; @body. The body can access to the local environment through the
297 `(push (list ',name (lambda (env fenv ,@args) ,@body))
300 (define-compilation if (condition true false)
302 (ls-compile condition env fenv) " !== " (ls-compile nil nil nil)
304 (ls-compile true env fenv)
306 (ls-compile false env fenv)
309 ;;; Return the required args of a lambda list
310 (defun lambda-list-required-argument (lambda-list)
311 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
313 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
315 (defun lambda-list-rest-argument (lambda-list)
316 (second (member '&rest lambda-list)))
318 (define-compilation lambda (lambda-list &rest body)
319 (let ((required-arguments (lambda-list-required-argument lambda-list))
320 (rest-argument (lambda-list-rest-argument lambda-list)))
321 (let ((new-env (extend-local-env
322 (append (and rest-argument (list rest-argument))
325 (concat "(function ("
326 (join (mapcar (lambda (x)
327 (lookup-variable-translation x new-env))
333 (let ((js!rest (lookup-variable-translation rest-argument new-env)))
334 (concat "var " js!rest "= " (ls-compile nil env fenv) ";" *newline*
335 "for (var i = arguments.length-1; i>="
336 (integer-to-string (length required-arguments))
339 "{car: arguments[i], cdr: " js!rest "};"
342 (concat (ls-compile-block (butlast body) new-env fenv)
343 "return " (ls-compile (car (last body)) new-env fenv) ";")
347 (define-compilation fsetq (var val)
348 (concat (lookup-function-translation var fenv)
350 (ls-compile val env fenv)))
352 (define-compilation setq (var val)
353 (concat (lookup-variable-translation var env)
355 (ls-compile val env fenv)))
359 (defun escape-string (string)
362 (size (length string)))
363 (while (< index size)
364 (let ((ch (char string index)))
365 (when (or (char= ch #\") (char= ch #\\))
366 (setq output (concat output "\\")))
367 (when (or (char= ch #\newline))
368 (setq output (concat output "\\"))
370 (setq output (concat output (string ch))))
374 (defun literal->js (sexp)
376 ((integerp sexp) (integer-to-string sexp))
377 ((stringp sexp) (concat "\"" (escape-string sexp) "\""))
378 ((symbolp sexp) (ls-compile `(intern ,(escape-string (symbol-name sexp))) *env* *fenv*))
379 ((consp sexp) (concat "{car: "
380 (literal->js (car sexp))
382 (literal->js (cdr sexp)) "}"))))
385 (defun literal (form)
386 (let ((var (concat "l" (integer-to-string (incf counter)))))
387 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
390 (define-compilation quote (sexp)
393 (define-compilation debug (form)
394 (concat "console.log(" (ls-compile form env fenv) ")"))
396 (define-compilation while (pred &rest body)
397 (concat "(function(){ while("
398 (ls-compile pred env fenv) " !== " (ls-compile nil nil nil)
400 (ls-compile-block body env fenv)
403 (define-compilation function (x)
405 ((and (listp x) (eq (car x) 'lambda))
406 (ls-compile x env fenv))
408 (lookup-function-translation x fenv))))
411 c(defmacro eval-when-compile (&body body)
412 `(eval-when (:compile-toplevel :load-toplevel :execute)
415 (define-compilation eval-when-compile (&rest body)
416 (eval (cons 'progn body))
419 (defmacro define-transformation (name args form)
420 `(define-compilation ,name ,args
421 (ls-compile ,form env fenv)))
423 (define-transformation progn (&rest body)
424 `((lambda () ,@body)))
426 (define-transformation let (bindings &rest body)
427 (let ((bindings (mapcar #'ensure-list bindings)))
428 `((lambda ,(mapcar 'car bindings) ,@body)
429 ,@(mapcar 'cadr bindings))))
431 ;;; A little backquote implementation without optimizations of any
432 ;;; kind for lispstrack.
433 (defun backquote-expand-1 (form)
439 ((eq (car form) 'unquote)
441 ((eq (car form) 'backquote)
442 (backquote-expand-1 (backquote-expand-1 (cadr form))))
447 ((and (listp s) (eq (car s) 'unquote))
448 (list 'list (cadr s)))
449 ((and (listp s) (eq (car s) 'unquote-splicing))
452 (list 'list (backquote-expand-1 s)))))
455 (defun backquote-expand (form)
456 (if (and (listp form) (eq (car form) 'backquote))
457 (backquote-expand-1 (cadr form))
460 (defmacro backquote (form)
461 (backquote-expand-1 form))
463 (define-transformation backquote (form)
464 (backquote-expand-1 form))
468 (defun compile-bool (x)
469 (concat "(" x "?" (ls-compile t nil nil) ": " (ls-compile nil nil nil) ")"))
471 (define-compilation + (x y)
472 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
474 (define-compilation - (x y)
475 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
477 (define-compilation * (x y)
478 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
480 (define-compilation / (x y)
481 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
483 (define-compilation < (x y)
484 (compile-bool (concat "((" (ls-compile x env fenv) ") < (" (ls-compile y env fenv) "))")))
486 (define-compilation = (x y)
487 (compile-bool (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))")))
489 (define-compilation numberp (x)
490 (compile-bool (concat "(typeof (" (ls-compile x env fenv) ") == \"number\")")))
493 (define-compilation mod (x y)
494 (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
496 (define-compilation floor (x)
497 (concat "(Math.floor(" (ls-compile x env fenv) "))"))
499 (define-compilation null (x)
500 (compile-bool (concat "(" (ls-compile x env fenv) "===" (ls-compile nil env fenv) ")")))
502 (define-compilation cons (x y)
503 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
505 (define-compilation consp (x)
507 (concat "(function(){ var tmp = "
508 (ls-compile x env fenv)
509 "; return (typeof tmp == 'object' && 'car' in tmp);})()")))
511 (define-compilation car (x)
512 (concat "(" (ls-compile x env fenv) ").car"))
514 (define-compilation cdr (x)
515 (concat "(" (ls-compile x env fenv) ").cdr"))
517 (define-compilation setcar (x new)
518 (concat "((" (ls-compile x env fenv) ").car = " (ls-compile new env fenv) ")"))
520 (define-compilation setcdr (x new)
521 (concat "((" (ls-compile x env fenv) ").cdr = " (ls-compile new env fenv) ")"))
523 (define-compilation symbolp (x)
525 (concat "(function(){ var tmp = "
526 (ls-compile x env fenv)
527 "; return (typeof tmp == 'object' && 'name' in tmp); })()")))
529 (define-compilation make-symbol (name)
530 (concat "{name: " (ls-compile name env fenv) "}"))
532 (define-compilation symbol-name (x)
533 (concat "(" (ls-compile x env fenv) ").name"))
535 (define-compilation eq (x y)
537 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")")))
539 (define-compilation equal (x y)
541 (concat "(" (ls-compile x env fenv) " == " (ls-compile y env fenv) ")")))
543 (define-compilation string (x)
544 (concat "String.fromCharCode(" (ls-compile x env fenv) ")"))
546 (define-compilation stringp (x)
548 (concat "(typeof(" (ls-compile x env fenv) ") == \"string\")")))
550 (define-compilation string-upcase (x)
551 (concat "(" (ls-compile x env fenv) ").toUpperCase()"))
553 (define-compilation string-length (x)
554 (concat "(" (ls-compile x env fenv) ").length"))
556 (define-compilation char (string index)
558 (ls-compile string env fenv)
560 (ls-compile index env fenv)
563 (define-compilation concat-two (string1 string2)
565 (ls-compile string1 env fenv)
567 (ls-compile string2 env fenv)
570 (define-compilation funcall (func &rest args)
572 (ls-compile func env fenv)
574 (join (mapcar (lambda (x)
575 (ls-compile x env fenv))
580 (define-compilation apply (func &rest args)
582 (concat "(" (ls-compile func env fenv) ")()")
583 (let ((args (butlast args))
584 (last (car (last args))))
585 (concat "(function(){" *newline*
586 "var f = " (ls-compile func env fenv) ";" *newline*
587 "var args = [" (join (mapcar (lambda (x)
588 (ls-compile x env fenv))
592 "var tail = (" (ls-compile last env fenv) ");" *newline*
593 "while (tail != " (ls-compile nil env fenv) "){" *newline*
594 " args.push(tail.car);" *newline*
595 " tail = tail.cdr;" *newline*
597 "return f.apply(this, args);" *newline*
600 (define-compilation js-eval (string)
601 (concat "eval(" (ls-compile string env fenv) ")"))
604 (define-compilation error (string)
605 (concat "(function (){ throw " (ls-compile string env fenv) ";" "return 0;})()"))
607 (define-compilation new ()
610 (define-compilation get (object key)
611 (concat "(function(){ var tmp = "
612 "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"
614 "return tmp == undefined? " (ls-compile nil nil nil) ": tmp ;"
617 (define-compilation set (object key value)
619 (ls-compile object env fenv)
621 (ls-compile key env fenv) "]"
622 " = " (ls-compile value env fenv) ")"))
624 (define-compilation in (key object)
626 (concat "(" (ls-compile key env fenv) " in " (ls-compile object env fenv) ")")))
630 (and (symbolp x) (eq (binding-type (lookup-function x *fenv*)) 'macro)))
632 (defun ls-macroexpand-1 (form env fenv)
633 (when (macrop (car form))
634 (let ((binding (lookup-function (car form) *env*)))
635 (if (eq (binding-type binding) 'macro)
636 (apply (eval (binding-translation binding)) (cdr form))
639 (defun compile-funcall (function args env fenv)
642 (concat (lookup-function-translation function fenv)
644 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
647 ((and (listp function) (eq (car function) 'lambda))
648 (concat "(" (ls-compile function env fenv) ")("
649 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
653 (error (concat "Invalid function designator " (symbol-name function))))))
655 (defun ls-compile (sexp env fenv)
657 ((symbolp sexp) (lookup-variable-translation sexp env))
658 ((integerp sexp) (integer-to-string sexp))
659 ((stringp sexp) (concat "\"" (escape-string sexp) "\""))
661 (if (assoc (car sexp) *compilations*)
662 (let ((comp (second (assoc (car sexp) *compilations*))))
663 (apply comp env fenv (cdr sexp)))
664 (if (macrop (car sexp))
665 (ls-compile (ls-macroexpand-1 sexp env fenv) env fenv)
666 (compile-funcall (car sexp) (cdr sexp) env fenv))))))
668 (defun ls-compile-toplevel (sexp)
669 (setq *toplevel-compilations* nil)
670 (let ((code (ls-compile sexp nil nil)))
672 (concat "/* " (princ-to-string sexp) " */"
673 (join (mapcar (lambda (x) (concat x ";" *newline*))
674 *toplevel-compilations*)
677 (setq *toplevel-compilations* nil))))
681 (defun read-whole-file (filename)
682 (with-open-file (in filename)
683 (let ((seq (make-array (file-length in) :element-type 'character)))
684 (read-sequence seq in)
687 (defun ls-compile-file (filename output)
688 (setq *env* nil *fenv* nil)
689 (setq *compilation-unit-checks* nil)
690 (with-open-file (out output :direction :output :if-exists :supersede)
691 (let* ((source (read-whole-file filename))
692 (in (make-string-stream source)))
696 for compilation = (ls-compile-toplevel x)
697 when (plusp (length compilation))
698 do (write-line (concat compilation "; ") out))
699 (dolist (check *compilation-unit-checks*)
701 (setq *compilation-unit-checks* nil))))
704 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))