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)))
167 (let ((cname (read-until stream #'terminalp)))
169 ((string= cname "space") (char-code #\space))
170 ((string= cname "newline") (char-code #\newline))
171 (t (char-code (char cname 0))))))
173 (let ((feature (read-until stream #'terminalp)))
175 ((string= feature "common-lisp")
176 (ls-read stream) ;ignore
178 ((string= feature "lispstrack")
181 (error "Unknown reader form.")))))))
183 (let ((string (read-until stream #'terminalp)))
184 (if (every #'digit-char-p string)
185 (parse-integer string)
186 (intern (string-upcase string))))))))
188 (defun ls-read-from-string (string)
189 (ls-read (make-string-stream string)))
195 (defun make-var-binding (symbol)
196 (cons symbol (concat "v" (integer-to-string (incf counter))))))
199 (defun make-func-binding (symbol)
200 (cons symbol (concat "f" (integer-to-string (incf counter))))))
202 (defvar *compilations* nil)
204 (defun ls-compile-block (sexps env fenv)
206 (remove nil (mapcar (lambda (x)
207 (ls-compile x env fenv))
212 (defun extend-env (args env)
213 (append (mapcar #'make-var-binding args) env))
215 (defparameter *env* '())
216 (defparameter *fenv* '())
218 (defun lookup (symbol env)
219 (let ((binding (assoc symbol env)))
220 (and binding (cdr binding))))
222 (defun lookup-variable (symbol env)
223 (or (lookup symbol env)
224 (lookup symbol *env*)
225 (error "Undefined variable `~a'" symbol)))
227 (defun lookup-function (symbol env)
228 (or (lookup symbol env)
229 (lookup symbol *fenv*)
230 (error "Undefined function `~a'" symbol)))
232 (defmacro define-compilation (name args &body body)
233 ;; Creates a new primitive `name' with parameters args and
234 ;; @body. The body can access to the local environment through the
236 `(push (list ',name (lambda (env fenv ,@args) ,@body))
239 (defvar *toplevel-compilations*)
241 (define-compilation if (condition true false)
243 (ls-compile condition env fenv)
245 (ls-compile true env fenv)
247 (ls-compile false env fenv)
250 ;;; Return the required args of a lambda list
251 (defun lambda-list-required-argument (lambda-list)
252 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
254 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
256 (defun lambda-list-rest-argument (lambda-list)
257 (second (member '&rest lambda-list)))
259 (define-compilation lambda (lambda-list &rest body)
260 (let ((required-arguments (lambda-list-required-argument lambda-list))
261 (rest-argument (lambda-list-rest-argument lambda-list)))
262 (let ((new-env (extend-env (append (if rest-argument (list rest-argument))
265 (concat "(function ("
266 (join (mapcar (lambda (x) (lookup-variable x new-env))
272 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
273 "for (var i = arguments.length-1; i>="
274 (integer-to-string (length required-arguments))
276 (lookup-variable rest-argument new-env) " = "
277 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
280 (concat (ls-compile-block (butlast body) new-env fenv)
281 "return " (ls-compile (car (last body)) new-env fenv) ";")
285 (define-compilation fsetq (var val)
286 (concat (lookup-function var fenv)
288 (ls-compile val env fenv)))
290 (define-compilation setq (var val)
291 (concat (lookup-variable var env)
293 (ls-compile val env fenv)))
298 (defun literal->js (sexp)
300 ((null sexp) "undefined")
301 ((integerp sexp) (integer-to-string sexp))
302 ((stringp sexp) (concat "\"" sexp "\""))
303 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
304 ((consp sexp) (concat "{car: "
305 (literal->js (car sexp))
307 (literal->js (cdr sexp)) "}"))))
310 (defun literal (form)
313 (let ((var (concat "l" (integer-to-string (incf counter)))))
314 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
317 (define-compilation quote (sexp)
320 (define-compilation debug (form)
321 (concat "console.log(" (ls-compile form env fenv) ")"))
323 (define-compilation while (pred &rest body)
324 (concat "(function(){ while("
325 (ls-compile pred env fenv)
327 (ls-compile-block body env fenv)
330 (define-compilation function (x)
332 ((and (listp x) (eq (car x) 'lambda))
333 (ls-compile x env fenv))
335 (lookup-function x fenv))))
338 (defmacro eval-when-compile (&body body)
339 `(eval-when (:compile-toplevel :load-toplevel :execute)
342 (define-compilation eval-when-compile (&rest body)
343 (eval (cons 'progn body))
346 (defmacro define-transformation (name args form)
347 `(define-compilation ,name ,args
348 (ls-compile ,form env fenv)))
350 (define-transformation progn (&rest body)
351 `((lambda () ,@body)))
353 (define-transformation let (bindings &rest body)
354 (let ((bindings (mapcar #'ensure-list bindings)))
355 `((lambda ,(mapcar 'car bindings) ,@body)
356 ,@(mapcar 'cadr bindings))))
358 ;;; A little backquote implementation without optimizations of any
359 ;;; kind for lispstrack.
360 (defun backquote-expand-1 (form)
366 ((eq (car form) 'unquote)
368 ((eq (car form) 'backquote)
369 (backquote-expand-1 (backquote-expand-1 (cadr form))))
374 ((and (listp s) (eq (car s) 'unquote))
375 (list 'list (cadr s)))
376 ((and (listp s) (eq (car s) 'unquote-splicing))
379 (list 'list (backquote-expand-1 s)))))
382 (defun backquote-expand (form)
383 (if (and (listp form) (eq (car form) 'backquote))
384 (backquote-expand-1 (cadr form))
387 (defmacro backquote (form)
388 (backquote-expand-1 form))
390 (define-transformation backquote (form)
391 (backquote-expand-1 form))
395 (define-compilation + (x y)
396 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
398 (define-compilation - (x y)
399 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
401 (define-compilation * (x y)
402 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
404 (define-compilation / (x y)
405 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
407 (define-compilation < (x y)
408 (concat "((" (ls-compile x env fenv) ") < (" (ls-compile y env fenv) "))"))
410 (define-compilation = (x y)
411 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
413 (define-compilation mod (x y)
414 (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
416 (define-compilation floor (x)
417 (concat "(Math.floor(" (ls-compile x env fenv) "))"))
419 (define-compilation null (x)
420 (concat "(" (ls-compile x env fenv) "== undefined)"))
422 (define-compilation cons (x y)
423 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
425 (define-compilation car (x)
426 (concat "(" (ls-compile x env fenv) ").car"))
428 (define-compilation cdr (x)
429 (concat "(" (ls-compile x env fenv) ").cdr"))
431 (define-compilation setcar (x new)
432 (concat "((" (ls-compile x env fenv) ").car = " (ls-compile new env fenv) ")"))
434 (define-compilation setcdr (x new)
435 (concat "((" (ls-compile x env fenv) ").cdr = " (ls-compile new env fenv) ")"))
438 (define-compilation make-symbol (name)
439 (concat "{name: " (ls-compile name env fenv) "}"))
441 (define-compilation symbol-name (x)
442 (concat "(" (ls-compile x env fenv) ").name"))
444 (define-compilation eq (x y)
445 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
447 (define-compilation string (x)
448 (concat "String.fromCharCode(" (ls-compile x env fenv) ")"))
450 (define-compilation char (string index)
452 (ls-compile string env fenv)
454 (ls-compile index env fenv)
457 (define-compilation concat-two (string1 string2)
459 (ls-compile string1 env fenv)
461 (ls-compile string2 env fenv)
464 (define-compilation funcall (func &rest args)
466 (ls-compile func env fenv)
468 (join (mapcar (lambda (x)
469 (ls-compile x env fenv))
474 (define-compilation new ()
477 (define-compilation get (object key)
478 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
480 (define-compilation set (object key value)
482 (ls-compile object env fenv)
484 (ls-compile key env fenv) "]"
485 " = " (ls-compile value env fenv) ")"))
488 (defun %compile-defvar (name)
489 (unless (lookup name *env*)
490 (push (make-var-binding name) *env*)
491 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*)))
493 (defun %compile-defun (name)
494 (unless (lookup name *fenv*)
495 (push (make-func-binding name) *fenv*)
496 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*)))
498 (defun %compile-defmacro (name lambda)
499 (push (cons name (cons 'macro lambda)) *fenv*))
501 (defun ls-macroexpand-1 (form &optional env fenv)
502 (let ((function (cdr (assoc (car form) *fenv*))))
503 (if (and (listp function) (eq (car function) 'macro))
504 (apply (eval (cdr function)) (cdr form))
507 (defun compile-funcall (function args env fenv)
510 (concat (lookup-function function fenv)
512 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
515 ((and (listp function) (eq (car function) 'lambda))
516 (concat "(" (ls-compile function env fenv) ")("
517 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
521 (error "Invalid function designator ~a." function))))
523 (defun ls-compile (sexp &optional env fenv)
525 ((symbolp sexp) (lookup-variable sexp env))
526 ((integerp sexp) (integer-to-string sexp))
527 ((stringp sexp) (concat "\"" sexp "\""))
529 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
530 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
532 (apply compiler-func env fenv (cdr sexp))
533 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
535 (defun ls-compile-toplevel (sexp)
536 (setq *toplevel-compilations* nil)
537 (let ((code (ls-compile sexp)))
539 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
540 *toplevel-compilations*)
543 (setq *toplevel-compilations* nil))))
548 (defun read-whole-file (filename)
549 (with-open-file (in filename)
550 (let ((seq (make-array (file-length in) :element-type 'character)))
551 (read-sequence seq in)
554 (defun ls-compile-file (filename output)
555 (setq *env* nil *fenv* nil)
556 (with-open-file (out output :direction :output :if-exists :supersede)
557 (let* ((source (read-whole-file filename))
558 (in (make-string-stream source)))
562 for compilation = (ls-compile-toplevel x)
563 when (plusp (length compilation))
564 do (write-line (concat compilation "; ") out)))))
567 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))