1 (defun !reduce (func list initial)
6 (funcall func initial (car list)))))
12 (defmacro while (condition &body body)
18 (defun concat-two (s1 s2)
19 (concatenate 'string s1 s2)))
24 (defun concat (&rest strs)
25 (!reduce (lambda (s1 s2) (concat-two s1 s2))
29 ;;; Concatenate a list of strings, with a separator
30 (defun join (list separator)
39 (join (cdr list) separator)))))
41 (defun join-trailing (list separator)
44 (concat (car list) separator (join-trailing (cdr list) separator))))
46 (defun integer-to-string (x)
51 (push (mod x 10) digits)
52 (setq x (truncate x 10)))
53 (join (mapcar (lambda (d) (string (char "0123456789" d)))
59 ;;; It is a basic Lisp reader. It does not use advanced stuff
60 ;;; intentionally, because we want to use it to bootstrap a simple
61 ;;; Lisp. The main entry point is the function `ls-read', which
62 ;;; accepts a strings as argument and return the Lisp expression.
63 (defun make-string-stream (string)
66 (defun %peek-char (stream)
68 (peek-char nil stream nil)
69 (and (< (cdr stream) (length (car stream)))
70 (char (car stream) (cdr stream)))))
72 (defun %read-char (stream)
74 (read-char stream nil)
75 (and (< (cdr stream) (length (car stream)))
76 (prog1 (char (car stream) (cdr stream))
77 (incf (cdr stream))))))
79 (defun whitespacep (ch)
80 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
82 (defun skip-whitespaces (stream)
84 (setq ch (%peek-char stream))
85 (while (and ch (whitespacep ch))
87 (setq ch (%peek-char stream)))))
90 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
92 (defun read-until (stream func)
95 (setq ch (%peek-char stream))
96 (while (not (funcall func ch))
97 (setq string (concat string (string ch)))
99 (setq ch (%peek-char stream)))
102 (defun skip-whitespaces-and-comments (stream)
104 (skip-whitespaces stream)
105 (setq ch (%peek-char stream))
106 (while (and ch (eql ch #\;))
107 (read-until stream (lambda (x) (eql x #\newline)))
108 (skip-whitespaces stream)
109 (setq ch (%peek-char stream)))))
111 (defun %read-list (stream)
112 (skip-whitespaces-and-comments stream)
113 (let ((ch (%peek-char stream)))
120 (skip-whitespaces-and-comments stream)
121 (prog1 (ls-read stream)
122 (unless (char= (%read-char stream) #\))
123 (error "')' was expected."))))
125 (cons (ls-read stream) (%read-list stream))))))
127 (defvar *eof* (make-symbol "EOF"))
128 (defun ls-read (stream)
129 (skip-whitespaces-and-comments stream)
130 (let ((ch (%peek-char stream)))
139 (list 'quote (ls-read stream)))
142 (list 'backquote (ls-read stream)))
145 (prog1 (read-until stream (lambda (ch) (char= ch #\")))
146 (%read-char stream)))
149 (if (eql (%peek-char stream) #\@)
150 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
151 (list 'unquote (ls-read stream))))
154 (ecase (%read-char stream)
156 (list 'function (ls-read stream)))
158 (let ((feature (read-until stream #'terminalp)))
160 ((string= feature "common-lisp")
161 (ls-read stream);ignore
163 ((string= feature "lispstrack")
166 (error "Unknown reader form.")))))))
168 (let ((string (read-until stream #'terminalp)))
169 (if (every #'digit-char-p string)
170 (parse-integer string)
171 (intern (string-upcase string))))))))
173 (defun ls-read-from-string (string)
174 (ls-read (make-string-stream string)))
180 (defun make-var-binding (symbol)
181 (cons symbol (concat "v" (integer-to-string (incf counter))))))
184 (defun make-func-binding (symbol)
185 (cons symbol (concat "f" (integer-to-string (incf counter))))))
187 (defvar *compilations* nil)
189 (defun ls-compile-block (sexps env fenv)
190 (join-trailing (mapcar (lambda (x)
191 (ls-compile x env fenv))
196 (defun extend-env (args env)
197 (append (mapcar #'make-var-binding args) env))
199 (defparameter *env* '())
200 (defparameter *fenv* '())
202 (defun ls-lookup (symbol env)
203 (let ((binding (assoc symbol env)))
204 (and binding (cdr binding))))
206 (defun lookup-variable (symbol env)
207 (or (ls-lookup symbol env)
208 (ls-lookup symbol *env*)
209 (error "Undefined variable `~a'" symbol)))
211 (defun lookup-function (symbol env)
212 (or (ls-lookup symbol env)
213 (ls-lookup symbol *fenv*)
214 (error "Undefined function `~a'" symbol)))
216 (defmacro define-compilation (name args &body body)
217 ;; Creates a new primitive `name' with parameters args and
218 ;; @body. The body can access to the local environment through the
220 `(push (list ',name (lambda (env fenv ,@args) ,@body))
223 (defvar *toplevel-compilations*)
225 (define-compilation if (condition true false)
227 (ls-compile condition env fenv)
229 (ls-compile true env fenv)
231 (ls-compile false env fenv)
234 ;;; Return the required args of a lambda list
235 (defun lambda-list-required-argument (lambda-list)
236 (if (or (null lambda-list) (eq (car lambda-list) '&rest))
238 (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
240 (defun lambda-list-rest-argument (lambda-list)
241 (second (member '&rest lambda-list)))
243 (define-compilation lambda (lambda-list &rest body)
244 (let ((required-arguments (lambda-list-required-argument lambda-list))
245 (rest-argument (lambda-list-rest-argument lambda-list)))
246 (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
247 (concat "(function ("
248 (join (mapcar (lambda (x) (lookup-variable x new-env))
254 (concat "var " (lookup-variable rest-argument new-env) ";" *newline*
255 "for (var i = arguments.length-1; i>="
256 (integer-to-string (length required-arguments))
258 (lookup-variable rest-argument new-env) " = "
259 "{car: arguments[i], cdr: " (lookup-variable rest-argument new-env) "};"
262 (concat (ls-compile-block (butlast body) new-env fenv)
263 "return " (ls-compile (car (last body)) new-env fenv) ";")
267 (define-compilation fsetq (var val)
268 (concat (lookup-function var fenv)
270 (ls-compile val env fenv)))
272 (define-compilation setq (var val)
273 (concat (lookup-variable var env)
275 (ls-compile val env fenv)))
280 (defun literal->js (sexp)
282 ((null sexp) "undefined")
283 ((integerp sexp) (integer-to-string sexp))
284 ((stringp sexp) (concat "\"" sexp "\""))
285 ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
286 ((consp sexp) (concat "{car: "
287 (literal->js (car sexp))
289 (literal->js (cdr sexp)) "}"))))
292 (defun literal (form)
293 (let ((var (concat "l" (integer-to-string (incf counter)))))
294 (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
297 (define-compilation quote (sexp)
300 (define-compilation debug (form)
301 (concat "console.log(" (ls-compile form env fenv) ")"))
303 (define-compilation while (pred &rest body)
304 (concat "(function(){ while("
305 (ls-compile pred env fenv)
307 (ls-compile-block body env fenv)
310 (define-compilation function (x)
312 ((and (listp x) (eq (car x) 'lambda))
313 (ls-compile x env fenv))
315 (lookup-function x fenv))))
318 (defmacro eval-when-compile (&body body)
319 `(eval-when (:compile-toplevel :execute)
322 (defvar *eval-when-compilations*)
323 (define-compilation eval-when-compile (&rest body)
324 (eval (cons 'progn body))
327 (defmacro define-transformation (name args form)
328 `(define-compilation ,name ,args
329 (ls-compile ,form env fenv)))
331 (define-transformation progn (&rest body)
332 `((lambda () ,@body)))
334 (define-transformation let (bindings &rest body)
335 `((lambda ,(mapcar 'car bindings) ,@body)
336 ,@(mapcar 'cadr bindings)))
338 ;;; A little backquote implementation without optimizations of any
339 ;;; kind for lispstrack.
340 (defun backquote-expand-1 (form)
346 ((eq (car form) 'unquote)
348 ((eq (car form) 'backquote)
349 (backquote-expand-1 (backquote-expand-1 (cadr form))))
354 ((and (listp s) (eq (car s) 'unquote))
355 (list 'list (cadr s)))
356 ((and (listp s) (eq (car s) 'unquote-splicing))
359 (list 'list (backquote-expand-1 s)))))
362 (defun backquote-expand (form)
363 (if (and (listp form) (eq (car form) 'backquote))
364 (backquote-expand-1 (cadr form))
367 (defmacro backquote (form)
368 (backquote-expand-1 form))
370 (define-transformation backquote (form)
371 (backquote-expand-1 form))
375 (define-compilation + (x y)
376 (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
378 (define-compilation - (x y)
379 (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
381 (define-compilation * (x y)
382 (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
384 (define-compilation / (x y)
385 (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
387 (define-compilation = (x y)
388 (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
390 (define-compilation null (x)
391 (concat "(" (ls-compile x env fenv) "== undefined)"))
393 (define-compilation cons (x y)
394 (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
396 (define-compilation car (x)
397 (concat "(" (ls-compile x env fenv) ").car"))
399 (define-compilation cdr (x)
400 (concat "(" (ls-compile x env fenv) ").cdr"))
402 (define-compilation make-symbol (name)
403 (concat "{name: " (ls-compile name env fenv) "}"))
405 (define-compilation symbol-name (x)
406 (concat "(" (ls-compile x env fenv) ").name"))
408 (define-compilation eq (x y)
409 (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
411 (define-compilation code-char (x)
412 (concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
414 (define-compilation char (string index)
416 (ls-compile string env fenv)
418 (ls-compile index env fenv)
421 (define-compilation concat-two (string1 string2)
423 (ls-compile string1 env fenv)
425 (ls-compile string2 env fenv)
428 (define-compilation funcall (func &rest args)
430 (ls-compile func env fenv)
432 (join (mapcar (lambda (x)
433 (ls-compile x env fenv))
438 (define-compilation new ()
441 (define-compilation get (object key)
442 (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"))
444 (define-compilation set (object key value)
446 (ls-compile object env fenv)
448 (ls-compile key env fenv) "]"
449 " = " (ls-compile value env fenv) ")"))
452 (defun %compile-defvar (name)
453 (push (make-var-binding name) *env*)
454 (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*))
456 (defun %compile-defun (name)
457 (push (make-func-binding name) *fenv*)
458 (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*))
460 (defun %compile-defmacro (name lambda)
461 (push (cons name (cons 'macro lambda)) *fenv*))
463 (defun ls-macroexpand-1 (form &optional env fenv)
464 (let ((function (cdr (assoc (car form) *fenv*))))
465 (if (and (listp function) (eq (car function) 'macro))
466 (apply (eval (cdr function)) (cdr form))
469 (defun compile-funcall (function args env fenv)
472 (concat (lookup-function function fenv)
474 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
477 ((and (listp function) (eq (car function) 'lambda))
478 (concat "(" (ls-compile function env fenv) ")("
479 (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
483 (error "Invalid function designator ~a." function))))
485 (defun ls-compile (sexp &optional env fenv)
487 ((symbolp sexp) (lookup-variable sexp env))
488 ((integerp sexp) (integer-to-string sexp))
489 ((stringp sexp) (concat "\"" sexp "\""))
491 (let ((sexp (ls-macroexpand-1 sexp env fenv)))
492 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
494 (apply compiler-func env fenv (cdr sexp))
495 (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
497 (defun ls-compile-toplevel (sexp)
498 (setq *toplevel-compilations* nil)
499 (let ((code (ls-compile sexp)))
501 (concat (join (mapcar (lambda (x) (concat x ";" *newline*))
502 *toplevel-compilations*)
505 (setq *toplevel-compilations* nil))))
509 (defun ls-compile-file (filename output)
510 (with-open-file (in filename)
511 (with-open-file (out output :direction :output :if-exists :supersede)
515 for compilation = (ls-compile-toplevel x)
516 when compilation do (write-line (concat compilation "; ") out)))))
518 (ls-compile-file "lispstrack.lisp" "lispstrack.js")))