4 (defmacro while (condition &body body)
12 ;;; simplify me, please
13 (defun concat (&rest strs)
14 (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
18 ;;; Concatenate a list of strings, with a separator
19 (defun join (list separator)
28 (join (cdr list) separator)))))
30 (defun join-trailing (list separator)
33 (concat (car list) separator (join-trailing (cdr list) separator))))
35 (defun integer-to-string (x)
40 (push (mod x 10) digits)
41 (setq x (truncate x 10)))
42 (join (mapcar (lambda (d) (string (char "0123456789" d)))
48 ;;; It is a basic Lisp reader. It does not use advanced stuff
49 ;;; intentionally, because we want to use it to bootstrap a simple
50 ;;; Lisp. The main entry point is the function `ls-read', which
51 ;;; accepts a strings as argument and return the Lisp expression.
52 (defun make-string-stream (string)
55 (defun %peek-char (stream)
57 (peek-char nil stream nil)
58 (and (< (cdr stream) (length (car stream)))
59 (char (car stream) (cdr stream)))))
61 (defun %read-char (stream)
63 (read-char stream nil)
64 (and (< (cdr stream) (length (car stream)))
65 (prog1 (char (car stream) (cdr stream))
66 (incf (cdr stream))))))
68 (defun whitespacep (ch)
69 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
71 (defun skip-whitespaces (stream)
73 (setq ch (%peek-char stream))
74 (while (and ch (whitespacep ch))
76 (setq ch (%peek-char stream)))))
79 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
81 (defun read-until (stream func)
84 (setq ch (%peek-char stream))
85 (while (not (funcall func ch))
86 (setq string (concat string (string ch)))
88 (setq ch (%peek-char stream)))
91 (defun skip-whitespaces-and-comments (stream)
93 (skip-whitespaces stream)
94 (setq ch (%peek-char stream))
95 (while (and ch (eql ch #\;))
96 (read-until stream (lambda (x) (eql x #\newline)))
97 (skip-whitespaces stream)
98 (setq ch (%peek-char stream)))))
100 (defun %read-list (stream)
101 (skip-whitespaces-and-comments stream)
102 (let ((ch (%peek-char stream)))
109 (skip-whitespaces-and-comments stream)
110 (prog1 (ls-read stream)
111 (unless (char= (%read-char stream) #\))
112 (error "')' was expected."))))
114 (cons (ls-read stream) (%read-list stream))))))
116 (defvar *eof* (make-symbol "EOF"))
117 (defun ls-read (stream)
118 (skip-whitespaces-and-comments stream)
119 (let ((ch (%peek-char stream)))
128 (list 'quote (ls-read stream)))
131 (list 'backquote (ls-read stream)))
134 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
135 (%read-char stream)))
138 (if (eql (%peek-char stream) #\@)
139 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
140 (list 'unquote (ls-read stream))))
143 (ecase (%read-char stream)
145 (list 'function (ls-read stream)))
147 (let ((feature (read-until stream #'terminalp)))
149 ((string= feature "common-lisp")
150 (ls-read stream);ignore
152 ((string= feature "lispstrack")
155 (error "Unknown reader form.")))))))
157 (let ((string (read-until stream #'terminalp)))
158 (if (every #'digit-char-p string)
159 (parse-integer string)
160 (intern (string-upcase string))))))))
162 (defun ls-read-from-string (string)
163 (ls-read (make-string-stream string)))
169 (defun make-var-binding (symbol)
170 (cons symbol (concat "v" (integer-to-string (incf counter))))))
173 (defun make-func-binding (symbol)
174 (cons symbol (concat "f" (integer-to-string (incf counter))))))
176 (defvar *compilations* nil)
178 (defun ls-compile-block (sexps env fenv)
179 (join-trailing (mapcar (lambda (x)
180 (ls-compile x env fenv))
185 (defun extend-env (args env)
186 (append (mapcar #'make-var-binding args) env))
188 (defparameter *env* '())
189 (defparameter *fenv* '())
191 (defun ls-lookup (symbol env)
192 (let ((binding (assoc symbol env)))
193 (and binding (cdr binding))))
195 (defun lookup-variable (symbol env)
196 (or (ls-lookup symbol env)
197 (ls-lookup symbol *env*)
198 (error "Undefined variable `~a'" symbol)))
200 (defun lookup-function (symbol env)
201 (or (ls-lookup symbol env)
202 (ls-lookup symbol *fenv*)
203 (error "Undefined function `~a'" symbol)))
205 (defmacro define-compilation (name args &body body)
206 ;; Creates a new primitive `name' with parameters args and
207 ;; @body. The body can access to the local environment through the
209 `(push (list ',name (lambda (env fenv ,@args) ,@body))
212 (defvar *toplevel-compilations*)
214 (define-compilation if (condition true false)
216 (ls-compile condition env fenv)
218 (ls-compile true env fenv)
220 (ls-compile false env fenv)
223 ;;; Return the required args of a lambda list
224 (defun lambda-list-required-argument (lambda-list)
225 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
227 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
229 (defun lambda-list-rest-argument (lambda-list)
230 (second (member '&rest lambda-list)))
232 (define-compilation lambda (lambda-list &rest body)
233 (let ((required-arguments (lambda-list-required-argument lambda-list))
234 (rest-argument (lambda-list-rest-argument lambda-list)))
235 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
236 (concat "(function ("
237 (join (mapcar (lambda (x) (lookup-variable x new-env))
243 (concat "var " (lookup-variable rest-argument new-env)
244 " = arguments.slice("
245 (prin1-to-string (length required-arguments))
249 (concat (ls-compile-block (butlast body) new-env fenv)
250 "return " (ls-compile (car (last body)) new-env fenv) ";")
254 (define-compilation fsetq (var val)
255 (concat (lookup-function var fenv)
257 (ls-compile val env fenv)))
259 (define-compilation setq (var val)
260 (concat (lookup-variable var env)
262 (ls-compile val env fenv)))
267 (defun literal->js (sexp)
269 ((null sexp) "undefined")
270 ((integerp sexp) (integer-to-string sexp))
271 ((stringp sexp) (concat "\"" sexp "\""))
272 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
273 ((consp sexp) (concat "{car: "
274 (literal->js (car sexp))
276 (literal->js (cdr sexp)) "}"))))
279 (defun literal (form)
280 (let ((var (concat "l" (integer-to-string (incf counter)))))
281 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
284 (define-compilation quote (sexp)
287 (define-compilation debug (form)
288 (concat "console.log(" (ls-compile form env fenv) ")"))
290 (define-compilation while (pred &rest body)
291 (concat "(function(){ while("
292 (ls-compile pred env fenv)
294 (ls-compile-block body env fenv)
297 (define-compilation function (x)
299 ((and (listp x) (eq (car x) 'lambda))
300 (ls-compile x env fenv))
302 (lookup-function x fenv))))
305 (defmacro eval-when-compile (&body body)
306 `(eval-when (:compile-toplevel :execute)
309 (defvar *eval-when-compilations*)
310 (define-compilation eval-when-compile (&rest body)
311 (eval (cons 'progn body))
314 (defmacro define-transformation (name args form)
315 `(define-compilation ,name ,args
316 (ls-compile ,form env fenv)))
318 (define-transformation progn (&rest body)
319 `((lambda () ,@body)))
321 (define-transformation let (bindings &rest body)
322 `((lambda ,(mapcar 'car bindings) ,@body)
323 ,@(mapcar 'cadr bindings)))
325 ;;; A little backquote implementation without optimizations of any
326 ;;; kind for lispstrack.
327 (defun backquote-expand-1 (form)
333 ((eq (car form) 'unquote)
335 ((eq (car form) 'backquote)
336 (backquote-expand-1 (backquote-expand-1 (cadr form))))
341 ((and (listp s) (eq (car s) 'unquote))
342 (list 'list (cadr s)))
343 ((and (listp s) (eq (car s) 'unquote-splicing))
346 (list 'list (backquote-expand-1 s)))))
349 (defun backquote-expand (form)
350 (if (and (listp form) (eq (car form) 'backquote))
351 (backquote-expand-1 (cadr form))
354 (defmacro backquote (form)
355 (backquote-expand-1 form))
357 (define-transformation backquote (form)
358 (backquote-expand-1 form))
362 (define-compilation + (x y)
363 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
365 (define-compilation - (x y)
366 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
368 (define-compilation * (x y)
369 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
371 (define-compilation / (x y)
372 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
374 (define-compilation = (x y)
375 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
377 (define-compilation null (x)
378 (concat "(" (ls-compile x env fenv) "== undefined)"))
380 (define-compilation cons (x y)
381 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
383 (define-compilation car (x)
384 (concat "(" (ls-compile x env fenv) ").car"))
386 (define-compilation cdr (x)
387 (concat "(" (ls-compile x env fenv) ").cdr"))
391 (define-compilation symbol-name (x)
392 (concat "(" (ls-compile x env fenv) ").name"))
394 (define-compilation eq (x y)
395 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
397 (define-compilation code-char (x)
398 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
400 (defun %compile-defvar (name)
401 (push (make-var-binding name) *env*)
402 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*))
404 (defun %compile-defun (name)
405 (push (make-func-binding name) *fenv*)
406 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*))
408 (defun %compile-defmacro (name lambda)
409 (push (cons name (cons 'macro lambda)) *fenv*))
411 (defun ls-macroexpand-1 (form &optional env fenv)
412 (let ((function (cdr (assoc (car form) *fenv*))))
413 (if (and (listp function) (eq (car function) 'macro))
414 (apply (eval (cdr function)) (cdr form))
417 (defun compile-funcall (function args env fenv)
420 (concat (lookup-function function fenv)
422 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
425 ((and (listp function) (eq (car function) 'lambda))
426 (concat "(" (ls-compile function env fenv) ")("
427 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
431 (error "Invalid function designator ~a." function))))
433 (defun ls-compile (sexp &optional env fenv)
435 ((symbolp sexp) (lookup-variable sexp env))
436 ((integerp sexp) (integer-to-string sexp))
437 ((stringp sexp) (concat "\"" sexp "\""))
439 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
440 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
442 (apply compiler-func env fenv (cdr sexp))
443 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
445 (defun ls-compile-toplevel (sexp)
446 (setq *toplevel-compilations* nil)
447 (let ((code (ls-compile sexp)))
449 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
450 *toplevel-compilations*)
453 (setq *toplevel-compilations* nil))))
456 (defun ls-compile-file (filename output)
457 (with-open-file (in filename)
458 (with-open-file (out output :direction :output :if-exists :supersede)
462 for compilation = (ls-compile-toplevel x)
463 when compilation do (write-line (concat compilation "; ") out)))))
466 (defun compile-test ()
467 (ls-compile-file "test.lisp" "test.js"))