+ (let* ((bindings (mapcar #'ensure-list bindings))
+ (variables (mapcar #'first bindings))
+ (cvalues (mapcar #'ls-compile (mapcar #'second bindings)))
+ (*environment* (extend-local-env (remove-if #'special-variable-p variables)))
+ (dynamic-bindings))
+ (code "(function("
+ (join (mapcar (lambda (x)
+ (if (special-variable-p x)
+ (let ((v (gvarname x)))
+ (push (cons x v) dynamic-bindings)
+ v)
+ (translate-variable x)))
+ variables)
+ ",")
+ "){" *newline*
+ (let ((body (ls-compile-block body t)))
+ (indent (let-binding-wrapper dynamic-bindings body)))
+ "})(" (join cvalues ",") ")")))
+
+
+;;; Return the code to initialize BINDING, and push it extending the
+;;; current lexical environment if the variable is not special.
+(defun let*-initialize-value (binding)
+ (let ((var (first binding))
+ (value (second binding)))
+ (if (special-variable-p var)
+ (code (ls-compile `(setq ,var ,value)) ";" *newline*)
+ (let* ((v (gvarname var))
+ (b (make-binding var 'variable v)))
+ (prog1 (code "var " v " = " (ls-compile value) ";" *newline*)
+ (push-to-lexenv b *environment* 'variable))))))
+
+;;; Wrap BODY to restore the symbol values of SYMBOLS after body. It
+;;; DOES NOT generate code to initialize the value of the symbols,
+;;; unlike let-binding-wrapper.
+(defun let*-binding-wrapper (symbols body)
+ (when (null symbols)
+ (return-from let*-binding-wrapper body))
+ (let ((store (mapcar (lambda (s) (cons s (gvarname s)))
+ (remove-if-not #'special-variable-p symbols))))
+ (code
+ "try {" *newline*
+ (indent
+ (mapconcat (lambda (b)
+ (let ((s (ls-compile `(quote ,(car b)))))
+ (code "var " (cdr b) " = " s ".value;" *newline*)))
+ store)
+ body)
+ "}" *newline*
+ "finally {" *newline*
+ (indent
+ (mapconcat (lambda (b)
+ (let ((s (ls-compile `(quote ,(car b)))))
+ (code s ".value" " = " (cdr b) ";" *newline*)))
+ store))
+ "}" *newline*)))
+
+(define-compilation let* (bindings &rest body)
+ (let ((bindings (mapcar #'ensure-list bindings))
+ (*environment* (copy-lexenv *environment*)))
+ (js!selfcall
+ (let ((specials (remove-if-not #'special-variable-p (mapcar #'first bindings)))
+ (body (concat (mapconcat #'let*-initialize-value bindings)
+ (ls-compile-block body t))))
+ (let*-binding-wrapper specials body)))))
+
+
+(defvar *block-counter* 0)
+
+(define-compilation block (name &rest body)
+ (let* ((tr (incf *block-counter*))
+ (b (make-binding name 'block tr)))
+ (when *multiple-value-p*
+ (push-binding-declaration 'multiple-value b))
+ (let* ((*environment* (extend-lexenv (list b) *environment* 'block))
+ (cbody (ls-compile-block body t)))
+ (if (member 'used (binding-declarations b))
+ (js!selfcall
+ "try {" *newline*
+ (indent cbody)
+ "}" *newline*
+ "catch (cf){" *newline*
+ " if (cf.type == 'block' && cf.id == " tr ")" *newline*
+ (if *multiple-value-p*
+ " return values.apply(this, forcemv(cf.values));"
+ " return cf.values;")
+ *newline*
+ " else" *newline*
+ " throw cf;" *newline*
+ "}" *newline*)
+ (js!selfcall cbody)))))
+
+(define-compilation return-from (name &optional value)
+ (let* ((b (lookup-in-lexenv name *environment* 'block))
+ (multiple-value-p (member 'multiple-value (binding-declarations b))))
+ (when (null b)
+ (error (concat "Unknown block `" (symbol-name name) "'.")))
+ (push-binding-declaration 'used b)
+ (js!selfcall
+ (when multiple-value-p (code "var values = mv;" *newline*))
+ "throw ({"
+ "type: 'block', "
+ "id: " (binding-value b) ", "
+ "values: " (ls-compile value multiple-value-p) ", "
+ "message: 'Return from unknown block " (symbol-name name) ".'"
+ "})")))
+
+(define-compilation catch (id &rest body)
+ (js!selfcall
+ "var id = " (ls-compile id) ";" *newline*
+ "try {" *newline*
+ (indent (ls-compile-block body t)) *newline*
+ "}" *newline*
+ "catch (cf){" *newline*
+ " if (cf.type == 'catch' && cf.id == id)" *newline*
+ (if *multiple-value-p*
+ " return values.apply(this, forcemv(cf.values));"
+ " return pv.apply(this, forcemv(cf.values));")
+ *newline*
+ " else" *newline*
+ " throw cf;" *newline*
+ "}" *newline*))
+
+(define-compilation throw (id value)
+ (js!selfcall
+ "var values = mv;" *newline*
+ "throw ({"
+ "type: 'catch', "
+ "id: " (ls-compile id) ", "
+ "values: " (ls-compile value t) ", "
+ "message: 'Throw uncatched.'"
+ "})"))
+
+
+(defvar *tagbody-counter* 0)
+(defvar *go-tag-counter* 0)
+
+(defun go-tag-p (x)
+ (or (integerp x) (symbolp x)))
+
+(defun declare-tagbody-tags (tbidx body)
+ (let ((bindings
+ (mapcar (lambda (label)
+ (let ((tagidx (integer-to-string (incf *go-tag-counter*))))
+ (make-binding label 'gotag (list tbidx tagidx))))
+ (remove-if-not #'go-tag-p body))))
+ (extend-lexenv bindings *environment* 'gotag)))
+
+(define-compilation tagbody (&rest body)
+ ;; Ignore the tagbody if it does not contain any go-tag. We do this
+ ;; because 1) it is easy and 2) many built-in forms expand to a
+ ;; implicit tagbody, so we save some space.
+ (unless (some #'go-tag-p body)
+ (return-from tagbody (ls-compile `(progn ,@body nil))))
+ ;; The translation assumes the first form in BODY is a label
+ (unless (go-tag-p (car body))
+ (push (gensym "START") body))
+ ;; Tagbody compilation
+ (let ((tbidx *tagbody-counter*))
+ (let ((*environment* (declare-tagbody-tags tbidx body))
+ initag)
+ (let ((b (lookup-in-lexenv (first body) *environment* 'gotag)))
+ (setq initag (second (binding-value b))))
+ (js!selfcall
+ "var tagbody_" tbidx " = " initag ";" *newline*
+ "tbloop:" *newline*
+ "while (true) {" *newline*
+ (indent "try {" *newline*
+ (indent (let ((content ""))
+ (code "switch(tagbody_" tbidx "){" *newline*
+ "case " initag ":" *newline*
+ (dolist (form (cdr body) content)
+ (concatf content
+ (if (not (go-tag-p form))
+ (indent (ls-compile form) ";" *newline*)
+ (let ((b (lookup-in-lexenv form *environment* 'gotag)))
+ (code "case " (second (binding-value b)) ":" *newline*)))))
+ "default:" *newline*
+ " break tbloop;" *newline*
+ "}" *newline*)))
+ "}" *newline*
+ "catch (jump) {" *newline*
+ " if (jump.type == 'tagbody' && jump.id == " tbidx ")" *newline*
+ " tagbody_" tbidx " = jump.label;" *newline*
+ " else" *newline*
+ " throw(jump);" *newline*
+ "}" *newline*)
+ "}" *newline*
+ "return " (ls-compile nil) ";" *newline*))))
+
+(define-compilation go (label)
+ (let ((b (lookup-in-lexenv label *environment* 'gotag))
+ (n (cond
+ ((symbolp label) (symbol-name label))
+ ((integerp label) (integer-to-string label)))))
+ (when (null b)
+ (error (concat "Unknown tag `" n "'.")))
+ (js!selfcall
+ "throw ({"
+ "type: 'tagbody', "
+ "id: " (first (binding-value b)) ", "
+ "label: " (second (binding-value b)) ", "
+ "message: 'Attempt to GO to non-existing tag " n "'"
+ "})" *newline*)))
+
+(define-compilation unwind-protect (form &rest clean-up)
+ (js!selfcall
+ "var ret = " (ls-compile nil) ";" *newline*
+ "try {" *newline*
+ (indent "ret = " (ls-compile form) ";" *newline*)
+ "} finally {" *newline*
+ (indent (ls-compile-block clean-up))
+ "}" *newline*
+ "return ret;" *newline*))
+
+(define-compilation multiple-value-call (func-form &rest forms)
+ (js!selfcall
+ "var func = " (ls-compile func-form) ";" *newline*
+ "var args = [" (if *multiple-value-p* "values" "pv") "];" *newline*
+ "return "
+ (js!selfcall
+ "var values = mv;" *newline*
+ "var vs;" *newline*
+ (mapconcat (lambda (form)
+ (code "vs = " (ls-compile form t) ";" *newline*
+ "if (typeof vs === 'object' && 'multiple-value' in vs)" *newline*
+ (indent "args = args.concat(vs);" *newline*)
+ "else" *newline*
+ (indent "args.push(vs);" *newline*)))
+ forms)
+ "return func.apply(window, args);" *newline*) ";" *newline*))
+
+(define-compilation multiple-value-prog1 (first-form &rest forms)
+ (js!selfcall
+ "var args = " (ls-compile first-form *multiple-value-p*) ";" *newline*
+ (ls-compile-block forms)
+ "return args;" *newline*))
+
+
+;;; Backquote implementation.
+;;;
+;;; Author: Guy L. Steele Jr. Date: 27 December 1985
+;;; Tested under Symbolics Common Lisp and Lucid Common Lisp.
+;;; This software is in the public domain.
+
+;;; The following are unique tokens used during processing.
+;;; They need not be symbols; they need not even be atoms.
+(defvar *comma* 'unquote)
+(defvar *comma-atsign* 'unquote-splicing)
+
+(defvar *bq-list* (make-symbol "BQ-LIST"))
+(defvar *bq-append* (make-symbol "BQ-APPEND"))
+(defvar *bq-list** (make-symbol "BQ-LIST*"))
+(defvar *bq-nconc* (make-symbol "BQ-NCONC"))
+(defvar *bq-clobberable* (make-symbol "BQ-CLOBBERABLE"))
+(defvar *bq-quote* (make-symbol "BQ-QUOTE"))
+(defvar *bq-quote-nil* (list *bq-quote* nil))
+
+;;; BACKQUOTE is an ordinary macro (not a read-macro) that processes
+;;; the expression foo, looking for occurrences of #:COMMA,
+;;; #:COMMA-ATSIGN, and #:COMMA-DOT. It constructs code in strict
+;;; accordance with the rules on pages 349-350 of the first edition
+;;; (pages 528-529 of this second edition). It then optionally
+;;; applies a code simplifier.
+
+;;; If the value of *BQ-SIMPLIFY* is non-NIL, then BACKQUOTE
+;;; processing applies the code simplifier. If the value is NIL,
+;;; then the code resulting from BACKQUOTE is exactly that
+;;; specified by the official rules.
+(defparameter *bq-simplify* t)
+
+(defmacro backquote (x)
+ (bq-completely-process x))
+
+;;; Backquote processing proceeds in three stages:
+;;;
+;;; (1) BQ-PROCESS applies the rules to remove occurrences of
+;;; #:COMMA, #:COMMA-ATSIGN, and #:COMMA-DOT corresponding to
+;;; this level of BACKQUOTE. (It also causes embedded calls to
+;;; BACKQUOTE to be expanded so that nesting is properly handled.)
+;;; Code is produced that is expressed in terms of functions
+;;; #:BQ-LIST, #:BQ-APPEND, and #:BQ-CLOBBERABLE. This is done
+;;; so that the simplifier will simplify only list construction
+;;; functions actually generated by BACKQUOTE and will not involve
+;;; any user code in the simplification. #:BQ-LIST means LIST,
+;;; #:BQ-APPEND means APPEND, and #:BQ-CLOBBERABLE means IDENTITY
+;;; but indicates places where "%." was used and where NCONC may
+;;; therefore be introduced by the simplifier for efficiency.
+;;;
+;;; (2) BQ-SIMPLIFY, if used, rewrites the code produced by
+;;; BQ-PROCESS to produce equivalent but faster code. The
+;;; additional functions #:BQ-LIST* and #:BQ-NCONC may be
+;;; introduced into the code.
+;;;
+;;; (3) BQ-REMOVE-TOKENS goes through the code and replaces
+;;; #:BQ-LIST with LIST, #:BQ-APPEND with APPEND, and so on.
+;;; #:BQ-CLOBBERABLE is simply eliminated (a call to it being
+;;; replaced by its argument). #:BQ-LIST* is replaced by either
+;;; LIST* or CONS (the latter is used in the two-argument case,
+;;; purely to make the resulting code a tad more readable).
+
+(defun bq-completely-process (x)
+ (let ((raw-result (bq-process x)))
+ (bq-remove-tokens (if *bq-simplify*
+ (bq-simplify raw-result)
+ raw-result))))
+
+(defun bq-process (x)
+ (cond ((atom x)
+ (list *bq-quote* x))
+ ((eq (car x) 'backquote)
+ (bq-process (bq-completely-process (cadr x))))
+ ((eq (car x) *comma*) (cadr x))
+ ((eq (car x) *comma-atsign*)
+ ;; (error ",@~S after `" (cadr x))
+ (error "ill-formed"))
+ ;; ((eq (car x) *comma-dot*)
+ ;; ;; (error ",.~S after `" (cadr x))
+ ;; (error "ill-formed"))
+ (t (do ((p x (cdr p))
+ (q '() (cons (bracket (car p)) q)))
+ ((atom p)
+ (cons *bq-append*
+ (nreconc q (list (list *bq-quote* p)))))
+ (when (eq (car p) *comma*)
+ (unless (null (cddr p))
+ ;; (error "Malformed ,~S" p)
+ (error "Malformed"))
+ (return (cons *bq-append*
+ (nreconc q (list (cadr p))))))
+ (when (eq (car p) *comma-atsign*)
+ ;; (error "Dotted ,@~S" p)
+ (error "Dotted"))
+ ;; (when (eq (car p) *comma-dot*)
+ ;; ;; (error "Dotted ,.~S" p)
+ ;; (error "Dotted"))
+ ))))
+
+;;; This implements the bracket operator of the formal rules.
+(defun bracket (x)
+ (cond ((atom x)
+ (list *bq-list* (bq-process x)))
+ ((eq (car x) *comma*)
+ (list *bq-list* (cadr x)))
+ ((eq (car x) *comma-atsign*)
+ (cadr x))
+ ;; ((eq (car x) *comma-dot*)
+ ;; (list *bq-clobberable* (cadr x)))
+ (t (list *bq-list* (bq-process x)))))
+
+;;; This auxiliary function is like MAPCAR but has two extra
+;;; purposes: (1) it handles dotted lists; (2) it tries to make
+;;; the result share with the argument x as much as possible.
+(defun maptree (fn x)
+ (if (atom x)
+ (funcall fn x)
+ (let ((a (funcall fn (car x)))
+ (d (maptree fn (cdr x))))
+ (if (and (eql a (car x)) (eql d (cdr x)))
+ x
+ (cons a d)))))
+
+;;; This predicate is true of a form that when read looked
+;;; like %@foo or %.foo.
+(defun bq-splicing-frob (x)
+ (and (consp x)
+ (or (eq (car x) *comma-atsign*)
+ ;; (eq (car x) *comma-dot*)
+ )))
+
+;;; This predicate is true of a form that when read
+;;; looked like %@foo or %.foo or just plain %foo.
+(defun bq-frob (x)
+ (and (consp x)
+ (or (eq (car x) *comma*)
+ (eq (car x) *comma-atsign*)
+ ;; (eq (car x) *comma-dot*)
+ )))
+
+;;; The simplifier essentially looks for calls to #:BQ-APPEND and
+;;; tries to simplify them. The arguments to #:BQ-APPEND are
+;;; processed from right to left, building up a replacement form.
+;;; At each step a number of special cases are handled that,
+;;; loosely speaking, look like this:
+;;;
+;;; (APPEND (LIST a b c) foo) => (LIST* a b c foo)
+;;; provided a, b, c are not splicing frobs
+;;; (APPEND (LIST* a b c) foo) => (LIST* a b (APPEND c foo))
+;;; provided a, b, c are not splicing frobs
+;;; (APPEND (QUOTE (x)) foo) => (LIST* (QUOTE x) foo)
+;;; (APPEND (CLOBBERABLE x) foo) => (NCONC x foo)
+(defun bq-simplify (x)
+ (if (atom x)
+ x
+ (let ((x (if (eq (car x) *bq-quote*)
+ x
+ (maptree #'bq-simplify x))))
+ (if (not (eq (car x) *bq-append*))
+ x
+ (bq-simplify-args x)))))
+
+(defun bq-simplify-args (x)
+ (do ((args (reverse (cdr x)) (cdr args))
+ (result
+ nil
+ (cond ((atom (car args))
+ (bq-attach-append *bq-append* (car args) result))
+ ((and (eq (caar args) *bq-list*)
+ (notany #'bq-splicing-frob (cdar args)))
+ (bq-attach-conses (cdar args) result))
+ ((and (eq (caar args) *bq-list**)
+ (notany #'bq-splicing-frob (cdar args)))
+ (bq-attach-conses
+ (reverse (cdr (reverse (cdar args))))
+ (bq-attach-append *bq-append*
+ (car (last (car args)))
+ result)))
+ ((and (eq (caar args) *bq-quote*)
+ (consp (cadar args))
+ (not (bq-frob (cadar args)))
+ (null (cddar args)))
+ (bq-attach-conses (list (list *bq-quote*
+ (caadar args)))
+ result))
+ ((eq (caar args) *bq-clobberable*)
+ (bq-attach-append *bq-nconc* (cadar args) result))
+ (t (bq-attach-append *bq-append*
+ (car args)
+ result)))))
+ ((null args) result)))
+
+(defun null-or-quoted (x)
+ (or (null x) (and (consp x) (eq (car x) *bq-quote*))))
+
+;;; When BQ-ATTACH-APPEND is called, the OP should be #:BQ-APPEND
+;;; or #:BQ-NCONC. This produces a form (op item result) but
+;;; some simplifications are done on the fly:
+;;;
+;;; (op '(a b c) '(d e f g)) => '(a b c d e f g)
+;;; (op item 'nil) => item, provided item is not a splicable frob
+;;; (op item 'nil) => (op item), if item is a splicable frob
+;;; (op item (op a b c)) => (op item a b c)
+(defun bq-attach-append (op item result)
+ (cond ((and (null-or-quoted item) (null-or-quoted result))
+ (list *bq-quote* (append (cadr item) (cadr result))))
+ ((or (null result) (equal result *bq-quote-nil*))
+ (if (bq-splicing-frob item) (list op item) item))
+ ((and (consp result) (eq (car result) op))
+ (list* (car result) item (cdr result)))
+ (t (list op item result))))
+
+;;; The effect of BQ-ATTACH-CONSES is to produce a form as if by
+;;; `(LIST* ,@items ,result) but some simplifications are done
+;;; on the fly.
+;;;
+;;; (LIST* 'a 'b 'c 'd) => '(a b c . d)
+;;; (LIST* a b c 'nil) => (LIST a b c)
+;;; (LIST* a b c (LIST* d e f g)) => (LIST* a b c d e f g)
+;;; (LIST* a b c (LIST d e f g)) => (LIST a b c d e f g)
+(defun bq-attach-conses (items result)
+ (cond ((and (every #'null-or-quoted items)
+ (null-or-quoted result))
+ (list *bq-quote*
+ (append (mapcar #'cadr items) (cadr result))))
+ ((or (null result) (equal result *bq-quote-nil*))
+ (cons *bq-list* items))
+ ((and (consp result)
+ (or (eq (car result) *bq-list*)
+ (eq (car result) *bq-list**)))
+ (cons (car result) (append items (cdr result))))
+ (t (cons *bq-list** (append items (list result))))))
+
+;;; Removes funny tokens and changes (#:BQ-LIST* a b) into
+;;; (CONS a b) instead of (LIST* a b), purely for readability.
+(defun bq-remove-tokens (x)
+ (cond ((eq x *bq-list*) 'list)
+ ((eq x *bq-append*) 'append)
+ ((eq x *bq-nconc*) 'nconc)
+ ((eq x *bq-list**) 'list*)
+ ((eq x *bq-quote*) 'quote)
+ ((atom x) x)
+ ((eq (car x) *bq-clobberable*)
+ (bq-remove-tokens (cadr x)))
+ ((and (eq (car x) *bq-list**)
+ (consp (cddr x))
+ (null (cdddr x)))
+ (cons 'cons (maptree #'bq-remove-tokens (cdr x))))
+ (t (maptree #'bq-remove-tokens x))))