1 (defun !reduce (func list initial)
6 (funcall func initial (car list)))))
12 (defmacro while (condition &body body)
17 (defun concat-two (s1 s2)
18 (concatenate 'string s1 s2)))
20 (defvar *newline* (string (code-char 10)))
22 (defun concat (&rest strs)
23 (!reduce (lambda (s1 s2) (concat-two s1 s2))
27 ;;; Concatenate a list of strings, with a separator
28 (defun join (list separator)
37 (join (cdr list) separator)))))
39 (defun join-trailing (list separator)
44 (join-trailing (cdr list) separator))
46 (concat (car list) separator (join-trailing (cdr list) separator)))))
48 (defun integer-to-string (x)
53 (push (mod x 10) digits)
54 (setq x (truncate x 10)))
55 (join (mapcar (lambda (d) (string (char "0123456789" d)))
61 ;;; It is a basic Lisp reader. It does not use advanced stuff
62 ;;; intentionally, because we want to use it to bootstrap a simple
63 ;;; Lisp. The main entry point is the function `ls-read', which
64 ;;; accepts a strings as argument and return the Lisp expression.
65 (defun make-string-stream (string)
68 (defun %peek-char (stream)
69 (and (< (cdr stream) (length (car stream)))
70 (char (car stream) (cdr stream))))
72 (defun %read-char (stream)
73 (and (< (cdr stream) (length (car stream)))
74 (prog1 (char (car stream) (cdr stream))
75 (incf (cdr stream)))))
77 (defun whitespacep (ch)
78 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
80 (defun skip-whitespaces (stream)
82 (setq ch (%peek-char stream))
83 (while (and ch (whitespacep ch))
85 (setq ch (%peek-char stream)))))
88 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
90 (defun read-until (stream func)
93 (setq ch (%peek-char stream))
94 (while (not (funcall func ch))
95 (setq string (concat string (string ch)))
97 (setq ch (%peek-char stream)))
100 (defun skip-whitespaces-and-comments (stream)
102 (skip-whitespaces stream)
103 (setq ch (%peek-char stream))
104 (while (and ch (eql ch #\;))
105 (read-until stream (lambda (x) (eql x #\newline)))
106 (skip-whitespaces stream)
107 (setq ch (%peek-char stream)))))
109 (defun %read-list (stream)
110 (skip-whitespaces-and-comments stream)
111 (let ((ch (%peek-char stream)))
118 (skip-whitespaces-and-comments stream)
119 (prog1 (ls-read stream)
120 (unless (char= (%read-char stream) #\))
121 (error "')' was expected."))))
123 (cons (ls-read stream) (%read-list stream))))))
125 (defvar *eof* (make-symbol "EOF"))
126 (defun ls-read (stream)
127 (skip-whitespaces-and-comments stream)
128 (let ((ch (%peek-char stream)))
137 (list 'quote (ls-read stream)))
140 (list 'backquote (ls-read stream)))
143 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
144 (%read-char stream)))
147 (if (eql (%peek-char stream) #\@)
148 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
149 (list 'unquote (ls-read stream))))
152 (ecase (%read-char stream)
154 (list 'function (ls-read stream)))
158 (let ((feature (read-until stream #'terminalp)))
160 ((string= feature "common-lisp")
161 (ls-read stream);ignore
163 ((string= feature "lispstrack")
166 (error "Unknown reader form.")))))
169 (let ((string (read-until stream #'terminalp)))
170 (if (every #'digit-char-p string)
171 (parse-integer string)
172 (intern (string-upcase string))))))))
174 (defun ls-read-from-string (string)
175 (ls-read (make-string-stream string)))
181 (defun make-var-binding (symbol)
182 (cons symbol (concat "v" (integer-to-string (incf counter))))))
185 (defun make-func-binding (symbol)
186 (cons symbol (concat "f" (integer-to-string (incf counter))))))
188 (defvar *compilations* nil)
190 (defun ls-compile-block (sexps env fenv)
192 (remove nil (mapcar (lambda (x)
193 (ls-compile x env fenv))
198 (defun extend-env (args env)
199 (append (mapcar #'make-var-binding args) env))
201 (defparameter *env* '())
202 (defparameter *fenv* '())
204 (defun lookup (symbol env)
205 (let ((binding (assoc symbol env)))
206 (and binding (cdr binding))))
208 (defun lookup-variable (symbol env)
209 (or (lookup symbol env)
210 (lookup symbol *env*)
211 (error "Undefined variable `~a'" symbol)))
213 (defun lookup-function (symbol env)
214 (or (lookup symbol env)
215 (lookup symbol *fenv*)
216 (error "Undefined function `~a'" symbol)))
218 (defmacro define-compilation (name args &body body)
219 ;; Creates a new primitive `name' with parameters args and
220 ;; @body. The body can access to the local environment through the
222 `(push (list ',name (lambda (env fenv ,@args) ,@body))
225 (defvar *toplevel-compilations*)
227 (define-compilation if (condition true false)
229 (ls-compile condition env fenv)
231 (ls-compile true env fenv)
233 (ls-compile false env fenv)
236 ;;; Return the required args of a lambda list
237 (defun lambda-list-required-argument (lambda-list)
238 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
240 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
242 (defun lambda-list-rest-argument (lambda-list)
243 (second (member '&rest lambda-list)))
245 (define-compilation lambda (lambda-list &rest body)
246 (let ((required-arguments (lambda-list-required-argument lambda-list))
247 (rest-argument (lambda-list-rest-argument lambda-list)))
248 (let ((new-env (extend-env (append (if rest-argument (list rest-argument))
251 (concat "(function ("
252 (join (mapcar (lambda (x) (lookup-variable x new-env))
258 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
259 "for (var i = arguments.length-1; i>="
260 (integer-to-string (length required-arguments))
262 (lookup-variable rest-argument new-env) " = "
263 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
266 (concat (ls-compile-block (butlast body) new-env fenv)
267 "return " (ls-compile (car (last body)) new-env fenv) ";")
271 (define-compilation fsetq (var val)
272 (concat (lookup-function var fenv)
274 (ls-compile val env fenv)))
276 (define-compilation setq (var val)
277 (concat (lookup-variable var env)
279 (ls-compile val env fenv)))
284 (defun literal->js (sexp)
286 ((null sexp) "undefined")
287 ((integerp sexp) (integer-to-string sexp))
288 ((stringp sexp) (concat "\"" sexp "\""))
289 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
290 ((consp sexp) (concat "{car: "
291 (literal->js (car sexp))
293 (literal->js (cdr sexp)) "}"))))
296 (defun literal (form)
299 (let ((var (concat "l" (integer-to-string (incf counter)))))
300 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
303 (define-compilation quote (sexp)
306 (define-compilation debug (form)
307 (concat "console.log(" (ls-compile form env fenv) ")"))
309 (define-compilation while (pred &rest body)
310 (concat "(function(){ while("
311 (ls-compile pred env fenv)
313 (ls-compile-block body env fenv)
316 (define-compilation function (x)
318 ((and (listp x) (eq (car x) 'lambda))
319 (ls-compile x env fenv))
321 (lookup-function x fenv))))
324 (defmacro eval-when-compile (&body body)
325 `(eval-when (:compile-toplevel :load-toplevel :execute)
328 (define-compilation eval-when-compile (&rest body)
329 (eval (cons 'progn body))
332 (defmacro define-transformation (name args form)
333 `(define-compilation ,name ,args
334 (ls-compile ,form env fenv)))
336 (define-transformation progn (&rest body)
337 `((lambda () ,@body)))
339 (define-transformation let (bindings &rest body)
340 `((lambda ,(mapcar 'car bindings) ,@body)
341 ,@(mapcar 'cadr bindings)))
343 ;;; A little backquote implementation without optimizations of any
344 ;;; kind for lispstrack.
345 (defun backquote-expand-1 (form)
351 ((eq (car form) 'unquote)
353 ((eq (car form) 'backquote)
354 (backquote-expand-1 (backquote-expand-1 (cadr form))))
359 ((and (listp s) (eq (car s) 'unquote))
360 (list 'list (cadr s)))
361 ((and (listp s) (eq (car s) 'unquote-splicing))
364 (list 'list (backquote-expand-1 s)))))
367 (defun backquote-expand (form)
368 (if (and (listp form) (eq (car form) 'backquote))
369 (backquote-expand-1 (cadr form))
372 (defmacro backquote (form)
373 (backquote-expand-1 form))
375 (define-transformation backquote (form)
376 (backquote-expand-1 form))
380 (define-compilation + (x y)
381 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
383 (define-compilation - (x y)
384 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
386 (define-compilation * (x y)
387 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
389 (define-compilation / (x y)
390 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
392 (define-compilation < (x y)
393 (concat "((" (ls-compile x env fenv) ") < (" (ls-compile y env fenv) "))"))
395 (define-compilation = (x y)
396 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
398 (define-compilation mod (x y)
399 (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
401 (define-compilation floor (x)
402 (concat "(Math.floor(" (ls-compile x env fenv) "))"))
404 (define-compilation null (x)
405 (concat "(" (ls-compile x env fenv) "== undefined)"))
407 (define-compilation cons (x y)
408 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
410 (define-compilation car (x)
411 (concat "(" (ls-compile x env fenv) ").car"))
413 (define-compilation cdr (x)
414 (concat "(" (ls-compile x env fenv) ").cdr"))
416 (define-compilation make-symbol (name)
417 (concat "{name: " (ls-compile name env fenv) "}"))
419 (define-compilation symbol-name (x)
420 (concat "(" (ls-compile x env fenv) ").name"))
422 (define-compilation eq (x y)
423 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
425 (define-compilation string (x)
426 (concat "String.fromCharCode(" (ls-compile x env fenv) ")"))
428 (define-compilation char (string index)
430 (ls-compile string env fenv)
432 (ls-compile index env fenv)
435 (define-compilation concat-two (string1 string2)
437 (ls-compile string1 env fenv)
439 (ls-compile string2 env fenv)
442 (define-compilation funcall (func &rest args)
444 (ls-compile func env fenv)
446 (join (mapcar (lambda (x)
447 (ls-compile x env fenv))
452 (define-compilation new ()
455 (define-compilation get (object key)
456 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
458 (define-compilation set (object key value)
460 (ls-compile object env fenv)
462 (ls-compile key env fenv) "]"
463 " = " (ls-compile value env fenv) ")"))
466 (defun %compile-defvar (name)
467 (unless (lookup name *env*)
468 (push (make-var-binding name) *env*)
469 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*)))
471 (defun %compile-defun (name)
472 (unless (lookup name *fenv*)
473 (push (make-func-binding name) *fenv*)
474 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*)))
476 (defun %compile-defmacro (name lambda)
477 (push (cons name (cons 'macro lambda)) *fenv*))
479 (defun ls-macroexpand-1 (form &optional env fenv)
480 (let ((function (cdr (assoc (car form) *fenv*))))
481 (if (and (listp function) (eq (car function) 'macro))
482 (apply (eval (cdr function)) (cdr form))
485 (defun compile-funcall (function args env fenv)
488 (concat (lookup-function function fenv)
490 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
493 ((and (listp function) (eq (car function) 'lambda))
494 (concat "(" (ls-compile function env fenv) ")("
495 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
499 (error "Invalid function designator ~a." function))))
501 (defun ls-compile (sexp &optional env fenv)
503 ((symbolp sexp) (lookup-variable sexp env))
504 ((integerp sexp) (integer-to-string sexp))
505 ((stringp sexp) (concat "\"" sexp "\""))
507 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
508 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
510 (apply compiler-func env fenv (cdr sexp))
511 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
513 (defun ls-compile-toplevel (sexp)
514 (setq *toplevel-compilations* nil)
515 (let ((code (ls-compile sexp)))
517 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
518 *toplevel-compilations*)
521 (setq *toplevel-compilations* nil))))
526 (defun read-whole-file (filename)
527 (with-open-file (in filename)
528 (let ((seq (make-array (file-length in) :element-type 'character)))
529 (read-sequence seq in)
532 (defun ls-compile-file (filename output)
533 (setq *env* nil *fenv* nil)
534 (with-open-file (out output :direction :output :if-exists :supersede)
535 (let* ((source (read-whole-file filename))
536 (in (make-string-stream source)))
540 for compilation = (ls-compile-toplevel x)
541 when (plusp (length compilation))
542 do (write-line (concat compilation "; ") out)))))
545 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))