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 (defvar *package* (new))
59 (set *package* name (make-symbol name)))
61 (defun find-symbol (name)
62 (get *package* name)))
65 (defun !reduce (func list initial)
70 (funcall func initial (car list)))))
76 (defmacro while (condition &body body)
82 (defun concat-two (s1 s2)
83 (concatenate 'string s1 s2)))
88 (defun concat (&rest strs)
89 (!reduce (lambda (s1 s2) (concat-two s1 s2))
93 ;;; Concatenate a list of strings, with a separator
94 (defun join (list separator)
103 (join (cdr list) separator)))))
105 (defun join-trailing (list separator)
108 (concat (car list) separator (join-trailing (cdr list) separator))))
110 (defun integer-to-string (x)
115 (push (mod x 10) digits)
116 (setq x (truncate x 10)))
117 (join (mapcar (lambda (d) (string (char "0123456789" d)))
123 ;;; It is a basic Lisp reader. It does not use advanced stuff
124 ;;; intentionally, because we want to use it to bootstrap a simple
125 ;;; Lisp. The main entry point is the function `ls-read', which
126 ;;; accepts a strings as argument and return the Lisp expression.
127 (defun make-string-stream (string)
130 (defun %peek-char (stream)
132 (peek-char nil stream nil)
133 (and (< (cdr stream) (length (car stream)))
134 (char (car stream) (cdr stream)))))
136 (defun %read-char (stream)
138 (read-char stream nil)
139 (and (< (cdr stream) (length (car stream)))
140 (prog1 (char (car stream) (cdr stream))
141 (incf (cdr stream))))))
143 (defun whitespacep (ch)
144 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
146 (defun skip-whitespaces (stream)
148 (setq ch (%peek-char stream))
149 (while (and ch (whitespacep ch))
151 (setq ch (%peek-char stream)))))
153 (defun terminalp (ch)
154 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
156 (defun read-until (stream func)
159 (setq ch (%peek-char stream))
160 (while (not (funcall func ch))
161 (setq string (concat string (string ch)))
163 (setq ch (%peek-char stream)))
166 (defun skip-whitespaces-and-comments (stream)
168 (skip-whitespaces stream)
169 (setq ch (%peek-char stream))
170 (while (and ch (eql ch #\;))
171 (read-until stream (lambda (x) (eql x #\newline)))
172 (skip-whitespaces stream)
173 (setq ch (%peek-char stream)))))
175 (defun %read-list (stream)
176 (skip-whitespaces-and-comments stream)
177 (let ((ch (%peek-char stream)))
184 (skip-whitespaces-and-comments stream)
185 (prog1 (ls-read stream)
186 (unless (char= (%read-char stream) #\))
187 (error "')' was expected."))))
189 (cons (ls-read stream) (%read-list stream))))))
191 (defvar *eof* (make-symbol "EOF"))
192 (defun ls-read (stream)
193 (skip-whitespaces-and-comments stream)
194 (let ((ch (%peek-char stream)))
203 (list 'quote (ls-read stream)))
206 (list 'backquote (ls-read stream)))
209 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
210 (%read-char stream)))
213 (if (eql (%peek-char stream) #\@)
214 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
215 (list 'unquote (ls-read stream))))
218 (ecase (%read-char stream)
220 (list 'function (ls-read stream)))
222 (let ((feature (read-until stream #'terminalp)))
224 ((string= feature "common-lisp")
225 (ls-read stream);ignore
227 ((string= feature "lispstrack")
230 (error "Unknown reader form.")))))))
232 (let ((string (read-until stream #'terminalp)))
233 (if (every #'digit-char-p string)
234 (parse-integer string)
235 (intern (string-upcase string))))))))
237 (defun ls-read-from-string (string)
238 (ls-read (make-string-stream string)))
244 (defun make-var-binding (symbol)
245 (cons symbol (concat "v" (integer-to-string (incf counter))))))
248 (defun make-func-binding (symbol)
249 (cons symbol (concat "f" (integer-to-string (incf counter))))))
251 (defvar *compilations* nil)
253 (defun ls-compile-block (sexps env fenv)
254 (join-trailing (mapcar (lambda (x)
255 (ls-compile x env fenv))
260 (defun extend-env (args env)
261 (append (mapcar #'make-var-binding args) env))
263 (defparameter *env* '())
264 (defparameter *fenv* '())
266 (defun ls-lookup (symbol env)
267 (let ((binding (assoc symbol env)))
268 (and binding (cdr binding))))
270 (defun lookup-variable (symbol env)
271 (or (ls-lookup symbol env)
272 (ls-lookup symbol *env*)
273 (error "Undefined variable `~a'" symbol)))
275 (defun lookup-function (symbol env)
276 (or (ls-lookup symbol env)
277 (ls-lookup symbol *fenv*)
278 (error "Undefined function `~a'" symbol)))
280 (defmacro define-compilation (name args &body body)
281 ;; Creates a new primitive `name' with parameters args and
282 ;; @body. The body can access to the local environment through the
284 `(push (list ',name (lambda (env fenv ,@args) ,@body))
287 (defvar *toplevel-compilations*)
289 (define-compilation if (condition true false)
291 (ls-compile condition env fenv)
293 (ls-compile true env fenv)
295 (ls-compile false env fenv)
298 ;;; Return the required args of a lambda list
299 (defun lambda-list-required-argument (lambda-list)
300 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
302 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
304 (defun lambda-list-rest-argument (lambda-list)
305 (second (member '&rest lambda-list)))
307 (define-compilation lambda (lambda-list &rest body)
308 (let ((required-arguments (lambda-list-required-argument lambda-list))
309 (rest-argument (lambda-list-rest-argument lambda-list)))
310 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
311 (concat "(function ("
312 (join (mapcar (lambda (x) (lookup-variable x new-env))
318 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
319 "for (var i = arguments.length-1; i>="
320 (integer-to-string (length required-arguments))
322 (lookup-variable rest-argument new-env) " = "
323 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
326 (concat (ls-compile-block (butlast body) new-env fenv)
327 "return " (ls-compile (car (last body)) new-env fenv) ";")
331 (define-compilation fsetq (var val)
332 (concat (lookup-function var fenv)
334 (ls-compile val env fenv)))
336 (define-compilation setq (var val)
337 (concat (lookup-variable var env)
339 (ls-compile val env fenv)))
344 (defun literal->js (sexp)
346 ((null sexp) "undefined")
347 ((integerp sexp) (integer-to-string sexp))
348 ((stringp sexp) (concat "\"" sexp "\""))
349 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
350 ((consp sexp) (concat "{car: "
351 (literal->js (car sexp))
353 (literal->js (cdr sexp)) "}"))))
356 (defun literal (form)
357 (let ((var (concat "l" (integer-to-string (incf counter)))))
358 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
361 (define-compilation quote (sexp)
364 (define-compilation debug (form)
365 (concat "console.log(" (ls-compile form env fenv) ")"))
367 (define-compilation while (pred &rest body)
368 (concat "(function(){ while("
369 (ls-compile pred env fenv)
371 (ls-compile-block body env fenv)
374 (define-compilation function (x)
376 ((and (listp x) (eq (car x) 'lambda))
377 (ls-compile x env fenv))
379 (lookup-function x fenv))))
382 (defmacro eval-when-compile (&body body)
383 `(eval-when (:compile-toplevel :execute)
386 (defvar *eval-when-compilations*)
387 (define-compilation eval-when-compile (&rest body)
388 (eval (cons 'progn body))
391 (defmacro define-transformation (name args form)
392 `(define-compilation ,name ,args
393 (ls-compile ,form env fenv)))
395 (define-transformation progn (&rest body)
396 `((lambda () ,@body)))
398 (define-transformation let (bindings &rest body)
399 `((lambda ,(mapcar 'car bindings) ,@body)
400 ,@(mapcar 'cadr bindings)))
402 ;;; A little backquote implementation without optimizations of any
403 ;;; kind for lispstrack.
404 (defun backquote-expand-1 (form)
410 ((eq (car form) 'unquote)
412 ((eq (car form) 'backquote)
413 (backquote-expand-1 (backquote-expand-1 (cadr form))))
418 ((and (listp s) (eq (car s) 'unquote))
419 (list 'list (cadr s)))
420 ((and (listp s) (eq (car s) 'unquote-splicing))
423 (list 'list (backquote-expand-1 s)))))
426 (defun backquote-expand (form)
427 (if (and (listp form) (eq (car form) 'backquote))
428 (backquote-expand-1 (cadr form))
431 (defmacro backquote (form)
432 (backquote-expand-1 form))
434 (define-transformation backquote (form)
435 (backquote-expand-1 form))
439 (define-compilation + (x y)
440 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
442 (define-compilation - (x y)
443 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
445 (define-compilation * (x y)
446 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
448 (define-compilation / (x y)
449 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
451 (define-compilation = (x y)
452 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
454 (define-compilation null (x)
455 (concat "(" (ls-compile x env fenv) "== undefined)"))
457 (define-compilation cons (x y)
458 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
460 (define-compilation car (x)
461 (concat "(" (ls-compile x env fenv) ").car"))
463 (define-compilation cdr (x)
464 (concat "(" (ls-compile x env fenv) ").cdr"))
466 (define-compilation make-symbol (name)
467 (concat "{name: " (ls-compile name env fenv) "}"))
469 (define-compilation symbol-name (x)
470 (concat "(" (ls-compile x env fenv) ").name"))
472 (define-compilation eq (x y)
473 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
475 (define-compilation eql (x y)
476 (concat "(" (ls-compile x env fenv) " == " (ls-compile y env fenv) ")"))
478 (define-compilation code-char (x)
479 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
481 (define-compilation char (string index)
483 (ls-compile string env fenv)
485 (ls-compile index env fenv)
488 (define-compilation concat-two (string1 string2)
490 (ls-compile string1 env fenv)
492 (ls-compile string2 env fenv)
495 (define-compilation funcall (func &rest args)
497 (ls-compile func env fenv)
499 (join (mapcar (lambda (x)
500 (ls-compile x env fenv))
505 (define-compilation new ()
508 (define-compilation get (object key)
509 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
511 (define-compilation set (object key value)
512 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]")
513 " = " (ls-compile value env fenv))
517 (defun %compile-defvar (name)
518 (push (make-var-binding name) *env*)
519 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*))
521 (defun %compile-defun (name)
522 (push (make-func-binding name) *fenv*)
523 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*))
525 (defun %compile-defmacro (name lambda)
526 (push (cons name (cons 'macro lambda)) *fenv*))
528 (defun ls-macroexpand-1 (form &optional env fenv)
529 (let ((function (cdr (assoc (car form) *fenv*))))
530 (if (and (listp function) (eq (car function) 'macro))
531 (apply (eval (cdr function)) (cdr form))
534 (defun compile-funcall (function args env fenv)
537 (concat (lookup-function function fenv)
539 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
542 ((and (listp function) (eq (car function) 'lambda))
543 (concat "(" (ls-compile function env fenv) ")("
544 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
548 (error "Invalid function designator ~a." function))))
550 (defun ls-compile (sexp &optional env fenv)
552 ((symbolp sexp) (lookup-variable sexp env))
553 ((integerp sexp) (integer-to-string sexp))
554 ((stringp sexp) (concat "\"" sexp "\""))
556 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
557 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
559 (apply compiler-func env fenv (cdr sexp))
560 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
562 (defun ls-compile-toplevel (sexp)
563 (setq *toplevel-compilations* nil)
564 (let ((code (ls-compile sexp)))
566 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
567 *toplevel-compilations*)
570 (setq *toplevel-compilations* nil))))
574 (defun ls-compile-file (filename output)
575 (with-open-file (in filename)
576 (with-open-file (out output :direction :output :if-exists :supersede)
580 for compilation = (ls-compile-toplevel x)
581 when compilation do (write-line (concat compilation "; ") out)))))
583 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))