+ ((symbolp form)
+ (list 'quote form))
+ ((atom form)
+ form)
+ ((eq (car form) 'unquote)
+ (car form))
+ ((eq (car form) 'backquote)
+ (backquote-expand-1 (backquote-expand-1 (cadr form))))
+ (t
+ (cons 'append
+ (mapcar (lambda (s)
+ (cond
+ ((and (listp s) (eq (car s) 'unquote))
+ (list 'list (cadr s)))
+ ((and (listp s) (eq (car s) 'unquote-splicing))
+ (cadr s))
+ (t
+ (list 'list (backquote-expand-1 s)))))
+ form)))))
+
+(defun backquote-expand (form)
+ (if (and (listp form) (eq (car form) 'backquote))
+ (backquote-expand-1 (cadr form))
+ form))
+
+(defmacro backquote (form)
+ (backquote-expand-1 form))
+
+(define-transformation backquote (form)
+ (backquote-expand-1 form))
+
+;;; Primitives
+
+(defun compile-bool (x)
+ (concat "(" x "?" (ls-compile t nil nil) ": " (ls-compile nil nil nil) ")"))
+
+(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)
+ (compile-bool (concat "((" (ls-compile x env fenv) ") < (" (ls-compile y env fenv) "))")))
+
+(define-compilation = (x y)
+ (compile-bool (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))")))
+
+(define-compilation numberp (x)
+ (compile-bool (concat "(typeof (" (ls-compile x env fenv) ") == \"number\")")))
+
+
+(define-compilation mod (x y)
+ (concat "((" (ls-compile x env fenv) ") % (" (ls-compile y env fenv) "))"))
+
+(define-compilation floor (x)
+ (concat "(Math.floor(" (ls-compile x env fenv) "))"))
+
+(define-compilation null (x)
+ (compile-bool (concat "(" (ls-compile x env fenv) "===" (ls-compile nil env fenv) ")")))
+
+(define-compilation cons (x y)
+ (concat "({car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "})"))
+
+(define-compilation consp (x)
+ (compile-bool
+ (concat "(function(){ var tmp = "
+ (ls-compile x env fenv)
+ "; return (typeof tmp == 'object' && 'car' in tmp);})()")))
+
+(define-compilation car (x)
+ (concat "(function () { var tmp = " (ls-compile x env fenv)
+ "; return tmp === " (ls-compile nil nil nil) "? "
+ (ls-compile nil nil nil)
+ ": tmp.car; })()"))
+
+(define-compilation cdr (x)
+ (concat "(function () { var tmp = " (ls-compile x env fenv)
+ "; return tmp === " (ls-compile nil nil nil) "? "
+ (ls-compile nil nil nil)
+ ": tmp.cdr; })()"))
+
+(define-compilation setcar (x new)
+ (concat "((" (ls-compile x env fenv) ").car = " (ls-compile new env fenv) ")"))
+
+(define-compilation setcdr (x new)
+ (concat "((" (ls-compile x env fenv) ").cdr = " (ls-compile new env fenv) ")"))
+
+(define-compilation symbolp (x)
+ (compile-bool
+ (concat "(function(){ var tmp = "
+ (ls-compile x env fenv)
+ "; return (typeof tmp == 'object' && 'name' in tmp); })()")))
+
+(define-compilation make-symbol (name)
+ (concat "{name: " (ls-compile name env fenv) "}"))
+
+(define-compilation symbol-name (x)
+ (concat "(" (ls-compile x env fenv) ").name"))
+
+(define-compilation eq (x y)
+ (compile-bool
+ (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")")))
+
+(define-compilation equal (x y)
+ (compile-bool
+ (concat "(" (ls-compile x env fenv) " == " (ls-compile y env fenv) ")")))
+
+(define-compilation string (x)
+ (concat "String.fromCharCode(" (ls-compile x env fenv) ")"))
+
+(define-compilation stringp (x)
+ (compile-bool
+ (concat "(typeof(" (ls-compile x env fenv) ") == \"string\")")))
+
+(define-compilation string-upcase (x)
+ (concat "(" (ls-compile x env fenv) ").toUpperCase()"))
+
+(define-compilation string-length (x)
+ (concat "(" (ls-compile x env fenv) ").length"))
+
+(define-compilation char (string index)
+ (concat "("
+ (ls-compile string env fenv)
+ ").charCodeAt("
+ (ls-compile index env fenv)
+ ")"))
+
+(define-compilation concat-two (string1 string2)
+ (concat "("
+ (ls-compile string1 env fenv)
+ ").concat("
+ (ls-compile string2 env fenv)
+ ")"))
+
+(define-compilation funcall (func &rest args)
+ (concat "("
+ (ls-compile func env fenv)
+ ")("
+ (join (mapcar (lambda (x)
+ (ls-compile x env fenv))
+ args)
+ ", ")
+ ")"))
+
+(define-compilation apply (func &rest args)
+ (if (null args)
+ (concat "(" (ls-compile func env fenv) ")()")
+ (let ((args (butlast args))
+ (last (car (last args))))
+ (concat "(function(){" *newline*
+ "var f = " (ls-compile func env fenv) ";" *newline*
+ "var args = [" (join (mapcar (lambda (x)
+ (ls-compile x env fenv))
+ args)
+ ", ")
+ "];" *newline*
+ "var tail = (" (ls-compile last env fenv) ");" *newline*
+ "while (tail != " (ls-compile nil env fenv) "){" *newline*
+ " args.push(tail.car);" *newline*
+ " tail = tail.cdr;" *newline*
+ "}" *newline*
+ "return f.apply(this, args);" *newline*
+ "})()" *newline*))))
+
+(define-compilation js-eval (string)
+ (concat "eval.apply(window, [" (ls-compile string env fenv) "])"))
+
+
+(define-compilation error (string)
+ (concat "(function (){ throw " (ls-compile string env fenv) ";" "return 0;})()"))
+
+(define-compilation new ()
+ "{}")
+
+(define-compilation get (object key)
+ (concat "(function(){ var tmp = "
+ "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]"
+ ";"
+ "return tmp == undefined? " (ls-compile nil nil nil) ": tmp ;"
+ "})()"))
+
+(define-compilation set (object key value)
+ (concat "(("
+ (ls-compile object env fenv)
+ ")["
+ (ls-compile key env fenv) "]"
+ " = " (ls-compile value env fenv) ")"))
+
+(define-compilation in (key object)
+ (compile-bool
+ (concat "(" (ls-compile key env fenv) " in " (ls-compile object env fenv) ")")))
+
+(defun macrop (x)
+ (and (symbolp x) (eq (binding-type (lookup-function x *fenv*)) 'macro)))
+
+(defun ls-macroexpand-1 (form env fenv)
+ (if (macrop (car form))
+ (let ((binding (lookup-function (car form) *env*)))
+ (if (eq (binding-type binding) 'macro)
+ (apply (eval (binding-translation binding)) (cdr form))
+ form))
+ form))
+
+(defun compile-funcall (function args env fenv)
+ (cond
+ ((symbolp function)
+ (concat (lookup-function-translation function fenv)
+ "("
+ (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
+ ", ")
+ ")"))
+ ((and (listp function) (eq (car function) 'lambda))
+ (concat "(" (ls-compile function env fenv) ")("
+ (join (mapcar (lambda (x) (ls-compile x env fenv)) args)
+ ", ")
+ ")"))
+ (t
+ (error (concat "Invalid function designator " (symbol-name function))))))
+
+(defun ls-compile (sexp env fenv)
+ (cond
+ ((symbolp sexp) (lookup-variable-translation sexp env))
+ ((integerp sexp) (integer-to-string sexp))
+ ((stringp sexp) (concat "\"" (escape-string sexp) "\""))