2 (defun !reduce (func list initial)
7 (funcall func initial (car list)))))
12 (defmacro while (condition &body body)
20 ;;; simplify me, please
21 (defun concat (&rest strs)
22 (!reduce (lambda (s1 s2) (concatenate 'string s1 s2))
26 ;;; Concatenate a list of strings, with a separator
27 (defun join (list separator)
36 (join (cdr list) separator)))))
38 (defun join-trailing (list separator)
41 (concat (car list) separator (join-trailing (cdr list) separator))))
43 (defun integer-to-string (x)
48 (push (mod x 10) digits)
49 (setq x (truncate x 10)))
50 (join (mapcar (lambda (d) (string (char "0123456789" d)))
56 ;;; It is a basic Lisp reader. It does not use advanced stuff
57 ;;; intentionally, because we want to use it to bootstrap a simple
58 ;;; Lisp. The main entry point is the function `ls-read', which
59 ;;; accepts a strings as argument and return the Lisp expression.
60 (defun make-string-stream (string)
63 (defun %peek-char (stream)
65 (peek-char nil stream nil)
66 (and (< (cdr stream) (length (car stream)))
67 (char (car stream) (cdr stream)))))
69 (defun %read-char (stream)
71 (read-char stream nil)
72 (and (< (cdr stream) (length (car stream)))
73 (prog1 (char (car stream) (cdr stream))
74 (incf (cdr stream))))))
76 (defun whitespacep (ch)
77 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
79 (defun skip-whitespaces (stream)
81 (setq ch (%peek-char stream))
82 (while (and ch (whitespacep ch))
84 (setq ch (%peek-char stream)))))
87 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
89 (defun read-until (stream func)
92 (setq ch (%peek-char stream))
93 (while (not (funcall func ch))
94 (setq string (concat string (string ch)))
96 (setq ch (%peek-char stream)))
99 (defun skip-whitespaces-and-comments (stream)
101 (skip-whitespaces stream)
102 (setq ch (%peek-char stream))
103 (while (and ch (eql ch #\;))
104 (read-until stream (lambda (x) (eql x #\newline)))
105 (skip-whitespaces stream)
106 (setq ch (%peek-char stream)))))
108 (defun %read-list (stream)
109 (skip-whitespaces-and-comments stream)
110 (let ((ch (%peek-char stream)))
117 (skip-whitespaces-and-comments stream)
118 (prog1 (ls-read stream)
119 (unless (char= (%read-char stream) #\))
120 (error "')' was expected."))))
122 (cons (ls-read stream) (%read-list stream))))))
124 (defvar *eof* (make-symbol "EOF"))
125 (defun ls-read (stream)
126 (skip-whitespaces-and-comments stream)
127 (let ((ch (%peek-char stream)))
136 (list 'quote (ls-read stream)))
139 (list 'backquote (ls-read stream)))
142 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
143 (%read-char stream)))
146 (if (eql (%peek-char stream) #\@)
147 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
148 (list 'unquote (ls-read stream))))
151 (ecase (%read-char stream)
153 (list 'function (ls-read stream)))
155 (let ((feature (read-until stream #'terminalp)))
157 ((string= feature "common-lisp")
158 (ls-read stream);ignore
160 ((string= feature "lispstrack")
163 (error "Unknown reader form.")))))))
165 (let ((string (read-until stream #'terminalp)))
166 (if (every #'digit-char-p string)
167 (parse-integer string)
168 (intern (string-upcase string))))))))
170 (defun ls-read-from-string (string)
171 (ls-read (make-string-stream string)))
177 (defun make-var-binding (symbol)
178 (cons symbol (concat "v" (integer-to-string (incf counter))))))
181 (defun make-func-binding (symbol)
182 (cons symbol (concat "f" (integer-to-string (incf counter))))))
184 (defvar *compilations* nil)
186 (defun ls-compile-block (sexps env fenv)
187 (join-trailing (mapcar (lambda (x)
188 (ls-compile x env fenv))
193 (defun extend-env (args env)
194 (append (mapcar #'make-var-binding args) env))
196 (defparameter *env* '())
197 (defparameter *fenv* '())
199 (defun ls-lookup (symbol env)
200 (let ((binding (assoc symbol env)))
201 (and binding (cdr binding))))
203 (defun lookup-variable (symbol env)
204 (or (ls-lookup symbol env)
205 (ls-lookup symbol *env*)
206 (error "Undefined variable `~a'" symbol)))
208 (defun lookup-function (symbol env)
209 (or (ls-lookup symbol env)
210 (ls-lookup symbol *fenv*)
211 (error "Undefined function `~a'" symbol)))
213 (defmacro define-compilation (name args &body body)
214 ;; Creates a new primitive `name' with parameters args and
215 ;; @body. The body can access to the local environment through the
217 `(push (list ',name (lambda (env fenv ,@args) ,@body))
220 (defvar *toplevel-compilations*)
222 (define-compilation if (condition true false)
224 (ls-compile condition env fenv)
226 (ls-compile true env fenv)
228 (ls-compile false env fenv)
231 ;;; Return the required args of a lambda list
232 (defun lambda-list-required-argument (lambda-list)
233 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
235 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
237 (defun lambda-list-rest-argument (lambda-list)
238 (second (member '&rest lambda-list)))
240 (define-compilation lambda (lambda-list &rest body)
241 (let ((required-arguments (lambda-list-required-argument lambda-list))
242 (rest-argument (lambda-list-rest-argument lambda-list)))
243 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
244 (concat "(function ("
245 (join (mapcar (lambda (x) (lookup-variable x new-env))
251 (concat "var " (lookup-variable rest-argument new-env)
252 " = arguments.slice("
253 (prin1-to-string (length required-arguments))
257 (concat (ls-compile-block (butlast body) new-env fenv)
258 "return " (ls-compile (car (last body)) new-env fenv) ";")
262 (define-compilation fsetq (var val)
263 (concat (lookup-function var fenv)
265 (ls-compile val env fenv)))
267 (define-compilation setq (var val)
268 (concat (lookup-variable var env)
270 (ls-compile val env fenv)))
275 (defun literal->js (sexp)
277 ((null sexp) "undefined")
278 ((integerp sexp) (integer-to-string sexp))
279 ((stringp sexp) (concat "\"" sexp "\""))
280 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
281 ((consp sexp) (concat "{car: "
282 (literal->js (car sexp))
284 (literal->js (cdr sexp)) "}"))))
287 (defun literal (form)
288 (let ((var (concat "l" (integer-to-string (incf counter)))))
289 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
292 (define-compilation quote (sexp)
295 (define-compilation debug (form)
296 (concat "console.log(" (ls-compile form env fenv) ")"))
298 (define-compilation while (pred &rest body)
299 (concat "(function(){ while("
300 (ls-compile pred env fenv)
302 (ls-compile-block body env fenv)
305 (define-compilation function (x)
307 ((and (listp x) (eq (car x) 'lambda))
308 (ls-compile x env fenv))
310 (lookup-function x fenv))))
313 (defmacro eval-when-compile (&body body)
314 `(eval-when (:compile-toplevel :execute)
317 (defvar *eval-when-compilations*)
318 (define-compilation eval-when-compile (&rest body)
319 (eval (cons 'progn body))
322 (defmacro define-transformation (name args form)
323 `(define-compilation ,name ,args
324 (ls-compile ,form env fenv)))
326 (define-transformation progn (&rest body)
327 `((lambda () ,@body)))
329 (define-transformation let (bindings &rest body)
330 `((lambda ,(mapcar 'car bindings) ,@body)
331 ,@(mapcar 'cadr bindings)))
333 ;;; A little backquote implementation without optimizations of any
334 ;;; kind for lispstrack.
335 (defun backquote-expand-1 (form)
341 ((eq (car form) 'unquote)
343 ((eq (car form) 'backquote)
344 (backquote-expand-1 (backquote-expand-1 (cadr form))))
349 ((and (listp s) (eq (car s) 'unquote))
350 (list 'list (cadr s)))
351 ((and (listp s) (eq (car s) 'unquote-splicing))
354 (list 'list (backquote-expand-1 s)))))
357 (defun backquote-expand (form)
358 (if (and (listp form) (eq (car form) 'backquote))
359 (backquote-expand-1 (cadr form))
362 (defmacro backquote (form)
363 (backquote-expand-1 form))
365 (define-transformation backquote (form)
366 (backquote-expand-1 form))
370 (define-compilation + (x y)
371 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
373 (define-compilation - (x y)
374 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
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 null (x)
386 (concat "(" (ls-compile x env fenv) "== undefined)"))
388 (define-compilation cons (x y)
389 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
391 (define-compilation car (x)
392 (concat "(" (ls-compile x env fenv) ").car"))
394 (define-compilation cdr (x)
395 (concat "(" (ls-compile x env fenv) ").cdr"))
397 (define-compilation symbol-name (x)
398 (concat "(" (ls-compile x env fenv) ").name"))
400 (define-compilation eq (x y)
401 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
403 (define-compilation code-char (x)
404 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
406 (define-compilation funcall (func &rest args)
408 (ls-compile func env fenv)
410 (join (mapcar (lambda (x)
411 (ls-compile x env fenv))
416 (defun %compile-defvar (name)
417 (push (make-var-binding name) *env*)
418 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*))
420 (defun %compile-defun (name)
421 (push (make-func-binding name) *fenv*)
422 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*))
424 (defun %compile-defmacro (name lambda)
425 (push (cons name (cons 'macro lambda)) *fenv*))
427 (defun ls-macroexpand-1 (form &optional env fenv)
428 (let ((function (cdr (assoc (car form) *fenv*))))
429 (if (and (listp function) (eq (car function) 'macro))
430 (apply (eval (cdr function)) (cdr form))
433 (defun compile-funcall (function args env fenv)
436 (concat (lookup-function function fenv)
438 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
441 ((and (listp function) (eq (car function) 'lambda))
442 (concat "(" (ls-compile function env fenv) ")("
443 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
447 (error "Invalid function designator ~a." function))))
449 (defun ls-compile (sexp &optional env fenv)
451 ((symbolp sexp) (lookup-variable sexp env))
452 ((integerp sexp) (integer-to-string sexp))
453 ((stringp sexp) (concat "\"" sexp "\""))
455 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
456 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
458 (apply compiler-func env fenv (cdr sexp))
459 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
461 (defun ls-compile-toplevel (sexp)
462 (setq *toplevel-compilations* nil)
463 (let ((code (ls-compile sexp)))
465 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
466 *toplevel-compilations*)
469 (setq *toplevel-compilations* nil))))
472 (defun ls-compile-file (filename output)
473 (with-open-file (in filename)
474 (with-open-file (out output :direction :output :if-exists :supersede)
478 for compilation = (ls-compile-toplevel x)
479 when compilation do (write-line (concat compilation "; ") out)))))
482 (defun compile-test ()
483 (ls-compile-file "test.lisp" "test.js"))