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)))
23 (defun concat (&rest strs)
24 (!reduce (lambda (s1 s2) (concat-two s1 s2))
28 ;;; Concatenate a list of strings, with a separator
29 (defun join (list separator)
38 (join (cdr list) separator)))))
40 (defun join-trailing (list separator)
43 (concat (car list) separator (join-trailing (cdr list) separator))))
45 (defun integer-to-string (x)
50 (push (mod x 10) digits)
51 (setq x (truncate x 10)))
52 (join (mapcar (lambda (d) (string (char "0123456789" d)))
58 ;;; It is a basic Lisp reader. It does not use advanced stuff
59 ;;; intentionally, because we want to use it to bootstrap a simple
60 ;;; Lisp. The main entry point is the function `ls-read', which
61 ;;; accepts a strings as argument and return the Lisp expression.
62 (defun make-string-stream (string)
65 (defun %peek-char (stream)
67 (peek-char nil stream nil)
68 (and (< (cdr stream) (length (car stream)))
69 (char (car stream) (cdr stream)))))
71 (defun %read-char (stream)
73 (read-char stream nil)
74 (and (< (cdr stream) (length (car stream)))
75 (prog1 (char (car stream) (cdr stream))
76 (incf (cdr stream))))))
78 (defun whitespacep (ch)
79 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
81 (defun skip-whitespaces (stream)
83 (setq ch (%peek-char stream))
84 (while (and ch (whitespacep ch))
86 (setq ch (%peek-char stream)))))
89 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
91 (defun read-until (stream func)
94 (setq ch (%peek-char stream))
95 (while (not (funcall func ch))
96 (setq string (concat string (string ch)))
98 (setq ch (%peek-char stream)))
101 (defun skip-whitespaces-and-comments (stream)
103 (skip-whitespaces stream)
104 (setq ch (%peek-char stream))
105 (while (and ch (eql ch #\;))
106 (read-until stream (lambda (x) (eql x #\newline)))
107 (skip-whitespaces stream)
108 (setq ch (%peek-char stream)))))
110 (defun %read-list (stream)
111 (skip-whitespaces-and-comments stream)
112 (let ((ch (%peek-char stream)))
119 (skip-whitespaces-and-comments stream)
120 (prog1 (ls-read stream)
121 (unless (char= (%read-char stream) #\))
122 (error "')' was expected."))))
124 (cons (ls-read stream) (%read-list stream))))))
126 (defvar *eof* (make-symbol "EOF"))
127 (defun ls-read (stream)
128 (skip-whitespaces-and-comments stream)
129 (let ((ch (%peek-char stream)))
138 (list 'quote (ls-read stream)))
141 (list 'backquote (ls-read stream)))
144 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
145 (%read-char stream)))
148 (if (eql (%peek-char stream) #\@)
149 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
150 (list 'unquote (ls-read stream))))
153 (ecase (%read-char stream)
155 (list 'function (ls-read stream)))
157 (let ((feature (read-until stream #'terminalp)))
159 ((string= feature "common-lisp")
160 (ls-read stream);ignore
162 ((string= feature "lispstrack")
165 (error "Unknown reader form.")))))))
167 (let ((string (read-until stream #'terminalp)))
168 (if (every #'digit-char-p string)
169 (parse-integer string)
170 (intern (string-upcase string))))))))
172 (defun ls-read-from-string (string)
173 (ls-read (make-string-stream string)))
179 (defun make-var-binding (symbol)
180 (cons symbol (concat "v" (integer-to-string (incf counter))))))
183 (defun make-func-binding (symbol)
184 (cons symbol (concat "f" (integer-to-string (incf counter))))))
186 (defvar *compilations* nil)
188 (defun ls-compile-block (sexps env fenv)
189 (join-trailing (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 ls-lookup (symbol env)
202 (let ((binding (assoc symbol env)))
203 (and binding (cdr binding))))
205 (defun lookup-variable (symbol env)
206 (or (ls-lookup symbol env)
207 (ls-lookup symbol *env*)
208 (error "Undefined variable `~a'" symbol)))
210 (defun lookup-function (symbol env)
211 (or (ls-lookup symbol env)
212 (ls-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 (cons rest-argument required-arguments) env)))
246 (concat "(function ("
247 (join (mapcar (lambda (x) (lookup-variable x new-env))
253 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
254 "for (var i = arguments.length-1; i>="
255 (integer-to-string (length required-arguments))
257 (lookup-variable rest-argument new-env) " = "
258 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
261 (concat (ls-compile-block (butlast body) new-env fenv)
262 "return " (ls-compile (car (last body)) new-env fenv) ";")
266 (define-compilation fsetq (var val)
267 (concat (lookup-function var fenv)
269 (ls-compile val env fenv)))
271 (define-compilation setq (var val)
272 (concat (lookup-variable var env)
274 (ls-compile val env fenv)))
279 (defun literal->js (sexp)
281 ((null sexp) "undefined")
282 ((integerp sexp) (integer-to-string sexp))
283 ((stringp sexp) (concat "\"" sexp "\""))
284 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
285 ((consp sexp) (concat "{car: "
286 (literal->js (car sexp))
288 (literal->js (cdr sexp)) "}"))))
291 (defun literal (form)
292 (let ((var (concat "l" (integer-to-string (incf counter)))))
293 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
296 (define-compilation quote (sexp)
299 (define-compilation debug (form)
300 (concat "console.log(" (ls-compile form env fenv) ")"))
302 (define-compilation while (pred &rest body)
303 (concat "(function(){ while("
304 (ls-compile pred env fenv)
306 (ls-compile-block body env fenv)
309 (define-compilation function (x)
311 ((and (listp x) (eq (car x) 'lambda))
312 (ls-compile x env fenv))
314 (lookup-function x fenv))))
317 (defmacro eval-when-compile (&body body)
318 `(eval-when (:compile-toplevel :execute)
321 (defvar *eval-when-compilations*)
322 (define-compilation eval-when-compile (&rest body)
323 (eval (cons 'progn body))
326 (defmacro define-transformation (name args form)
327 `(define-compilation ,name ,args
328 (ls-compile ,form env fenv)))
330 (define-transformation progn (&rest body)
331 `((lambda () ,@body)))
333 (define-transformation let (bindings &rest body)
334 `((lambda ,(mapcar 'car bindings) ,@body)
335 ,@(mapcar 'cadr bindings)))
337 ;;; A little backquote implementation without optimizations of any
338 ;;; kind for lispstrack.
339 (defun backquote-expand-1 (form)
345 ((eq (car form) 'unquote)
347 ((eq (car form) 'backquote)
348 (backquote-expand-1 (backquote-expand-1 (cadr form))))
353 ((and (listp s) (eq (car s) 'unquote))
354 (list 'list (cadr s)))
355 ((and (listp s) (eq (car s) 'unquote-splicing))
358 (list 'list (backquote-expand-1 s)))))
361 (defun backquote-expand (form)
362 (if (and (listp form) (eq (car form) 'backquote))
363 (backquote-expand-1 (cadr form))
366 (defmacro backquote (form)
367 (backquote-expand-1 form))
369 (define-transformation backquote (form)
370 (backquote-expand-1 form))
374 (define-compilation + (x y)
375 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
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 null (x)
390 (concat "(" (ls-compile x env fenv) "== undefined)"))
392 (define-compilation cons (x y)
393 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
395 (define-compilation car (x)
396 (concat "(" (ls-compile x env fenv) ").car"))
398 (define-compilation cdr (x)
399 (concat "(" (ls-compile x env fenv) ").cdr"))
401 (define-compilation make-symbol (name)
402 (concat "{name: " (ls-compile name env fenv) "}"))
404 (define-compilation symbol-name (x)
405 (concat "(" (ls-compile x env fenv) ").name"))
407 (define-compilation eq (x y)
408 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
410 (define-compilation code-char (x)
411 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
413 (define-compilation char (string index)
415 (ls-compile string env fenv)
417 (ls-compile index env fenv)
420 (define-compilation concat-two (string1 string2)
422 (ls-compile string1 env fenv)
424 (ls-compile string2 env fenv)
427 (define-compilation funcall (func &rest args)
429 (ls-compile func env fenv)
431 (join (mapcar (lambda (x)
432 (ls-compile x env fenv))
437 (define-compilation new ()
440 (define-compilation get (object key)
441 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
443 (define-compilation set (object key value)
445 (ls-compile object env fenv)
447 (ls-compile key env fenv) "]"
448 " = " (ls-compile value env fenv) ")"))
451 (defun %compile-defvar (name)
452 (push (make-var-binding name) *env*)
453 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*))
455 (defun %compile-defun (name)
456 (push (make-func-binding name) *fenv*)
457 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*))
459 (defun %compile-defmacro (name lambda)
460 (push (cons name (cons 'macro lambda)) *fenv*))
462 (defun ls-macroexpand-1 (form &optional env fenv)
463 (let ((function (cdr (assoc (car form) *fenv*))))
464 (if (and (listp function) (eq (car function) 'macro))
465 (apply (eval (cdr function)) (cdr form))
468 (defun compile-funcall (function args env fenv)
471 (concat (lookup-function function fenv)
473 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
476 ((and (listp function) (eq (car function) 'lambda))
477 (concat "(" (ls-compile function env fenv) ")("
478 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
482 (error "Invalid function designator ~a." function))))
484 (defun ls-compile (sexp &optional env fenv)
486 ((symbolp sexp) (lookup-variable sexp env))
487 ((integerp sexp) (integer-to-string sexp))
488 ((stringp sexp) (concat "\"" sexp "\""))
490 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
491 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
493 (apply compiler-func env fenv (cdr sexp))
494 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
496 (defun ls-compile-toplevel (sexp)
497 (setq *toplevel-compilations* nil)
498 (let ((code (ls-compile sexp)))
500 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
501 *toplevel-compilations*)
504 (setq *toplevel-compilations* nil))))
508 (defun ls-compile-file (filename output)
509 (with-open-file (in filename)
510 (with-open-file (out output :direction :output :if-exists :supersede)
514 for compilation = (ls-compile-toplevel x)
515 when compilation do (write-line (concat compilation "; ") out)))))
517 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))