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)
189 (remove nil (mapcar (lambda (x)
190 (ls-compile x env fenv))
195 (defun extend-env (args env)
196 (append (mapcar #'make-var-binding args) env))
198 (defparameter *env* '())
199 (defparameter *fenv* '())
201 (defun lookup (symbol env)
202 (let ((binding (assoc symbol env)))
203 (and binding (cdr binding))))
205 (defun lookup-variable (symbol env)
206 (or (lookup symbol env)
207 (lookup symbol *env*)
208 (error "Undefined variable `~a'" symbol)))
210 (defun lookup-function (symbol env)
211 (or (lookup symbol env)
212 (lookup symbol *fenv*)
213 (error "Undefined function `~a'" symbol)))
215 (defmacro define-compilation (name args &body body)
216 ;; Creates a new primitive `name' with parameters args and
217 ;; @body. The body can access to the local environment through the
219 `(push (list ',name (lambda (env fenv ,@args) ,@body))
222 (defvar *toplevel-compilations*)
224 (define-compilation if (condition true false)
226 (ls-compile condition env fenv)
228 (ls-compile true env fenv)
230 (ls-compile false env fenv)
233 ;;; Return the required args of a lambda list
234 (defun lambda-list-required-argument (lambda-list)
235 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
237 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
239 (defun lambda-list-rest-argument (lambda-list)
240 (second (member '&rest lambda-list)))
242 (define-compilation lambda (lambda-list &rest body)
243 (let ((required-arguments (lambda-list-required-argument lambda-list))
244 (rest-argument (lambda-list-rest-argument lambda-list)))
245 (let ((new-env (extend-env (append (if rest-argument (list rest-argument))
248 (concat "(function ("
249 (join (mapcar (lambda (x) (lookup-variable x new-env))
255 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
256 "for (var i = arguments.length-1; i>="
257 (integer-to-string (length required-arguments))
259 (lookup-variable rest-argument new-env) " = "
260 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
263 (concat (ls-compile-block (butlast body) new-env fenv)
264 "return " (ls-compile (car (last body)) new-env fenv) ";")
268 (define-compilation fsetq (var val)
269 (concat (lookup-function var fenv)
271 (ls-compile val env fenv)))
273 (define-compilation setq (var val)
274 (concat (lookup-variable var env)
276 (ls-compile val env fenv)))
281 (defun literal->js (sexp)
283 ((null sexp) "unspecified")
284 ((integerp sexp) (integer-to-string sexp))
285 ((stringp sexp) (concat "\"" sexp "\""))
286 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
287 ((consp sexp) (concat "{car: "
288 (literal->js (car sexp))
290 (literal->js (cdr sexp)) "}"))))
293 (defun literal (form)
296 (let ((var (concat "l" (integer-to-string (incf counter)))))
297 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
300 (define-compilation quote (sexp)
303 (define-compilation debug (form)
304 (concat "console.log(" (ls-compile form env fenv) ")"))
306 (define-compilation while (pred &rest body)
307 (concat "(function(){ while("
308 (ls-compile pred env fenv)
310 (ls-compile-block body env fenv)
313 (define-compilation function (x)
315 ((and (listp x) (eq (car x) 'lambda))
316 (ls-compile x env fenv))
318 (lookup-function x fenv))))
321 (defmacro eval-when-compile (&body body)
322 `(eval-when (:compile-toplevel :load-toplevel :execute)
325 (define-compilation eval-when-compile (&rest body)
326 (eval (cons 'progn body))
329 (defmacro define-transformation (name args form)
330 `(define-compilation ,name ,args
331 (ls-compile ,form env fenv)))
333 (define-transformation progn (&rest body)
334 `((lambda () ,@body)))
336 (define-transformation let (bindings &rest body)
337 `((lambda ,(mapcar 'car bindings) ,@body)
338 ,@(mapcar 'cadr bindings)))
340 ;;; A little backquote implementation without optimizations of any
341 ;;; kind for lispstrack.
342 (defun backquote-expand-1 (form)
348 ((eq (car form) 'unquote)
350 ((eq (car form) 'backquote)
351 (backquote-expand-1 (backquote-expand-1 (cadr form))))
356 ((and (listp s) (eq (car s) 'unquote))
357 (list 'list (cadr s)))
358 ((and (listp s) (eq (car s) 'unquote-splicing))
361 (list 'list (backquote-expand-1 s)))))
364 (defun backquote-expand (form)
365 (if (and (listp form) (eq (car form) 'backquote))
366 (backquote-expand-1 (cadr form))
369 (defmacro backquote (form)
370 (backquote-expand-1 form))
372 (define-transformation backquote (form)
373 (backquote-expand-1 form))
377 (define-compilation + (x y)
378 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
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 mod (x y)
393 (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
395 (define-compilation floor (x)
396 (concat "(Math.floor(" (ls-compile x env fenv) "))"))
398 (define-compilation null (x)
399 (concat "(" (ls-compile x env fenv) "== undefined)"))
401 (define-compilation cons (x y)
402 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
404 (define-compilation car (x)
405 (concat "(" (ls-compile x env fenv) ").car"))
407 (define-compilation cdr (x)
408 (concat "(" (ls-compile x env fenv) ").cdr"))
410 (define-compilation make-symbol (name)
411 (concat "{name: " (ls-compile name env fenv) "}"))
413 (define-compilation symbol-name (x)
414 (concat "(" (ls-compile x env fenv) ").name"))
416 (define-compilation eq (x y)
417 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
419 (define-compilation string (x)
420 (concat "String.fromCharCode(" (ls-compile x env fenv) ")"))
422 (define-compilation char (string index)
424 (ls-compile string env fenv)
426 (ls-compile index env fenv)
429 (define-compilation concat-two (string1 string2)
431 (ls-compile string1 env fenv)
433 (ls-compile string2 env fenv)
436 (define-compilation funcall (func &rest args)
438 (ls-compile func env fenv)
440 (join (mapcar (lambda (x)
441 (ls-compile x env fenv))
446 (define-compilation new ()
449 (define-compilation get (object key)
450 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
452 (define-compilation set (object key value)
454 (ls-compile object env fenv)
456 (ls-compile key env fenv) "]"
457 " = " (ls-compile value env fenv) ")"))
460 (defun %compile-defvar (name)
461 (unless (lookup name *env*)
462 (push (make-var-binding name) *env*)
463 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*)))
465 (defun %compile-defun (name)
466 (unless (lookup name *fenv*)
467 (push (make-func-binding name) *fenv*)
468 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*)))
470 (defun %compile-defmacro (name lambda)
471 (push (cons name (cons 'macro lambda)) *fenv*))
473 (defun ls-macroexpand-1 (form &optional env fenv)
474 (let ((function (cdr (assoc (car form) *fenv*))))
475 (if (and (listp function) (eq (car function) 'macro))
476 (apply (eval (cdr function)) (cdr form))
479 (defun compile-funcall (function args env fenv)
482 (concat (lookup-function function fenv)
484 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
487 ((and (listp function) (eq (car function) 'lambda))
488 (concat "(" (ls-compile function env fenv) ")("
489 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
493 (error "Invalid function designator ~a." function))))
495 (defun ls-compile (sexp &optional env fenv)
497 ((symbolp sexp) (lookup-variable sexp env))
498 ((integerp sexp) (integer-to-string sexp))
499 ((stringp sexp) (concat "\"" sexp "\""))
501 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
502 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
504 (apply compiler-func env fenv (cdr sexp))
505 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
507 (defun ls-compile-toplevel (sexp)
508 (setq *toplevel-compilations* nil)
509 (let ((code (ls-compile sexp)))
511 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
512 *toplevel-compilations*)
515 (setq *toplevel-compilations* nil))))
519 (defun ls-compile-file (filename output)
520 (setq *env* nil *fenv* nil)
521 (with-open-file (in filename)
522 (with-open-file (out output :direction :output :if-exists :supersede)
526 for compilation = (ls-compile-toplevel x)
527 when (plusp (length compilation))
528 do (write-line (concat compilation "; ") out)))))
530 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))