3 (defmacro while (condition &body body)
8 ;;; simplify me, please
9 (defun concat (&rest strs)
10 (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
16 (defun make-binding (symbol)
17 (cons symbol (format nil "V_~d" (incf counter)))))
19 ;;; Concatenate a list of strings, with a separator
20 (defun join (list separator)
29 (join (cdr list) separator)))))
33 (defvar *compilations* nil)
35 (defun ls-compile-sexps (sexps env)
36 (concat (join (mapcar (lambda (x)
37 (concat (ls-compile x env) ";"))
42 (defun ls-compile-block (sexps env)
43 (concat (ls-compile-sexps (butlast sexps) env)
44 "return " (ls-compile (car (last sexps)) env) ";"))
47 (defun extend-env (args env)
48 (append (mapcar #'make-binding args) env))
50 (defun ls-lookup (symbol env)
51 (let ((binding (assoc symbol env)))
53 (format nil "~a" (cdr binding))
54 (error "Undefined variable `~a'" symbol))))
56 (defmacro define-compilation (name args &body body)
57 ;; Creates a new primitive `name' with parameters args and
58 ;; @body. The body can access to the local environment through the
60 `(push (list ',name (lambda (env ,@args) ,@body))
63 (define-compilation if (condition true false)
64 (format nil "((~a)? (~a) : (~a))"
65 (ls-compile condition env)
67 (ls-compile false env)))
69 (define-compilation lambda (args &rest body)
70 (let ((new-env (extend-env args env)))
72 (join (mapcar (lambda (x) (ls-lookup x new-env))
77 (ls-compile-block body new-env)
81 (define-compilation setq (var val)
82 (format nil "~a = ~a" (ls-lookup var env) (ls-compile val env)))
84 (defun lisp->js (sexp)
86 ((integerp sexp) (format nil "~a" sexp))
87 ((stringp sexp) (format nil "\"~a\"" sexp))
88 ((listp sexp) (concat "[" (join (mapcar 'lisp->js sexp) ",") "]"))))
90 (define-compilation quote (sexp)
93 (define-compilation debug (form)
94 (format nil "console.log(~a)" (ls-compile form env)))
96 (define-compilation while (pred &rest body)
97 (format nil "(function(){while(~a){~a}})() "
99 (ls-compile-sexps body env)))
101 (defmacro eval-when-compile (&body body)
102 `(eval-when (:compile-toplevel :execute)
105 (define-compilation eval-when-compile (when &rest body)
108 ;;; aritmetic primitives
109 (define-compilation + (x y)
110 (concat "((" (ls-compile x env) ") + (" (ls-compile y env) "))"))
112 (define-compilation - (x y)
113 (concat "((" (ls-compile x env) ") - (" (ls-compile y env) "))"))
115 (define-compilation * (x y)
116 (concat "((" (ls-compile x env) ") * (" (ls-compile y env) "))"))
118 (define-compilation / (x y)
119 (concat "((" (ls-compile x env) ") / (" (ls-compile y env) "))"))
121 (define-compilation = (x y)
122 (concat "((" (ls-compile x env) ") == (" (ls-compile y env) "))"))
125 (defparameter *env* '())
126 (defparameter *env-fun* '())
128 (defun ls-compile (sexp &optional env)
130 ((symbolp sexp) (ls-lookup sexp env))
131 ((integerp sexp) (format nil "~a" sexp))
132 ((stringp sexp) (format nil " \"~a\" " sexp))
134 (let ((compiler-func (second (assoc (car sexp) *compilations*))))
136 (apply compiler-func env (cdr sexp))
137 (funcall (ls-compile (car sexp) env) )
144 (defun compile-test ()
145 (with-open-file (in "test.lisp")
146 (with-open-file (out "test.js" :direction :output :if-exists :supersede)
148 for x = (read in nil) while x
149 do (write-string (concat (ls-compile x) "; ") out)))))