4 (%compile-defmacro 'defmacro
5 (lambda (name args &rest body)
7 (%compile-defmacro ',name (lambda ,args ,@body))))))
9 (defmacro defvar (name value)
12 (%compile-defvar ',name))
15 (defmacro defun (name args &rest body)
18 (%compile-defun ',name))
19 (fsetq ,name (lambda ,args ,@body))))
21 (defun = (x y) (= x y))
22 (defun + (x y) (+ x y))
23 (defun - (x y) (- x y))
24 (defun * (x y) (* x y))
25 (defun / (x y) (/ x y))
26 (defun 1+ (x) (+ x 1))
27 (defun 1- (x) (- x 1))
28 (defun cons (x y) (cons x y))
29 (defun car (x) (car x))
30 (defun cdr (x) (cdr x))
32 (defun append (list1 list2)
36 (append (cdr list1) list2))))
38 (defun reverse-aux (list acc)
41 (reverse-aux (cdr list) (cons (car list) acc))))
44 (reverse-aux list '()))
46 (defun mapcar (func list)
49 (cons (funcall func (car list))
50 (mapcar func (cdr list)))))
52 (defmacro push (x place)
53 `(setq ,place (cons ,x ,place))))
56 (defun !reduce (func list initial)
61 (funcall func initial (car list)))))
67 (defmacro while (condition &body body)
73 (defun concat-two (s1 s2)
74 (concatenate 'string s1 s2)))
79 (defun concat (&rest strs)
80 (!reduce (lambda (s1 s2) (concat-two s1 s2))
84 ;;; Concatenate a list of strings, with a separator
85 (defun join (list separator)
94 (join (cdr list) separator)))))
96 (defun join-trailing (list separator)
99 (concat (car list) separator (join-trailing (cdr list) separator))))
101 (defun integer-to-string (x)
106 (push (mod x 10) digits)
107 (setq x (truncate x 10)))
108 (join (mapcar (lambda (d) (string (char "0123456789" d)))
114 ;;; It is a basic Lisp reader. It does not use advanced stuff
115 ;;; intentionally, because we want to use it to bootstrap a simple
116 ;;; Lisp. The main entry point is the function `ls-read', which
117 ;;; accepts a strings as argument and return the Lisp expression.
118 (defun make-string-stream (string)
121 (defun %peek-char (stream)
123 (peek-char nil stream nil)
124 (and (< (cdr stream) (length (car stream)))
125 (char (car stream) (cdr stream)))))
127 (defun %read-char (stream)
129 (read-char stream nil)
130 (and (< (cdr stream) (length (car stream)))
131 (prog1 (char (car stream) (cdr stream))
132 (incf (cdr stream))))))
134 (defun whitespacep (ch)
135 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
137 (defun skip-whitespaces (stream)
139 (setq ch (%peek-char stream))
140 (while (and ch (whitespacep ch))
142 (setq ch (%peek-char stream)))))
144 (defun terminalp (ch)
145 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
147 (defun read-until (stream func)
150 (setq ch (%peek-char stream))
151 (while (not (funcall func ch))
152 (setq string (concat string (string ch)))
154 (setq ch (%peek-char stream)))
157 (defun skip-whitespaces-and-comments (stream)
159 (skip-whitespaces stream)
160 (setq ch (%peek-char stream))
161 (while (and ch (eql ch #\;))
162 (read-until stream (lambda (x) (eql x #\newline)))
163 (skip-whitespaces stream)
164 (setq ch (%peek-char stream)))))
166 (defun %read-list (stream)
167 (skip-whitespaces-and-comments stream)
168 (let ((ch (%peek-char stream)))
175 (skip-whitespaces-and-comments stream)
176 (prog1 (ls-read stream)
177 (unless (char= (%read-char stream) #\))
178 (error "')' was expected."))))
180 (cons (ls-read stream) (%read-list stream))))))
182 (defvar *eof* (make-symbol "EOF"))
183 (defun ls-read (stream)
184 (skip-whitespaces-and-comments stream)
185 (let ((ch (%peek-char stream)))
194 (list 'quote (ls-read stream)))
197 (list 'backquote (ls-read stream)))
200 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
201 (%read-char stream)))
204 (if (eql (%peek-char stream) #\@)
205 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
206 (list 'unquote (ls-read stream))))
209 (ecase (%read-char stream)
211 (list 'function (ls-read stream)))
213 (let ((feature (read-until stream #'terminalp)))
215 ((string= feature "common-lisp")
216 (ls-read stream);ignore
218 ((string= feature "lispstrack")
221 (error "Unknown reader form.")))))))
223 (let ((string (read-until stream #'terminalp)))
224 (if (every #'digit-char-p string)
225 (parse-integer string)
226 (intern (string-upcase string))))))))
228 (defun ls-read-from-string (string)
229 (ls-read (make-string-stream string)))
235 (defun make-var-binding (symbol)
236 (cons symbol (concat "v" (integer-to-string (incf counter))))))
239 (defun make-func-binding (symbol)
240 (cons symbol (concat "f" (integer-to-string (incf counter))))))
242 (defvar *compilations* nil)
244 (defun ls-compile-block (sexps env fenv)
245 (join-trailing (mapcar (lambda (x)
246 (ls-compile x env fenv))
251 (defun extend-env (args env)
252 (append (mapcar #'make-var-binding args) env))
254 (defparameter *env* '())
255 (defparameter *fenv* '())
257 (defun ls-lookup (symbol env)
258 (let ((binding (assoc symbol env)))
259 (and binding (cdr binding))))
261 (defun lookup-variable (symbol env)
262 (or (ls-lookup symbol env)
263 (ls-lookup symbol *env*)
264 (error "Undefined variable `~a'" symbol)))
266 (defun lookup-function (symbol env)
267 (or (ls-lookup symbol env)
268 (ls-lookup symbol *fenv*)
269 (error "Undefined function `~a'" symbol)))
271 (defmacro define-compilation (name args &body body)
272 ;; Creates a new primitive `name' with parameters args and
273 ;; @body. The body can access to the local environment through the
275 `(push (list ',name (lambda (env fenv ,@args) ,@body))
278 (defvar *toplevel-compilations*)
280 (define-compilation if (condition true false)
282 (ls-compile condition env fenv)
284 (ls-compile true env fenv)
286 (ls-compile false env fenv)
289 ;;; Return the required args of a lambda list
290 (defun lambda-list-required-argument (lambda-list)
291 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
293 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
295 (defun lambda-list-rest-argument (lambda-list)
296 (second (member '&rest lambda-list)))
298 (define-compilation lambda (lambda-list &rest body)
299 (let ((required-arguments (lambda-list-required-argument lambda-list))
300 (rest-argument (lambda-list-rest-argument lambda-list)))
301 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
302 (concat "(function ("
303 (join (mapcar (lambda (x) (lookup-variable x new-env))
309 (concat "var " (lookup-variable rest-argument new-env)
310 " = arguments.slice("
311 (prin1-to-string (length required-arguments))
315 (concat (ls-compile-block (butlast body) new-env fenv)
316 "return " (ls-compile (car (last body)) new-env fenv) ";")
320 (define-compilation fsetq (var val)
321 (concat (lookup-function var fenv)
323 (ls-compile val env fenv)))
325 (define-compilation setq (var val)
326 (concat (lookup-variable var env)
328 (ls-compile val env fenv)))
333 (defun literal->js (sexp)
335 ((null sexp) "undefined")
336 ((integerp sexp) (integer-to-string sexp))
337 ((stringp sexp) (concat "\"" sexp "\""))
338 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
339 ((consp sexp) (concat "{car: "
340 (literal->js (car sexp))
342 (literal->js (cdr sexp)) "}"))))
345 (defun literal (form)
346 (let ((var (concat "l" (integer-to-string (incf counter)))))
347 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
350 (define-compilation quote (sexp)
353 (define-compilation debug (form)
354 (concat "console.log(" (ls-compile form env fenv) ")"))
356 (define-compilation while (pred &rest body)
357 (concat "(function(){ while("
358 (ls-compile pred env fenv)
360 (ls-compile-block body env fenv)
363 (define-compilation function (x)
365 ((and (listp x) (eq (car x) 'lambda))
366 (ls-compile x env fenv))
368 (lookup-function x fenv))))
371 (defmacro eval-when-compile (&body body)
372 `(eval-when (:compile-toplevel :execute)
375 (defvar *eval-when-compilations*)
376 (define-compilation eval-when-compile (&rest body)
377 (eval (cons 'progn body))
380 (defmacro define-transformation (name args form)
381 `(define-compilation ,name ,args
382 (ls-compile ,form env fenv)))
384 (define-transformation progn (&rest body)
385 `((lambda () ,@body)))
387 (define-transformation let (bindings &rest body)
388 `((lambda ,(mapcar 'car bindings) ,@body)
389 ,@(mapcar 'cadr bindings)))
391 ;;; A little backquote implementation without optimizations of any
392 ;;; kind for lispstrack.
393 (defun backquote-expand-1 (form)
399 ((eq (car form) 'unquote)
401 ((eq (car form) 'backquote)
402 (backquote-expand-1 (backquote-expand-1 (cadr form))))
407 ((and (listp s) (eq (car s) 'unquote))
408 (list 'list (cadr s)))
409 ((and (listp s) (eq (car s) 'unquote-splicing))
412 (list 'list (backquote-expand-1 s)))))
415 (defun backquote-expand (form)
416 (if (and (listp form) (eq (car form) 'backquote))
417 (backquote-expand-1 (cadr form))
420 (defmacro backquote (form)
421 (backquote-expand-1 form))
423 (define-transformation backquote (form)
424 (backquote-expand-1 form))
428 (define-compilation + (x y)
429 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
431 (define-compilation - (x y)
432 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
434 (define-compilation * (x y)
435 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
437 (define-compilation / (x y)
438 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
440 (define-compilation = (x y)
441 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
443 (define-compilation null (x)
444 (concat "(" (ls-compile x env fenv) "== undefined)"))
446 (define-compilation cons (x y)
447 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
449 (define-compilation car (x)
450 (concat "(" (ls-compile x env fenv) ").car"))
452 (define-compilation cdr (x)
453 (concat "(" (ls-compile x env fenv) ").cdr"))
455 (define-compilation symbol-name (x)
456 (concat "(" (ls-compile x env fenv) ").name"))
458 (define-compilation eq (x y)
459 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
461 (define-compilation eql (x y)
462 (concat "(" (ls-compile x env fenv) " == " (ls-compile y env fenv) ")"))
464 (define-compilation code-char (x)
465 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
467 (define-compilation char (string index)
469 (ls-compile string env fenv)
471 (ls-compile index env fenv)
474 (define-compilation concat-two (string1 string2)
476 (ls-compile string1 env fenv)
478 (ls-compile string2 env fenv)
481 (define-compilation funcall (func &rest args)
483 (ls-compile func env fenv)
485 (join (mapcar (lambda (x)
486 (ls-compile x env fenv))
491 (defun %compile-defvar (name)
492 (push (make-var-binding name) *env*)
493 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*))
495 (defun %compile-defun (name)
496 (push (make-func-binding name) *fenv*)
497 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*))
499 (defun %compile-defmacro (name lambda)
500 (push (cons name (cons 'macro lambda)) *fenv*))
502 (defun ls-macroexpand-1 (form &optional env fenv)
503 (let ((function (cdr (assoc (car form) *fenv*))))
504 (if (and (listp function) (eq (car function) 'macro))
505 (apply (eval (cdr function)) (cdr form))
508 (defun compile-funcall (function args env fenv)
511 (concat (lookup-function function fenv)
513 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
516 ((and (listp function) (eq (car function) 'lambda))
517 (concat "(" (ls-compile function env fenv) ")("
518 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
522 (error "Invalid function designator ~a." function))))
524 (defun ls-compile (sexp &optional env fenv)
526 ((symbolp sexp) (lookup-variable sexp env))
527 ((integerp sexp) (integer-to-string sexp))
528 ((stringp sexp) (concat "\"" sexp "\""))
530 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
531 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
533 (apply compiler-func env fenv (cdr sexp))
534 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
536 (defun ls-compile-toplevel (sexp)
537 (setq *toplevel-compilations* nil)
538 (let ((code (ls-compile sexp)))
540 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
541 *toplevel-compilations*)
544 (setq *toplevel-compilations* nil))))
548 (defun ls-compile-file (filename output)
549 (with-open-file (in filename)
550 (with-open-file (out output :direction :output :if-exists :supersede)
554 for compilation = (ls-compile-toplevel x)
555 when compilation do (write-line (concat compilation "; ") out)))))
557 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))