+ ((stringp sexp) (format nil "\"~a\"" sexp))
+ ((consp sexp) (concat "{car: "
+ (literal->js (car sexp))
+ ", cdr: "
+ (literal->js (cdr sexp)) "}"))))
+
+(let ((counter 0))
+ (defun literal (form)
+ (let ((var (format nil "l~d" (incf counter))))
+ (push (cons var (literal->js form)) *literals*)
+ var)))
+
+(define-compilation quote (sexp)
+ (literal sexp))
+
+(define-compilation debug (form)
+ (format nil "console.log(~a)" (ls-compile form env fenv)))
+
+(define-compilation while (pred &rest body)
+ (format nil "(function(){while(~a){~a}})() "
+ (ls-compile pred env fenv)
+ (ls-compile-block body env fenv)))
+
+(defmacro eval-when-compile (&body body)
+ `(eval-when (:compile-toplevel :execute)
+ ,@body))
+
+(defvar *eval-when-compilations*)
+(define-compilation eval-when-compile (&rest body)
+ (setq *eval-when-compilations* "")
+ (eval (cons 'progn body))
+ (if (string= *eval-when-compilations* "")
+ nil
+ *eval-when-compilations*))
+
+;;; aritmetic primitives
+(define-compilation + (x y)
+ (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))"))
+
+(define-compilation - (x y)
+ (concat "((" (ls-compile x env fenv) ") - (" (ls-compile y env fenv) "))"))
+
+(define-compilation * (x y)
+ (concat "((" (ls-compile x env fenv) ") * (" (ls-compile y env fenv) "))"))
+
+(define-compilation / (x y)
+ (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))"))
+
+(define-compilation = (x y)
+ (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))
+
+(define-compilation cons (x y)
+ (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
+
+(define-compilation car (x)
+ (concat "(" (ls-compile x env fenv) ").car"))
+
+(define-compilation cdr (x)
+ (concat "(" (ls-compile x env fenv) ").cdr"))
+
+(defmacro with-eval-when-compilation (&body body)
+ `(setq *eval-when-compilations*
+ (concat *eval-when-compilations* (progn ,@body))))
+
+(defun %compile-defvar (name)
+ (push (make-var-binding name) *env*)
+ (with-eval-when-compilation
+ (format nil "var ~a" (lookup-variable name *env*))))
+
+(defun %compile-defun (name)
+ (push (make-func-binding name) *fenv*)
+ (with-eval-when-compilation
+ (format nil "var ~a" (lookup-variable name *fenv*))))
+
+(defun %compile-defmacro (name lambda)
+ (push (cons name (cons 'macro lambda)) *fenv*))
+
+(defun compile-funcall (name args env fenv)
+ (format nil "~a(~{~a~^, ~})"
+ (lookup-function name fenv)
+ (mapcar (lambda (x) (ls-compile x env fenv)) args)))
+
+(defun ls-macroexpand-1 (form &optional env fenv)
+ (let ((function (cdr (assoc (car form) *fenv*))))
+ (if (and (listp function) (eq (car function) 'macro))
+ (apply (eval (cdr function)) (cdr form))
+ form)))
+
+(defun ls-compile (sexp &optional env fenv)
+ (cond
+ ((symbolp sexp) (lookup-variable sexp env))
+ ((integerp sexp) (format nil "~a" sexp))
+ ((stringp sexp) (format nil "\"~a\"" sexp))
+ ((listp sexp)
+ (let ((sexp (ls-macroexpand-1 sexp env fenv)))
+ (let ((compiler-func (second (assoc (car sexp) *compilations*))))
+ (if compiler-func
+ (apply compiler-func env fenv (cdr sexp))
+ (compile-funcall (car sexp) (cdr sexp) env fenv)))))))
+
+(defun ls-compile-toplevel (sexp)
+ (setq *literals* nil)
+ (let ((code (ls-compile sexp)))
+ (prog1
+ (concat (join (mapcar (lambda (lit)
+ (concat "var " (car lit) " = " (cdr lit) ";
+"))
+ *literals*)
+ "")
+ code)
+ (setq *literals* nil))))
+
+
+(defun ls-compile-file (filename output)
+ (with-open-file (in filename)
+ (with-open-file (out output :direction :output :if-exists :supersede)
+ (loop
+ for x = (read in nil) while x
+ for compilation = (ls-compile-toplevel x)
+ when compilation do (write-line (concat compilation "; ") out)))))
+
+
+;;; Testing
+
+(defun compile-test ()
+ (ls-compile-file "test.lisp" "test.js"))