+;;; Utils
+
+(defmacro while (condition &body body)
+ `(do ()
+ ((not ,condition))
+ ,@body))
+
+;;; simplify me, please
+(defun concat (&rest strs)
+ (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
+ strs
+ :initial-value ""))
+
+
+;;;; READER
+
+;;; It is a basic Lisp reader. It does not use advanced stuff
+;;; intentionally, because we want to use it to bootstrap a simple
+;;; Lisp. The main entry point is the function `ls-read', which
+;;; accepts a strings as argument and return the Lisp expression.
+(defun make-string-stream (string)
+ (cons string 0))
+
+(defun %peek-char (stream)
+ (if (streamp stream)
+ (peek-char nil stream nil)
+ (and (< (cdr stream) (length (car stream)))
+ (char (car stream) (cdr stream)))))
+
+(defun %read-char (stream)
+ (if (streamp stream)
+ (read-char stream nil)
+ (and (< (cdr stream) (length (car stream)))
+ (prog1 (char (car stream) (cdr stream))
+ (incf (cdr stream))))))
+
+(defun whitespacep (ch)
+ (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
+
+(defun skip-whitespaces (stream)
+ (loop for ch = (%peek-char stream)
+ while (and ch (whitespacep ch))
+ do (%read-char stream)))
+
+(defun terminalp (ch)
+ (or (null ch) (whitespacep ch) (char= #\) ch)))
+
+(defun read-until (stream func)
+ (let ((string ""))
+ (loop for ch = (%peek-char stream)
+ until (funcall func ch)
+ do (setq string (concat string (string ch)))
+ do (%read-char stream))
+ string))
+
+(defun skip-whitespaces-and-comments (stream)
+ (let (ch)
+ (skip-whitespaces stream)
+ (setq ch (%peek-char stream))
+ (while (and ch (eql ch #\;))
+ (read-until stream (lambda (x) (eql x #\newline)))
+ (skip-whitespaces stream)
+ (setq ch (%peek-char stream)))))
+
+(defun %read-list (stream)
+ (skip-whitespaces-and-comments stream)
+ (let ((ch (%peek-char stream)))
+ (cond
+ ((char= ch #\))
+ (%read-char stream)
+ nil)
+ ((char= ch #\.)
+ (%read-char stream)
+ (skip-whitespaces-and-comments stream)
+ (prog1 (ls-read stream)
+ (unless (char= (%read-char stream) #\))
+ (error "')' was expected."))))
+ (t
+ (cons (ls-read stream) (%read-list stream))))))
+
+(defvar *eof* (make-symbol "EOF"))
+(defun ls-read (stream)
+ (skip-whitespaces-and-comments stream)
+ (let ((ch (%peek-char stream)))
+ (cond
+ ((null ch)
+ *eof*)
+ ((char= ch #\()
+ (%read-char stream)
+ (%read-list stream))
+ ((char= ch #\')
+ (%read-char stream)
+ (list 'quote (ls-read stream)))
+ ((char= ch #\`)
+ (%read-char stream)
+ (list 'backquote (ls-read stream)))
+ ((char= ch #\")
+ (%read-char stream)
+ (prog1 (read-until stream (lambda (ch) (char= ch #\")))
+ (%read-char stream)))
+ ((char= ch #\,)
+ (%read-char stream)
+ (if (eql (%peek-char stream) #\@)
+ (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
+ (list 'unquote (ls-read stream))))
+ ((char= ch #\#)
+ (%read-char stream)
+ (ecase (%read-char stream)
+ (#\'
+ (list 'function (ls-read stream)))))
+ (t
+ (let ((string (read-until stream #'terminalp)))
+ (if (every #'digit-char-p string)
+ (parse-integer string)
+ (intern (string-upcase string))))))))
+
+(defun ls-read-from-string (string)
+ (ls-read (make-string-stream string)))
+
+
+;;;; Compiler
+
+(let ((counter 0))
+ (defun make-var-binding (symbol)
+ (cons symbol (format nil "v~d" (incf counter)))))
+
+(let ((counter 0))
+ (defun make-func-binding (symbol)
+ (cons symbol (format nil "f~d" (incf counter)))))
+
+
+;;; Concatenate a list of strings, with a separator
+(defun join (list separator)
+ (cond
+ ((null list)
+ "")
+ ((null (cdr list))
+ (car list))
+ (t
+ (concat (car list)
+ separator
+ (join (cdr list) separator)))))
+
+;;; Compiler
+
+(defvar *compilations* nil)
+
+(defun ls-compile-block (sexps env fenv)
+ (concat (join (mapcar (lambda (x)
+ (concat (ls-compile x env fenv) ";"))
+ sexps)
+ ";
+")))
+
+(defun extend-env (args env)
+ (append (mapcar #'make-var-binding args) env))
+
+(defparameter *env* '())
+(defparameter *fenv* '())
+
+(defun ls-lookup (symbol env)
+ (let ((binding (assoc symbol env)))
+ (and binding (format nil "~a" (cdr binding)))))
+
+(defun lookup-variable (symbol env)
+ (or (ls-lookup symbol env)
+ (ls-lookup symbol *env*)
+ (error "Undefined variable `~a'" symbol)))
+
+(defun lookup-function (symbol env)
+ (or (ls-lookup symbol env)
+ (ls-lookup symbol *fenv*)
+ (error "Undefined function `~a'" symbol)))
+
+(defmacro define-compilation (name args &body body)
+ ;; Creates a new primitive `name' with parameters args and
+ ;; @body. The body can access to the local environment through the
+ ;; variable ENV.
+ `(push (list ',name (lambda (env fenv ,@args) ,@body))
+ *compilations*))
+
+(define-compilation if (condition true false)
+ (format nil "((~a)? (~a) : (~a))"
+ (ls-compile condition env fenv)
+ (ls-compile true env fenv)
+ (ls-compile false env fenv)))
+
+;;; Return the required args of a lambda list
+(defun lambda-list-required-argument (lambda-list)
+ (if (or (null lambda-list) (eq (car lambda-list) '&rest))
+ nil
+ (cons (car lambda-list) (lambda-list-required-argument (cdr lambda-list)))))
+
+(defun lambda-list-rest-argument (lambda-list)
+ (second (member '&rest lambda-list)))
+
+(define-compilation lambda (lambda-list &rest body)
+ (let ((required-arguments (lambda-list-required-argument lambda-list))
+ (rest-argument (lambda-list-rest-argument lambda-list)))
+ (let ((new-env (extend-env (cons rest-argument required-arguments) env)))
+ (concat "(function ("
+ (join (mapcar (lambda (x) (lookup-variable x new-env))
+ required-arguments)
+ ",")
+ "){
+"
+ (if rest-argument
+ (concat "var " (lookup-variable rest-argument new-env)
+ " = arguments.slice("
+ (prin1-to-string (length required-arguments)) ");
+")
+ "")
+
+ (concat (ls-compile-block (butlast body) new-env fenv)
+ "return " (ls-compile (car (last body)) new-env fenv) ";")
+ "
+})"))))
+
+(define-compilation fsetq (var val)
+ (format nil "~a = ~a" (lookup-function var fenv) (ls-compile val env fenv)))
+
+(define-compilation setq (var val)
+ (format nil "~a = ~a" (lookup-variable var env) (ls-compile val env fenv)))
+
+
+;;; Literals
+
+(defvar *literals* '())
+
+(defun literal->js (sexp)
+ (cond
+ ((null sexp) "undefined")
+ ((integerp sexp) (format nil "~a" sexp))
+ ((stringp sexp) (format nil "\"~a\"" sexp))
+ ((symbolp sexp) (format nil "{name: \"~a\"}" (symbol-name 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)))
+
+(define-compilation function (x)
+ (cond
+ ((and (listp x) (eq (car x) 'lambda))
+ (ls-compile x env fenv))
+ ((symbolp x)
+ (lookup-function x 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*))
+
+
+(defmacro define-transformation (name args form)
+ `(define-compilation ,name ,args
+ (ls-compile ,form env fenv)))
+
+(define-transformation progn (&rest body)
+ `((lambda () ,@body)))
+
+(define-transformation let (bindings &rest body)
+ `((lambda ,(mapcar 'car bindings) ,@body)
+ ,@(mapcar 'cadr bindings)))
+
+;;; 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"))
+
+(define-compilation symbol-name (x)
+ (concat "(" (ls-compile x env fenv) ").name"))
+
+(define-compilation eq (x y)
+ (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))
+
+
+
+(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 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 compile-funcall (function args env fenv)
+ (cond
+ ((symbolp function)
+ (format nil "~a(~{~a~^, ~})"
+ (lookup-function function fenv)
+ (mapcar (lambda (x) (ls-compile x env fenv)) args)))
+ ((and (listp function) (eq (car function) 'lambda))
+ (format nil "(~a)(~{~a~^, ~})"
+ (ls-compile function env fenv)
+ (mapcar (lambda (x) (ls-compile x env fenv)) args)))
+ (t
+ (error "Invalid function designator ~a." function))))
+
+(defun ls-compile (sexp &optional env fenv)