3 (defmacro while (condition &body body)
8 ;;; simplify me, please
9 (defun concat (&rest strs)
10 (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
14 ;;; Concatenate a list of strings, with a separator
15 (defun join (list separator)
24 (join (cdr list) separator)))))
26 (defun integer-to-string (x)
31 (push (mod x 10) digits)
32 (setq x (truncate x 10)))
33 (join (mapcar (lambda (d) (string (char "0123456789" d)))
39 ;;; It is a basic Lisp reader. It does not use advanced stuff
40 ;;; intentionally, because we want to use it to bootstrap a simple
41 ;;; Lisp. The main entry point is the function `ls-read', which
42 ;;; accepts a strings as argument and return the Lisp expression.
43 (defun make-string-stream (string)
46 (defun %peek-char (stream)
48 (peek-char nil stream nil)
49 (and (< (cdr stream) (length (car stream)))
50 (char (car stream) (cdr stream)))))
52 (defun %read-char (stream)
54 (read-char stream nil)
55 (and (< (cdr stream) (length (car stream)))
56 (prog1 (char (car stream) (cdr stream))
57 (incf (cdr stream))))))
59 (defun whitespacep (ch)
60 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
62 (defun skip-whitespaces (stream)
64 (setq ch (%peek-char stream))
65 (while (and ch (whitespacep ch))
67 (setq ch (%peek-char stream)))))
70 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
72 (defun read-until (stream func)
75 (setq ch (%peek-char stream))
76 (while (not (funcall func ch))
77 (setq string (concat string (string ch)))
79 (setq ch (%peek-char stream)))
82 (defun skip-whitespaces-and-comments (stream)
84 (skip-whitespaces stream)
85 (setq ch (%peek-char stream))
86 (while (and ch (eql ch #\;))
87 (read-until stream (lambda (x) (eql x #\newline)))
88 (skip-whitespaces stream)
89 (setq ch (%peek-char stream)))))
91 (defun %read-list (stream)
92 (skip-whitespaces-and-comments stream)
93 (let ((ch (%peek-char stream)))
100 (skip-whitespaces-and-comments stream)
101 (prog1 (ls-read stream)
102 (unless (char= (%read-char stream) #\))
103 (error "')' was expected."))))
105 (cons (ls-read stream) (%read-list stream))))))
107 (defvar *eof* (make-symbol "EOF"))
108 (defun ls-read (stream)
109 (skip-whitespaces-and-comments stream)
110 (let ((ch (%peek-char stream)))
119 (list 'quote (ls-read stream)))
122 (list 'backquote (ls-read stream)))
125 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
126 (%read-char stream)))
129 (if (eql (%peek-char stream) #\@)
130 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
131 (list 'unquote (ls-read stream))))
134 (ecase (%read-char stream)
136 (list 'function (ls-read stream)))
138 (let ((feature (read-until stream #'terminalp)))
140 ((string= feature "common-lisp")
141 (ls-read stream);ignore
143 ((string= feature "lispstrack")
146 (error "Unknown reader form.")))))))
148 (let ((string (read-until stream #'terminalp)))
149 (if (every #'digit-char-p string)
150 (parse-integer string)
151 (intern (string-upcase string))))))))
153 (defun ls-read-from-string (string)
154 (ls-read (make-string-stream string)))
160 (defun make-var-binding (symbol)
161 (cons symbol (concat "v" (integer-to-string (incf counter))))))
164 (defun make-func-binding (symbol)
165 (cons symbol (concat "f" (integer-to-string (incf counter))))))
167 (defvar *compilations* nil)
169 (defun ls-compile-block (sexps env fenv)
170 (concat (join (mapcar (lambda (x)
171 (concat (ls-compile x env fenv) ";"))
176 (defun extend-env (args env)
177 (append (mapcar #'make-var-binding args) env))
179 (defparameter *env* '())
180 (defparameter *fenv* '())
182 (defun ls-lookup (symbol env)
183 (let ((binding (assoc symbol env)))
184 (and binding (cdr binding))))
186 (defun lookup-variable (symbol env)
187 (or (ls-lookup symbol env)
188 (ls-lookup symbol *env*)
189 (error "Undefined variable `~a'" symbol)))
191 (defun lookup-function (symbol env)
192 (or (ls-lookup symbol env)
193 (ls-lookup symbol *fenv*)
194 (error "Undefined function `~a'" symbol)))
196 (defmacro define-compilation (name args &body body)
197 ;; Creates a new primitive `name' with parameters args and
198 ;; @body. The body can access to the local environment through the
200 `(push (list ',name (lambda (env fenv ,@args) ,@body))
203 (define-compilation if (condition true false)
205 (ls-compile condition env fenv)
207 (ls-compile true env fenv)
209 (ls-compile false env fenv)
212 ;;; Return the required args of a lambda list
213 (defun lambda-list-required-argument (lambda-list)
214 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
216 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
218 (defun lambda-list-rest-argument (lambda-list)
219 (second (member '&rest lambda-list)))
221 (define-compilation lambda (lambda-list &rest body)
222 (let ((required-arguments (lambda-list-required-argument lambda-list))
223 (rest-argument (lambda-list-rest-argument lambda-list)))
224 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
225 (concat "(function ("
226 (join (mapcar (lambda (x) (lookup-variable x new-env))
232 (concat "var " (lookup-variable rest-argument new-env)
233 " = arguments.slice("
234 (prin1-to-string (length required-arguments)) ");
238 (concat (ls-compile-block (butlast body) new-env fenv)
239 "return " (ls-compile (car (last body)) new-env fenv) ";")
243 (define-compilation fsetq (var val)
244 (concat (lookup-function var fenv)
246 (ls-compile val env fenv)))
248 (define-compilation setq (var val)
249 (concat (lookup-variable var env)
251 (ls-compile val env fenv)))
256 (defvar *literals* '())
258 (defun literal->js (sexp)
260 ((null sexp) "undefined")
261 ((integerp sexp) (integer-to-string sexp))
262 ((stringp sexp) (concat "\"" sexp "\""))
263 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
264 ((consp sexp) (concat "{car: "
265 (literal->js (car sexp))
267 (literal->js (cdr sexp)) "}"))))
270 (defun literal (form)
271 (let ((var (concat "l" (integer-to-string (incf counter)))))
272 (push (cons var (literal->js form)) *literals*)
275 (define-compilation quote (sexp)
278 (define-compilation debug (form)
279 (concat "console.log(" (ls-compile form env fenv) ")"))
281 (define-compilation while (pred &rest body)
282 (concat "(function(){ while("
283 (ls-compile pred env fenv)
285 (ls-compile-block body env fenv)
288 (define-compilation function (x)
290 ((and (listp x) (eq (car x) 'lambda))
291 (ls-compile x env fenv))
293 (lookup-function x fenv))))
295 (defmacro eval-when-compile (&body body)
296 `(eval-when (:compile-toplevel :execute)
299 (defvar *eval-when-compilations*)
300 (define-compilation eval-when-compile (&rest body)
301 (setq *eval-when-compilations* "")
302 (eval (cons 'progn body))
303 (if (string= *eval-when-compilations* "")
305 *eval-when-compilations*))
307 (defmacro define-transformation (name args form)
308 `(define-compilation ,name ,args
309 (ls-compile ,form env fenv)))
311 (define-transformation progn (&rest body)
312 `((lambda () ,@body)))
314 (define-transformation let (bindings &rest body)
315 `((lambda ,(mapcar 'car bindings) ,@body)
316 ,@(mapcar 'cadr bindings)))
318 ;;; A little backquote implementation without optimizations of any
319 ;;; kind for lispstrack.
320 (defun backquote-expand-1 (form)
326 ((eq (car form) 'unquote)
328 ((eq (car form) 'backquote)
329 (backquote-expand-1 (backquote-expand-1 (cadr form))))
334 ((and (listp s) (eq (car s) 'unquote))
335 (list 'list (cadr s)))
336 ((and (listp s) (eq (car s) 'unquote-splicing))
339 (list 'list (backquote-expand-1 s)))))
342 (defun backquote-expand (form)
343 (if (and (listp form) (eq (car form) 'backquote))
344 (backquote-expand-1 (cadr form))
347 (define-transformation backquote (form)
348 (backquote-expand-1 form))
352 (define-compilation + (x y)
353 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
355 (define-compilation - (x y)
356 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
358 (define-compilation * (x y)
359 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
361 (define-compilation / (x y)
362 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
364 (define-compilation = (x y)
365 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
367 (define-compilation cons (x y)
368 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
370 (define-compilation car (x)
371 (concat "(" (ls-compile x env fenv) ").car"))
373 (define-compilation cdr (x)
374 (concat "(" (ls-compile x env fenv) ").cdr"))
376 (define-compilation symbol-name (x)
377 (concat "(" (ls-compile x env fenv) ").name"))
379 (define-compilation eq (x y)
380 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
382 (define-compilation code-char (x)
383 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
386 (defmacro with-eval-when-compilation (&body body)
387 `(setq *eval-when-compilations*
388 (concat *eval-when-compilations* (progn ,@body))))
390 (defun %compile-defvar (name)
391 (push (make-var-binding name) *env*)
392 (with-eval-when-compilation
393 (concat "var " (lookup-variable name *env*))))
395 (defun %compile-defun (name)
396 (push (make-func-binding name) *fenv*)
397 (with-eval-when-compilation
398 (concat "var " (lookup-variable name *fenv*))))
400 (defun %compile-defmacro (name lambda)
401 (push (cons name (cons 'macro lambda)) *fenv*))
403 (defun ls-macroexpand-1 (form &optional env fenv)
404 (let ((function (cdr (assoc (car form) *fenv*))))
405 (if (and (listp function) (eq (car function) 'macro))
406 (apply (eval (cdr function)) (cdr form))
409 (defun compile-funcall (function args env fenv)
412 (concat (lookup-function function fenv)
414 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
417 ((and (listp function) (eq (car function) 'lambda))
418 (concat "(" (ls-compile function env fenv) ")("
419 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
423 (error "Invalid function designator ~a." function))))
425 (defun ls-compile (sexp &optional env fenv)
427 ((symbolp sexp) (lookup-variable sexp env))
428 ((integerp sexp) (integer-to-string sexp))
429 ((stringp sexp) (concat "\"" sexp "\""))
431 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
432 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
434 (apply compiler-func env fenv (cdr sexp))
435 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
437 (defun ls-compile-toplevel (sexp)
438 (setq *literals* nil)
439 (let ((code (ls-compile sexp)))
441 (concat (join (mapcar (lambda (lit)
442 (concat "var " (car lit) " = " (cdr lit) ";
447 (setq *literals* nil))))
449 (defun ls-compile-file (filename output)
450 (with-open-file (in filename)
451 (with-open-file (out output :direction :output :if-exists :supersede)
455 for compilation = (ls-compile-toplevel x)
456 when compilation do (write-line (concat compilation "; ") out)))))
460 (defun compile-test ()
461 (ls-compile-file "test.lisp" "test.js"))