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)
42 (concat (car list) separator (join-trailing (cdr list) separator))))
44 (defun integer-to-string (x)
49 (push (mod x 10) digits)
50 (setq x (truncate x 10)))
51 (join (mapcar (lambda (d) (string (char "0123456789" d)))
57 ;;; It is a basic Lisp reader. It does not use advanced stuff
58 ;;; intentionally, because we want to use it to bootstrap a simple
59 ;;; Lisp. The main entry point is the function `ls-read', which
60 ;;; accepts a strings as argument and return the Lisp expression.
61 (defun make-string-stream (string)
64 (defun %peek-char (stream)
66 (peek-char nil stream nil)
67 (and (< (cdr stream) (length (car stream)))
68 (char (car stream) (cdr stream)))))
70 (defun %read-char (stream)
72 (read-char stream nil)
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)))
156 (let ((feature (read-until stream #'terminalp)))
158 ((string= feature "common-lisp")
159 (ls-read stream);ignore
161 ((string= feature "lispstrack")
164 (error "Unknown reader form.")))))))
166 (let ((string (read-until stream #'terminalp)))
167 (if (every #'digit-char-p string)
168 (parse-integer string)
169 (intern (string-upcase string))))))))
171 (defun ls-read-from-string (string)
172 (ls-read (make-string-stream string)))
178 (defun make-var-binding (symbol)
179 (cons symbol (concat "v" (integer-to-string (incf counter))))))
182 (defun make-func-binding (symbol)
183 (cons symbol (concat "f" (integer-to-string (incf counter))))))
185 (defvar *compilations* nil)
187 (defun ls-compile-block (sexps env fenv)
188 (join-trailing (mapcar (lambda (x)
189 (ls-compile x env fenv))
194 (defun extend-env (args env)
195 (append (mapcar #'make-var-binding args) env))
197 (defparameter *env* '())
198 (defparameter *fenv* '())
200 (defun lookup (symbol env)
201 (let ((binding (assoc symbol env)))
202 (and binding (cdr binding))))
204 (defun lookup-variable (symbol env)
205 (or (lookup symbol env)
206 (lookup symbol *env*)
207 (error "Undefined variable `~a'" symbol)))
209 (defun lookup-function (symbol env)
210 (or (lookup symbol env)
211 (lookup symbol *fenv*)
212 (error "Undefined function `~a'" symbol)))
214 (defmacro define-compilation (name args &body body)
215 ;; Creates a new primitive `name' with parameters args and
216 ;; @body. The body can access to the local environment through the
218 `(push (list ',name (lambda (env fenv ,@args) ,@body))
221 (defvar *toplevel-compilations*)
223 (define-compilation if (condition true false)
225 (ls-compile condition env fenv) " == undefined"
227 (ls-compile true env fenv)
229 (ls-compile false env fenv)
232 ;;; Return the required args of a lambda list
233 (defun lambda-list-required-argument (lambda-list)
234 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
236 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
238 (defun lambda-list-rest-argument (lambda-list)
239 (second (member '&rest lambda-list)))
241 (define-compilation lambda (lambda-list &rest body)
242 (let ((required-arguments (lambda-list-required-argument lambda-list))
243 (rest-argument (lambda-list-rest-argument lambda-list)))
244 (let ((new-env (extend-env (append (if rest-argument (list rest-argument))
247 (concat "(function ("
248 (join (mapcar (lambda (x) (lookup-variable x new-env))
254 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
255 "for (var i = arguments.length-1; i>="
256 (integer-to-string (length required-arguments))
258 (lookup-variable rest-argument new-env) " = "
259 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
262 (concat (ls-compile-block (butlast body) new-env fenv)
263 "return " (ls-compile (car (last body)) new-env fenv) ";")
267 (define-compilation fsetq (var val)
268 (concat (lookup-function var fenv)
270 (ls-compile val env fenv)))
272 (define-compilation setq (var val)
273 (concat (lookup-variable var env)
275 (ls-compile val env fenv)))
280 (defun literal->js (sexp)
282 ((null sexp) "unspecified")
283 ((integerp sexp) (integer-to-string sexp))
284 ((stringp sexp) (concat "\"" sexp "\""))
285 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
286 ((consp sexp) (concat "{car: "
287 (literal->js (car sexp))
289 (literal->js (cdr sexp)) "}"))))
292 (defun literal (form)
295 (let ((var (concat "l" (integer-to-string (incf counter)))))
296 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
299 (define-compilation quote (sexp)
302 (define-compilation debug (form)
303 (concat "console.log(" (ls-compile form env fenv) ")"))
305 (define-compilation while (pred &rest body)
306 (concat "(function(){ while("
307 (ls-compile pred env fenv)
309 (ls-compile-block body env fenv)
312 (define-compilation function (x)
314 ((and (listp x) (eq (car x) 'lambda))
315 (ls-compile x env fenv))
317 (lookup-function x fenv))))
320 (defmacro eval-when-compile (&body body)
321 `(eval-when (:compile-toplevel :load-toplevel :execute)
324 (define-compilation eval-when-compile (&rest body)
325 (eval (cons 'progn body))
328 (defmacro define-transformation (name args form)
329 `(define-compilation ,name ,args
330 (ls-compile ,form env fenv)))
332 (define-transformation progn (&rest body)
333 `((lambda () ,@body)))
335 (define-transformation let (bindings &rest body)
336 `((lambda ,(mapcar 'car bindings) ,@body)
337 ,@(mapcar 'cadr bindings)))
339 ;;; A little backquote implementation without optimizations of any
340 ;;; kind for lispstrack.
341 (defun backquote-expand-1 (form)
347 ((eq (car form) 'unquote)
349 ((eq (car form) 'backquote)
350 (backquote-expand-1 (backquote-expand-1 (cadr form))))
355 ((and (listp s) (eq (car s) 'unquote))
356 (list 'list (cadr s)))
357 ((and (listp s) (eq (car s) 'unquote-splicing))
360 (list 'list (backquote-expand-1 s)))))
363 (defun backquote-expand (form)
364 (if (and (listp form) (eq (car form) 'backquote))
365 (backquote-expand-1 (cadr form))
368 (defmacro backquote (form)
369 (backquote-expand-1 form))
371 (define-transformation backquote (form)
372 (backquote-expand-1 form))
376 (define-compilation + (x y)
377 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
379 (define-compilation - (x y)
380 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
382 (define-compilation * (x y)
383 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
385 (define-compilation / (x y)
386 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
388 (define-compilation = (x y)
389 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
391 (define-compilation mod (x y)
392 (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
394 (define-compilation floor (x)
395 (concat "(Math.floor(" (ls-compile x env fenv) "))"))
397 (define-compilation null (x)
398 (concat "(" (ls-compile x env fenv) "== undefined)"))
400 (define-compilation cons (x y)
401 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
403 (define-compilation car (x)
404 (concat "(" (ls-compile x env fenv) ").car"))
406 (define-compilation cdr (x)
407 (concat "(" (ls-compile x env fenv) ").cdr"))
409 (define-compilation make-symbol (name)
410 (concat "{name: " (ls-compile name env fenv) "}"))
412 (define-compilation symbol-name (x)
413 (concat "(" (ls-compile x env fenv) ").name"))
415 (define-compilation eq (x y)
416 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
418 (define-compilation string (x)
419 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
421 (define-compilation char (string index)
423 (ls-compile string env fenv)
425 (ls-compile index env fenv)
428 (define-compilation concat-two (string1 string2)
430 (ls-compile string1 env fenv)
432 (ls-compile string2 env fenv)
435 (define-compilation funcall (func &rest args)
437 (ls-compile func env fenv)
439 (join (mapcar (lambda (x)
440 (ls-compile x env fenv))
445 (define-compilation new ()
448 (define-compilation get (object key)
449 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
451 (define-compilation set (object key value)
453 (ls-compile object env fenv)
455 (ls-compile key env fenv) "]"
456 " = " (ls-compile value env fenv) ")"))
459 (defun %compile-defvar (name)
460 (unless (lookup name *env*)
461 (push (make-var-binding name) *env*)
462 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*)))
464 (defun %compile-defun (name)
465 (unless (lookup name *fenv*)
466 (push (make-func-binding name) *fenv*)
467 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*)))
469 (defun %compile-defmacro (name lambda)
470 (push (cons name (cons 'macro lambda)) *fenv*))
472 (defun ls-macroexpand-1 (form &optional env fenv)
473 (let ((function (cdr (assoc (car form) *fenv*))))
474 (if (and (listp function) (eq (car function) 'macro))
475 (apply (eval (cdr function)) (cdr form))
478 (defun compile-funcall (function args env fenv)
481 (concat (lookup-function function fenv)
483 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
486 ((and (listp function) (eq (car function) 'lambda))
487 (concat "(" (ls-compile function env fenv) ")("
488 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
492 (error "Invalid function designator ~a." function))))
494 (defun ls-compile (sexp &optional env fenv)
496 ((symbolp sexp) (lookup-variable sexp env))
497 ((integerp sexp) (integer-to-string sexp))
498 ((stringp sexp) (concat "\"" sexp "\""))
500 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
501 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
503 (apply compiler-func env fenv (cdr sexp))
504 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
506 (defun ls-compile-toplevel (sexp)
507 (setq *toplevel-compilations* nil)
508 (let ((code (ls-compile sexp)))
510 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
511 *toplevel-compilations*)
514 (setq *toplevel-compilations* nil))))
518 (defun ls-compile-file (filename output)
519 (with-open-file (in filename)
520 (with-open-file (out output :direction :output :if-exists :supersede)
524 for compilation = (ls-compile-toplevel x)
525 when compilation do (write-line (concat compilation "; ") out)))))
527 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))