4 (defmacro while (condition &body body)
9 ;;; simplify me, please
10 (defun concat (&rest strs)
11 (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
15 ;;; Concatenate a list of strings, with a separator
16 (defun join (list separator)
25 (join (cdr list) separator)))))
27 (defun integer-to-string (x)
32 (push (mod x 10) digits)
33 (setq x (truncate x 10)))
34 (join (mapcar (lambda (d) (string (char "0123456789" d)))
40 ;;; It is a basic Lisp reader. It does not use advanced stuff
41 ;;; intentionally, because we want to use it to bootstrap a simple
42 ;;; Lisp. The main entry point is the function `ls-read', which
43 ;;; accepts a strings as argument and return the Lisp expression.
44 (defun make-string-stream (string)
47 (defun %peek-char (stream)
49 (peek-char nil stream nil)
50 (and (< (cdr stream) (length (car stream)))
51 (char (car stream) (cdr stream)))))
53 (defun %read-char (stream)
55 (read-char stream nil)
56 (and (< (cdr stream) (length (car stream)))
57 (prog1 (char (car stream) (cdr stream))
58 (incf (cdr stream))))))
60 (defun whitespacep (ch)
61 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
63 (defun skip-whitespaces (stream)
65 (setq ch (%peek-char stream))
66 (while (and ch (whitespacep ch))
68 (setq ch (%peek-char stream)))))
71 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
73 (defun read-until (stream func)
76 (setq ch (%peek-char stream))
77 (while (not (funcall func ch))
78 (setq string (concat string (string ch)))
80 (setq ch (%peek-char stream)))
83 (defun skip-whitespaces-and-comments (stream)
85 (skip-whitespaces stream)
86 (setq ch (%peek-char stream))
87 (while (and ch (eql ch #\;))
88 (read-until stream (lambda (x) (eql x #\newline)))
89 (skip-whitespaces stream)
90 (setq ch (%peek-char stream)))))
92 (defun %read-list (stream)
93 (skip-whitespaces-and-comments stream)
94 (let ((ch (%peek-char stream)))
101 (skip-whitespaces-and-comments stream)
102 (prog1 (ls-read stream)
103 (unless (char= (%read-char stream) #\))
104 (error "')' was expected."))))
106 (cons (ls-read stream) (%read-list stream))))))
108 (defvar *eof* (make-symbol "EOF"))
109 (defun ls-read (stream)
110 (skip-whitespaces-and-comments stream)
111 (let ((ch (%peek-char stream)))
120 (list 'quote (ls-read stream)))
123 (list 'backquote (ls-read stream)))
126 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
127 (%read-char stream)))
130 (if (eql (%peek-char stream) #\@)
131 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
132 (list 'unquote (ls-read stream))))
135 (ecase (%read-char stream)
137 (list 'function (ls-read stream)))
139 (let ((feature (read-until stream #'terminalp)))
141 ((string= feature "common-lisp")
142 (ls-read stream);ignore
144 ((string= feature "lispstrack")
147 (error "Unknown reader form.")))))))
149 (let ((string (read-until stream #'terminalp)))
150 (if (every #'digit-char-p string)
151 (parse-integer string)
152 (intern (string-upcase string))))))))
154 (defun ls-read-from-string (string)
155 (ls-read (make-string-stream string)))
161 (defun make-var-binding (symbol)
162 (cons symbol (concat "v" (integer-to-string (incf counter))))))
165 (defun make-func-binding (symbol)
166 (cons symbol (concat "f" (integer-to-string (incf counter))))))
168 (defvar *compilations* nil)
170 (defun ls-compile-block (sexps env fenv)
171 (concat (join (mapcar (lambda (x)
172 (concat (ls-compile x env fenv) ";"))
177 (defun extend-env (args env)
178 (append (mapcar #'make-var-binding args) env))
180 (defparameter *env* '())
181 (defparameter *fenv* '())
183 (defun ls-lookup (symbol env)
184 (let ((binding (assoc symbol env)))
185 (and binding (cdr binding))))
187 (defun lookup-variable (symbol env)
188 (or (ls-lookup symbol env)
189 (ls-lookup symbol *env*)
190 (error "Undefined variable `~a'" symbol)))
192 (defun lookup-function (symbol env)
193 (or (ls-lookup symbol env)
194 (ls-lookup symbol *fenv*)
195 (error "Undefined function `~a'" symbol)))
197 (defmacro define-compilation (name args &body body)
198 ;; Creates a new primitive `name' with parameters args and
199 ;; @body. The body can access to the local environment through the
201 `(push (list ',name (lambda (env fenv ,@args) ,@body))
204 (define-compilation if (condition true false)
206 (ls-compile condition env fenv)
208 (ls-compile true env fenv)
210 (ls-compile false env fenv)
213 ;;; Return the required args of a lambda list
214 (defun lambda-list-required-argument (lambda-list)
215 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
217 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
219 (defun lambda-list-rest-argument (lambda-list)
220 (second (member '&rest lambda-list)))
222 (define-compilation lambda (lambda-list &rest body)
223 (let ((required-arguments (lambda-list-required-argument lambda-list))
224 (rest-argument (lambda-list-rest-argument lambda-list)))
225 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
226 (concat "(function ("
227 (join (mapcar (lambda (x) (lookup-variable x new-env))
233 (concat "var " (lookup-variable rest-argument new-env)
234 " = arguments.slice("
235 (prin1-to-string (length required-arguments)) ");
239 (concat (ls-compile-block (butlast body) new-env fenv)
240 "return " (ls-compile (car (last body)) new-env fenv) ";")
244 (define-compilation fsetq (var val)
245 (concat (lookup-function var fenv)
247 (ls-compile val env fenv)))
249 (define-compilation setq (var val)
250 (concat (lookup-variable var env)
252 (ls-compile val env fenv)))
257 (defvar *literals* '())
259 (defun literal->js (sexp)
261 ((null sexp) "undefined")
262 ((integerp sexp) (integer-to-string sexp))
263 ((stringp sexp) (concat "\"" sexp "\""))
264 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
265 ((consp sexp) (concat "{car: "
266 (literal->js (car sexp))
268 (literal->js (cdr sexp)) "}"))))
271 (defun literal (form)
272 (let ((var (concat "l" (integer-to-string (incf counter)))))
273 (push (cons var (literal->js form)) *literals*)
276 (define-compilation quote (sexp)
279 (define-compilation debug (form)
280 (concat "console.log(" (ls-compile form env fenv) ")"))
282 (define-compilation while (pred &rest body)
283 (concat "(function(){ while("
284 (ls-compile pred env fenv)
286 (ls-compile-block body env fenv)
289 (define-compilation function (x)
291 ((and (listp x) (eq (car x) 'lambda))
292 (ls-compile x env fenv))
294 (lookup-function x fenv))))
296 (defmacro eval-when-compile (&body body)
297 `(eval-when (:compile-toplevel :execute)
300 (defvar *eval-when-compilations*)
301 (define-compilation eval-when-compile (&rest body)
302 (setq *eval-when-compilations* "")
303 (eval (cons 'progn body))
304 (if (string= *eval-when-compilations* "")
306 *eval-when-compilations*))
308 (defmacro define-transformation (name args form)
309 `(define-compilation ,name ,args
310 (ls-compile ,form env fenv)))
312 (define-transformation progn (&rest body)
313 `((lambda () ,@body)))
315 (define-transformation let (bindings &rest body)
316 `((lambda ,(mapcar 'car bindings) ,@body)
317 ,@(mapcar 'cadr bindings)))
319 ;;; A little backquote implementation without optimizations of any
320 ;;; kind for lispstrack.
321 (defun backquote-expand-1 (form)
327 ((eq (car form) 'unquote)
329 ((eq (car form) 'backquote)
330 (backquote-expand-1 (backquote-expand-1 (cadr form))))
335 ((and (listp s) (eq (car s) 'unquote))
336 (list 'list (cadr s)))
337 ((and (listp s) (eq (car s) 'unquote-splicing))
340 (list 'list (backquote-expand-1 s)))))
343 (defun backquote-expand (form)
344 (if (and (listp form) (eq (car form) 'backquote))
345 (backquote-expand-1 (cadr form))
348 (define-transformation backquote (form)
349 (backquote-expand-1 form))
353 (define-compilation + (x y)
354 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
356 (define-compilation - (x y)
357 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
359 (define-compilation * (x y)
360 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
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 cons (x y)
369 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
371 (define-compilation car (x)
372 (concat "(" (ls-compile x env fenv) ").car"))
374 (define-compilation cdr (x)
375 (concat "(" (ls-compile x env fenv) ").cdr"))
377 (define-compilation symbol-name (x)
378 (concat "(" (ls-compile x env fenv) ").name"))
380 (define-compilation eq (x y)
381 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
383 (define-compilation code-char (x)
384 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
387 (defmacro with-eval-when-compilation (&body body)
388 `(setq *eval-when-compilations*
389 (concat *eval-when-compilations* (progn ,@body))))
391 (defun %compile-defvar (name)
392 (push (make-var-binding name) *env*)
393 (with-eval-when-compilation
394 (concat "var " (lookup-variable name *env*))))
396 (defun %compile-defun (name)
397 (push (make-func-binding name) *fenv*)
398 (with-eval-when-compilation
399 (concat "var " (lookup-variable name *fenv*))))
401 (defun %compile-defmacro (name lambda)
402 (push (cons name (cons 'macro lambda)) *fenv*))
404 (defun ls-macroexpand-1 (form &optional env fenv)
405 (let ((function (cdr (assoc (car form) *fenv*))))
406 (if (and (listp function) (eq (car function) 'macro))
407 (apply (eval (cdr function)) (cdr form))
410 (defun compile-funcall (function args env fenv)
413 (concat (lookup-function function fenv)
415 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
418 ((and (listp function) (eq (car function) 'lambda))
419 (concat "(" (ls-compile function env fenv) ")("
420 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
424 (error "Invalid function designator ~a." function))))
426 (defun ls-compile (sexp &optional env fenv)
428 ((symbolp sexp) (lookup-variable sexp env))
429 ((integerp sexp) (integer-to-string sexp))
430 ((stringp sexp) (concat "\"" sexp "\""))
432 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
433 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
435 (apply compiler-func env fenv (cdr sexp))
436 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
438 (defun ls-compile-toplevel (sexp)
439 (setq *literals* nil)
440 (let ((code (ls-compile sexp)))
442 (concat (join (mapcar (lambda (lit)
443 (concat "var " (car lit) " = " (cdr lit) ";
448 (setq *literals* nil))))
451 (defun ls-compile-file (filename output)
452 (with-open-file (in filename)
453 (with-open-file (out output :direction :output :if-exists :supersede)
457 for compilation = (ls-compile-toplevel x)
458 when compilation do (write-line (concat compilation "; ") out)))))
461 (defun compile-test ()
462 (ls-compile-file "test.lisp" "test.js"))