7 (defun !reduce (func list initial)
12 (funcall func initial (car list)))))
18 (defmacro while (condition &body body)
23 (defun concat-two (s1 s2)
24 (concatenate 'string s1 s2))
26 (defun setcar (cons new)
27 (setf (car cons) new))
28 (defun setcdr (cons new)
29 (setf (cdr cons) new)))
31 (defvar *newline* (string (code-char 10)))
33 (defun concat (&rest strs)
34 (!reduce (lambda (s1 s2) (concat-two s1 s2))
38 ;;; Concatenate a list of strings, with a separator
39 (defun join (list separator)
48 (join (cdr list) separator)))))
50 (defun join-trailing (list separator)
55 (join-trailing (cdr list) separator))
57 (concat (car list) separator (join-trailing (cdr list) separator)))))
59 (defun integer-to-string (x)
64 (push (mod x 10) digits)
65 (setq x (truncate x 10)))
66 (join (mapcar (lambda (d) (string (char "0123456789" d)))
72 ;;; It is a basic Lisp reader. It does not use advanced stuff
73 ;;; intentionally, because we want to use it to bootstrap a simple
74 ;;; Lisp. The main entry point is the function `ls-read', which
75 ;;; accepts a strings as argument and return the Lisp expression.
76 (defun make-string-stream (string)
79 (defun %peek-char (stream)
80 (and (< (cdr stream) (length (car stream)))
81 (char (car stream) (cdr stream))))
83 (defun %read-char (stream)
84 (and (< (cdr stream) (length (car stream)))
85 (prog1 (char (car stream) (cdr stream))
86 (setcdr stream (1+ (cdr stream))))))
88 (defun whitespacep (ch)
89 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
91 (defun skip-whitespaces (stream)
93 (setq ch (%peek-char stream))
94 (while (and ch (whitespacep ch))
96 (setq ch (%peek-char stream)))))
99 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
101 (defun read-until (stream func)
104 (setq ch (%peek-char stream))
105 (while (not (funcall func ch))
106 (setq string (concat string (string ch)))
108 (setq ch (%peek-char stream)))
111 (defun skip-whitespaces-and-comments (stream)
113 (skip-whitespaces stream)
114 (setq ch (%peek-char stream))
115 (while (and ch (eql ch #\;))
116 (read-until stream (lambda (x) (eql x #\newline)))
117 (skip-whitespaces stream)
118 (setq ch (%peek-char stream)))))
120 (defun %read-list (stream)
121 (skip-whitespaces-and-comments stream)
122 (let ((ch (%peek-char stream)))
129 (skip-whitespaces-and-comments stream)
130 (prog1 (ls-read stream)
131 (unless (char= (%read-char stream) #\))
132 (error "')' was expected."))))
134 (cons (ls-read stream) (%read-list stream))))))
136 (defvar *eof* (make-symbol "EOF"))
137 (defun ls-read (stream)
138 (skip-whitespaces-and-comments stream)
139 (let ((ch (%peek-char stream)))
148 (list 'quote (ls-read stream)))
151 (list 'backquote (ls-read stream)))
154 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
155 (%read-char stream)))
158 (if (eql (%peek-char stream) #\@)
159 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
160 (list 'unquote (ls-read stream))))
163 (ecase (%read-char stream)
165 (list 'function (ls-read stream)))
168 (concat (string (%read-char stream))
169 (read-until stream #'terminalp))))
171 ((string= cname "space") (char-code #\space))
172 ((string= cname "newline") (char-code #\newline))
173 (t (char-code (char cname 0))))))
175 (let ((feature (read-until stream #'terminalp)))
177 ((string= feature "common-lisp")
178 (ls-read stream) ;ignore
180 ((string= feature "lispstrack")
183 (error "Unknown reader form.")))))))
185 (let ((string (read-until stream #'terminalp)))
186 (if (every #'digit-char-p string)
187 (parse-integer string)
188 (intern (string-upcase string))))))))
190 (defun ls-read-from-string (string)
191 (ls-read (make-string-stream string)))
197 (defun make-var-binding (symbol)
198 (cons symbol (concat "v" (integer-to-string (incf counter))))))
201 (defun make-func-binding (symbol)
202 (cons symbol (concat "f" (integer-to-string (incf counter))))))
204 (defvar *compilations* nil)
206 (defun ls-compile-block (sexps env fenv)
208 (remove nil (mapcar (lambda (x)
209 (ls-compile x env fenv))
214 (defun extend-env (args env)
215 (append (mapcar #'make-var-binding args) env))
217 (defparameter *env* '())
218 (defparameter *fenv* '())
220 (defun lookup (symbol env)
221 (let ((binding (assoc symbol env)))
222 (and binding (cdr binding))))
224 (defun lookup-variable (symbol env)
225 (or (lookup symbol env)
226 (lookup symbol *env*)
227 (error "Undefined variable `~a'" symbol)))
229 (defun lookup-function (symbol env)
230 (or (lookup symbol env)
231 (lookup symbol *fenv*)
232 (error "Undefined function `~a'" symbol)))
234 (defmacro define-compilation (name args &body body)
235 ;; Creates a new primitive `name' with parameters args and
236 ;; @body. The body can access to the local environment through the
238 `(push (list ',name (lambda (env fenv ,@args) ,@body))
241 (defvar *toplevel-compilations*)
243 (define-compilation if (condition true false)
245 (ls-compile condition env fenv)
247 (ls-compile true env fenv)
249 (ls-compile false env fenv)
252 ;;; Return the required args of a lambda list
253 (defun lambda-list-required-argument (lambda-list)
254 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
256 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
258 (defun lambda-list-rest-argument (lambda-list)
259 (second (member '&rest lambda-list)))
261 (define-compilation lambda (lambda-list &rest body)
262 (let ((required-arguments (lambda-list-required-argument lambda-list))
263 (rest-argument (lambda-list-rest-argument lambda-list)))
264 (let ((new-env (extend-env (append (if rest-argument (list rest-argument))
267 (concat "(function ("
268 (join (mapcar (lambda (x) (lookup-variable x new-env))
274 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
275 "for (var i = arguments.length-1; i>="
276 (integer-to-string (length required-arguments))
278 (lookup-variable rest-argument new-env) " = "
279 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
282 (concat (ls-compile-block (butlast body) new-env fenv)
283 "return " (ls-compile (car (last body)) new-env fenv) ";")
287 (define-compilation fsetq (var val)
288 (concat (lookup-function var fenv)
290 (ls-compile val env fenv)))
292 (define-compilation setq (var val)
293 (concat (lookup-variable var env)
295 (ls-compile val env fenv)))
300 (defun literal->js (sexp)
302 ((null sexp) "undefined")
303 ((integerp sexp) (integer-to-string sexp))
304 ((stringp sexp) (concat "\"" sexp "\""))
305 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
306 ((consp sexp) (concat "{car: "
307 (literal->js (car sexp))
309 (literal->js (cdr sexp)) "}"))))
312 (defun literal (form)
315 (let ((var (concat "l" (integer-to-string (incf counter)))))
316 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
319 (define-compilation quote (sexp)
322 (define-compilation debug (form)
323 (concat "console.log(" (ls-compile form env fenv) ")"))
325 (define-compilation while (pred &rest body)
326 (concat "(function(){ while("
327 (ls-compile pred env fenv)
329 (ls-compile-block body env fenv)
332 (define-compilation function (x)
334 ((and (listp x) (eq (car x) 'lambda))
335 (ls-compile x env fenv))
337 (lookup-function x fenv))))
340 (defmacro eval-when-compile (&body body)
341 `(eval-when (:compile-toplevel :load-toplevel :execute)
344 (define-compilation eval-when-compile (&rest body)
345 (eval (cons 'progn body))
348 (defmacro define-transformation (name args form)
349 `(define-compilation ,name ,args
350 (ls-compile ,form env fenv)))
352 (define-transformation progn (&rest body)
353 `((lambda () ,@body)))
355 (define-transformation let (bindings &rest body)
356 (let ((bindings (mapcar #'ensure-list bindings)))
357 `((lambda ,(mapcar 'car bindings) ,@body)
358 ,@(mapcar 'cadr bindings))))
360 ;;; A little backquote implementation without optimizations of any
361 ;;; kind for lispstrack.
362 (defun backquote-expand-1 (form)
368 ((eq (car form) 'unquote)
370 ((eq (car form) 'backquote)
371 (backquote-expand-1 (backquote-expand-1 (cadr form))))
376 ((and (listp s) (eq (car s) 'unquote))
377 (list 'list (cadr s)))
378 ((and (listp s) (eq (car s) 'unquote-splicing))
381 (list 'list (backquote-expand-1 s)))))
384 (defun backquote-expand (form)
385 (if (and (listp form) (eq (car form) 'backquote))
386 (backquote-expand-1 (cadr form))
389 (defmacro backquote (form)
390 (backquote-expand-1 form))
392 (define-transformation backquote (form)
393 (backquote-expand-1 form))
397 (define-compilation + (x y)
398 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
400 (define-compilation - (x y)
401 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
403 (define-compilation * (x y)
404 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
406 (define-compilation / (x y)
407 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
409 (define-compilation < (x y)
410 (concat "((" (ls-compile x env fenv) ") < (" (ls-compile y env fenv) "))"))
412 (define-compilation = (x y)
413 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
415 (define-compilation mod (x y)
416 (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
418 (define-compilation floor (x)
419 (concat "(Math.floor(" (ls-compile x env fenv) "))"))
421 (define-compilation null (x)
422 (concat "(" (ls-compile x env fenv) "== undefined)"))
424 (define-compilation cons (x y)
425 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
427 (define-compilation car (x)
428 (concat "(" (ls-compile x env fenv) ").car"))
430 (define-compilation cdr (x)
431 (concat "(" (ls-compile x env fenv) ").cdr"))
433 (define-compilation setcar (x new)
434 (concat "((" (ls-compile x env fenv) ").car = " (ls-compile new env fenv) ")"))
436 (define-compilation setcdr (x new)
437 (concat "((" (ls-compile x env fenv) ").cdr = " (ls-compile new env fenv) ")"))
440 (define-compilation make-symbol (name)
441 (concat "{name: " (ls-compile name env fenv) "}"))
443 (define-compilation symbol-name (x)
444 (concat "(" (ls-compile x env fenv) ").name"))
446 (define-compilation eq (x y)
447 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
449 (define-compilation string (x)
450 (concat "String.fromCharCode(" (ls-compile x env fenv) ")"))
452 (define-compilation char (string index)
454 (ls-compile string env fenv)
456 (ls-compile index env fenv)
459 (define-compilation concat-two (string1 string2)
461 (ls-compile string1 env fenv)
463 (ls-compile string2 env fenv)
466 (define-compilation funcall (func &rest args)
468 (ls-compile func env fenv)
470 (join (mapcar (lambda (x)
471 (ls-compile x env fenv))
476 (define-compilation new ()
479 (define-compilation get (object key)
480 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
482 (define-compilation set (object key value)
484 (ls-compile object env fenv)
486 (ls-compile key env fenv) "]"
487 " = " (ls-compile value env fenv) ")"))
490 (defun %compile-defvar (name)
491 (unless (lookup name *env*)
492 (push (make-var-binding name) *env*)
493 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*)))
495 (defun %compile-defun (name)
496 (unless (lookup name *fenv*)
497 (push (make-func-binding name) *fenv*)
498 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*)))
500 (defun %compile-defmacro (name lambda)
501 (push (cons name (cons 'macro lambda)) *fenv*))
503 (defun ls-macroexpand-1 (form &optional env fenv)
504 (let ((function (cdr (assoc (car form) *fenv*))))
505 (if (and (listp function) (eq (car function) 'macro))
506 (apply (eval (cdr function)) (cdr form))
509 (defun compile-funcall (function args env fenv)
512 (concat (lookup-function function fenv)
514 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
517 ((and (listp function) (eq (car function) 'lambda))
518 (concat "(" (ls-compile function env fenv) ")("
519 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
523 (error "Invalid function designator ~a." function))))
525 (defun ls-compile (sexp &optional env fenv)
527 ((symbolp sexp) (lookup-variable sexp env))
528 ((integerp sexp) (integer-to-string sexp))
529 ((stringp sexp) (concat "\"" sexp "\""))
531 (if (assoc (car sexp) *compilations*)
532 (let ((comp (second (assoc (car sexp) *compilations*))))
533 (apply comp env fenv (cdr sexp)))
534 (let ((fn (cdr (assoc (car sexp) *fenv*))))
535 (if (and (listp fn) (eq (car fn) 'macro))
536 (ls-compile (ls-macroexpand-1 sexp env fenv) env fenv)
537 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
540 (defun ls-compile-toplevel (sexp)
541 (setq *toplevel-compilations* nil)
542 (let ((code (ls-compile sexp)))
544 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
545 *toplevel-compilations*)
548 (setq *toplevel-compilations* nil))))
553 (defun read-whole-file (filename)
554 (with-open-file (in filename)
555 (let ((seq (make-array (file-length in) :element-type 'character)))
556 (read-sequence seq in)
559 (defun ls-compile-file (filename output)
560 (setq *env* nil *fenv* nil)
561 (with-open-file (out output :direction :output :if-exists :supersede)
562 (let* ((source (read-whole-file filename))
563 (in (make-string-stream source)))
567 for compilation = (ls-compile-toplevel x)
568 when (plusp (length compilation))
569 do (write-line (concat compilation "; ") out)))))
572 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))