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)
70 (peek-char nil stream nil)
71 (and (< (cdr stream) (length (car stream)))
72 (char (car stream) (cdr stream)))))
74 (defun %read-char (stream)
76 (read-char stream nil)
77 (and (< (cdr stream) (length (car stream)))
78 (prog1 (char (car stream) (cdr stream))
79 (incf (cdr stream))))))
81 (defun whitespacep (ch)
82 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
84 (defun skip-whitespaces (stream)
86 (setq ch (%peek-char stream))
87 (while (and ch (whitespacep ch))
89 (setq ch (%peek-char stream)))))
92 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
94 (defun read-until (stream func)
97 (setq ch (%peek-char stream))
98 (while (not (funcall func ch))
99 (setq string (concat string (string ch)))
101 (setq ch (%peek-char stream)))
104 (defun skip-whitespaces-and-comments (stream)
106 (skip-whitespaces stream)
107 (setq ch (%peek-char stream))
108 (while (and ch (eql ch #\;))
109 (read-until stream (lambda (x) (eql x #\newline)))
110 (skip-whitespaces stream)
111 (setq ch (%peek-char stream)))))
113 (defun %read-list (stream)
114 (skip-whitespaces-and-comments stream)
115 (let ((ch (%peek-char stream)))
122 (skip-whitespaces-and-comments stream)
123 (prog1 (ls-read stream)
124 (unless (char= (%read-char stream) #\))
125 (error "')' was expected."))))
127 (cons (ls-read stream) (%read-list stream))))))
129 (defvar *eof* (make-symbol "EOF"))
130 (defun ls-read (stream)
131 (skip-whitespaces-and-comments stream)
132 (let ((ch (%peek-char stream)))
141 (list 'quote (ls-read stream)))
144 (list 'backquote (ls-read stream)))
147 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
148 (%read-char stream)))
151 (if (eql (%peek-char stream) #\@)
152 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
153 (list 'unquote (ls-read stream))))
156 (ecase (%read-char stream)
158 (list 'function (ls-read stream)))
160 (let ((feature (read-until stream #'terminalp)))
162 ((string= feature "common-lisp")
163 (ls-read stream);ignore
165 ((string= feature "lispstrack")
168 (error "Unknown reader form.")))))))
170 (let ((string (read-until stream #'terminalp)))
171 (if (every #'digit-char-p string)
172 (parse-integer string)
173 (intern (string-upcase string))))))))
175 (defun ls-read-from-string (string)
176 (ls-read (make-string-stream string)))
182 (defun make-var-binding (symbol)
183 (cons symbol (concat "v" (integer-to-string (incf counter))))))
186 (defun make-func-binding (symbol)
187 (cons symbol (concat "f" (integer-to-string (incf counter))))))
189 (defvar *compilations* nil)
191 (defun ls-compile-block (sexps env fenv)
193 (remove nil (mapcar (lambda (x)
194 (ls-compile x env fenv))
199 (defun extend-env (args env)
200 (append (mapcar #'make-var-binding args) env))
202 (defparameter *env* '())
203 (defparameter *fenv* '())
205 (defun lookup (symbol env)
206 (let ((binding (assoc symbol env)))
207 (and binding (cdr binding))))
209 (defun lookup-variable (symbol env)
210 (or (lookup symbol env)
211 (lookup symbol *env*)
212 (error "Undefined variable `~a'" symbol)))
214 (defun lookup-function (symbol env)
215 (or (lookup symbol env)
216 (lookup symbol *fenv*)
217 (error "Undefined function `~a'" symbol)))
219 (defmacro define-compilation (name args &body body)
220 ;; Creates a new primitive `name' with parameters args and
221 ;; @body. The body can access to the local environment through the
223 `(push (list ',name (lambda (env fenv ,@args) ,@body))
226 (defvar *toplevel-compilations*)
228 (define-compilation if (condition true false)
230 (ls-compile condition env fenv)
232 (ls-compile true env fenv)
234 (ls-compile false env fenv)
237 ;;; Return the required args of a lambda list
238 (defun lambda-list-required-argument (lambda-list)
239 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
241 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
243 (defun lambda-list-rest-argument (lambda-list)
244 (second (member '&rest lambda-list)))
246 (define-compilation lambda (lambda-list &rest body)
247 (let ((required-arguments (lambda-list-required-argument lambda-list))
248 (rest-argument (lambda-list-rest-argument lambda-list)))
249 (let ((new-env (extend-env (append (if rest-argument (list rest-argument))
252 (concat "(function ("
253 (join (mapcar (lambda (x) (lookup-variable x new-env))
259 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
260 "for (var i = arguments.length-1; i>="
261 (integer-to-string (length required-arguments))
263 (lookup-variable rest-argument new-env) " = "
264 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
267 (concat (ls-compile-block (butlast body) new-env fenv)
268 "return " (ls-compile (car (last body)) new-env fenv) ";")
272 (define-compilation fsetq (var val)
273 (concat (lookup-function var fenv)
275 (ls-compile val env fenv)))
277 (define-compilation setq (var val)
278 (concat (lookup-variable var env)
280 (ls-compile val env fenv)))
285 (defun literal->js (sexp)
287 ((null sexp) "undefined")
288 ((integerp sexp) (integer-to-string sexp))
289 ((stringp sexp) (concat "\"" sexp "\""))
290 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
291 ((consp sexp) (concat "{car: "
292 (literal->js (car sexp))
294 (literal->js (cdr sexp)) "}"))))
297 (defun literal (form)
300 (let ((var (concat "l" (integer-to-string (incf counter)))))
301 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
304 (define-compilation quote (sexp)
307 (define-compilation debug (form)
308 (concat "console.log(" (ls-compile form env fenv) ")"))
310 (define-compilation while (pred &rest body)
311 (concat "(function(){ while("
312 (ls-compile pred env fenv)
314 (ls-compile-block body env fenv)
317 (define-compilation function (x)
319 ((and (listp x) (eq (car x) 'lambda))
320 (ls-compile x env fenv))
322 (lookup-function x fenv))))
325 (defmacro eval-when-compile (&body body)
326 `(eval-when (:compile-toplevel :load-toplevel :execute)
329 (define-compilation eval-when-compile (&rest body)
330 (eval (cons 'progn body))
333 (defmacro define-transformation (name args form)
334 `(define-compilation ,name ,args
335 (ls-compile ,form env fenv)))
337 (define-transformation progn (&rest body)
338 `((lambda () ,@body)))
340 (define-transformation let (bindings &rest body)
341 `((lambda ,(mapcar 'car bindings) ,@body)
342 ,@(mapcar 'cadr bindings)))
344 ;;; A little backquote implementation without optimizations of any
345 ;;; kind for lispstrack.
346 (defun backquote-expand-1 (form)
352 ((eq (car form) 'unquote)
354 ((eq (car form) 'backquote)
355 (backquote-expand-1 (backquote-expand-1 (cadr form))))
360 ((and (listp s) (eq (car s) 'unquote))
361 (list 'list (cadr s)))
362 ((and (listp s) (eq (car s) 'unquote-splicing))
365 (list 'list (backquote-expand-1 s)))))
368 (defun backquote-expand (form)
369 (if (and (listp form) (eq (car form) 'backquote))
370 (backquote-expand-1 (cadr form))
373 (defmacro backquote (form)
374 (backquote-expand-1 form))
376 (define-transformation backquote (form)
377 (backquote-expand-1 form))
381 (define-compilation + (x y)
382 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
384 (define-compilation - (x y)
385 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
387 (define-compilation * (x y)
388 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
390 (define-compilation / (x y)
391 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
393 (define-compilation = (x y)
394 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
396 (define-compilation mod (x y)
397 (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
399 (define-compilation floor (x)
400 (concat "(Math.floor(" (ls-compile x env fenv) "))"))
402 (define-compilation null (x)
403 (concat "(" (ls-compile x env fenv) "== undefined)"))
405 (define-compilation cons (x y)
406 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
408 (define-compilation car (x)
409 (concat "(" (ls-compile x env fenv) ").car"))
411 (define-compilation cdr (x)
412 (concat "(" (ls-compile x env fenv) ").cdr"))
414 (define-compilation make-symbol (name)
415 (concat "{name: " (ls-compile name env fenv) "}"))
417 (define-compilation symbol-name (x)
418 (concat "(" (ls-compile x env fenv) ").name"))
420 (define-compilation eq (x y)
421 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
423 (define-compilation string (x)
424 (concat "String.fromCharCode(" (ls-compile x env fenv) ")"))
426 (define-compilation char (string index)
428 (ls-compile string env fenv)
430 (ls-compile index env fenv)
433 (define-compilation concat-two (string1 string2)
435 (ls-compile string1 env fenv)
437 (ls-compile string2 env fenv)
440 (define-compilation funcall (func &rest args)
442 (ls-compile func env fenv)
444 (join (mapcar (lambda (x)
445 (ls-compile x env fenv))
450 (define-compilation new ()
453 (define-compilation get (object key)
454 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
456 (define-compilation set (object key value)
458 (ls-compile object env fenv)
460 (ls-compile key env fenv) "]"
461 " = " (ls-compile value env fenv) ")"))
464 (defun %compile-defvar (name)
465 (unless (lookup name *env*)
466 (push (make-var-binding name) *env*)
467 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*)))
469 (defun %compile-defun (name)
470 (unless (lookup name *fenv*)
471 (push (make-func-binding name) *fenv*)
472 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*)))
474 (defun %compile-defmacro (name lambda)
475 (push (cons name (cons 'macro lambda)) *fenv*))
477 (defun ls-macroexpand-1 (form &optional env fenv)
478 (let ((function (cdr (assoc (car form) *fenv*))))
479 (if (and (listp function) (eq (car function) 'macro))
480 (apply (eval (cdr function)) (cdr form))
483 (defun compile-funcall (function args env fenv)
486 (concat (lookup-function function fenv)
488 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
491 ((and (listp function) (eq (car function) 'lambda))
492 (concat "(" (ls-compile function env fenv) ")("
493 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
497 (error "Invalid function designator ~a." function))))
499 (defun ls-compile (sexp &optional env fenv)
501 ((symbolp sexp) (lookup-variable sexp env))
502 ((integerp sexp) (integer-to-string sexp))
503 ((stringp sexp) (concat "\"" sexp "\""))
505 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
506 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
508 (apply compiler-func env fenv (cdr sexp))
509 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
511 (defun ls-compile-toplevel (sexp)
512 (setq *toplevel-compilations* nil)
513 (let ((code (ls-compile sexp)))
515 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
516 *toplevel-compilations*)
519 (setq *toplevel-compilations* nil))))
523 (defun ls-compile-file (filename output)
524 (setq *env* nil *fenv* nil)
525 (with-open-file (in filename)
526 (with-open-file (out output :direction :output :if-exists :supersede)
530 for compilation = (ls-compile-toplevel x)
531 when (plusp (length compilation))
532 do (write-line (concat compilation "; ") out)))))
534 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))