(in-package "PARENSCRIPT")
+(defmacro with-local-macro-environment ((var env) &body body)
+ `(let* ((,var (make-macro-dictionary))
+ (,env (cons ,var ,env)))
+ ,@body))
+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; literals
(defmacro defpsliteral (name string)
`(progn
- (add-ps-literal ',name)
+ (add-ps-reserved-symbol ',name)
(define-ps-special-form ,name ()
(list 'js:literal ,string))))
(macrolet ((def-for-literal (name printer)
`(progn
- (add-ps-literal ',name)
+ (add-ps-reserved-symbol ',name)
(define-ps-special-form ,name (&optional label)
(list ',printer label)))))
(def-for-literal break js:break)
(def-for-literal continue js:continue))
-(defpsmacro quote (x)
- (typecase x
- (cons (cons 'array (mapcar (lambda (x) (when x `',x)) x)))
- (null '(array))
- (symbol (string-downcase x))
- (number x)
- (string x)))
+(define-ps-special-form quote (x)
+ (compile-parenscript-form
+ (typecase x
+ (cons `(array ,@(mapcar (lambda (x) (when x `',x)) x)))
+ (null '(array))
+ (keyword x)
+ (symbol (symbol-to-js-string x))
+ (number x)
+ (string x))
+ :expecting expecting))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; unary operators
(spacep (if (listp op) (second op) nil)))
`(define-ps-special-form ,op (x)
(list 'js:unary-operator ',op
- (compile-parenscript-form x :expecting :expression)
+ (compile-parenscript-form (ps-macroexpand x) :expecting :expression)
:prefix t :space ,spacep))))
ops))))
(def-unary-ops ~ ! (new t) (delete t) (void t) (typeof t)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; statements
(define-ps-special-form return (&optional value)
- `(js:return ,(compile-parenscript-form value :expecting :expression)))
+ `(js:return ,(compile-parenscript-form (ps-macroexpand value) :expecting :expression)))
(define-ps-special-form throw (value)
- `(js:throw ,(compile-parenscript-form value :expecting :expression)))
+ `(js:throw ,(compile-parenscript-form (ps-macroexpand value) :expecting :expression)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; arrays
(define-ps-special-form array (&rest values)
- `(js:array ,@(mapcar (lambda (form) (compile-parenscript-form form :expecting :expression))
+ `(js:array ,@(mapcar (lambda (form) (compile-parenscript-form (ps-macroexpand form) :expecting :expression))
values)))
(define-ps-special-form aref (array &rest coords)
- `(js:aref ,(compile-parenscript-form array :expecting :expression)
+ `(js:aref ,(compile-parenscript-form (ps-macroexpand array) :expecting :expression)
,(mapcar (lambda (form)
- (compile-parenscript-form form :expecting :expression))
+ (compile-parenscript-form (ps-macroexpand form) :expecting :expression))
coords)))
(defpsmacro list (&rest values)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; operators
(define-ps-special-form incf (x &optional (delta 1))
- (if (eql delta 1)
- `(js:unary-operator js:++ ,(compile-parenscript-form x :expecting :expression) :prefix t)
- `(js:operator js:+= ,(compile-parenscript-form x :expecting :expression)
- ,(compile-parenscript-form delta :expecting :expression))))
+ (let ((x (ps-macroexpand x))
+ (delta (ps-macroexpand delta)))
+ (if (eql delta 1)
+ `(js:unary-operator js:++ ,(compile-parenscript-form x :expecting :expression) :prefix t)
+ `(js:operator js:+= ,(compile-parenscript-form x :expecting :expression)
+ ,(compile-parenscript-form delta :expecting :expression)))))
(define-ps-special-form decf (x &optional (delta 1))
- (if (eql delta 1)
- `(js:unary-operator js:-- ,(compile-parenscript-form x :expecting :expression) :prefix t)
- `(js:operator js:-= ,(compile-parenscript-form x :expecting :expression)
- ,(compile-parenscript-form delta :expecting :expression))))
+ (let ((x (ps-macroexpand x))
+ (delta (ps-macroexpand delta)))
+ (if (eql delta 1)
+ `(js:unary-operator js:-- ,(compile-parenscript-form x :expecting :expression) :prefix t)
+ `(js:operator js:-= ,(compile-parenscript-form x :expecting :expression)
+ ,(compile-parenscript-form delta :expecting :expression)))))
(define-ps-special-form - (first &rest rest)
- (if rest
- `(js:operator js:- ,@(mapcar (lambda (val) (compile-parenscript-form val :expecting :expression))
- (cons first rest)))
- `(js:unary-operator js:- ,(compile-parenscript-form first :expecting :expression) :prefix t)))
+ (let ((first (ps-macroexpand first))
+ (rest (mapcar #'ps-macroexpand rest)))
+ (if rest
+ `(js:operator js:- ,@(mapcar (lambda (val) (compile-parenscript-form val :expecting :expression))
+ (cons first rest)))
+ `(js:unary-operator js:- ,(compile-parenscript-form first :expecting :expression) :prefix t))))
(define-ps-special-form not (x)
- (let ((form (compile-parenscript-form x :expecting :expression))
+ (let ((form (compile-parenscript-form (ps-macroexpand x) :expecting :expression))
inverse-op)
(if (and (eq (car form) 'js:operator)
(= (length (cddr form)) 2)
(eq 'js:literal (car form)))))
(define-ps-special-form progn (&rest body)
- (if (and (eq expecting :expression) (= 1 (length body)))
- (compile-parenscript-form (car body) :expecting :expression)
- `(,(if (eq expecting :expression) 'js:|,| 'js:block)
- ,@(let* ((block (flatten-blocks (remove nil (mapcar (lambda (form)
- (compile-parenscript-form form :expecting expecting))
- body)))))
- (append (remove-if #'constant-literal-form-p (butlast block)) (last block))))))
+ (let ((body (mapcar #'ps-macroexpand body)))
+ (if (and (eq expecting :expression) (= 1 (length body)))
+ (compile-parenscript-form (car body) :expecting :expression)
+ `(,(if (eq expecting :expression) 'js:|,| 'js:block)
+ ,@(let* ((block (flatten-blocks (remove nil (mapcar (lambda (form)
+ (compile-parenscript-form form :expecting expecting))
+ body)))))
+ (append (remove-if #'constant-literal-form-p (butlast block)) (last block)))))))
(define-ps-special-form cond (&rest clauses)
(ecase expecting
(define-ps-special-form if (test then &optional else)
(ecase expecting
- (:statement `(js:if ,(compile-parenscript-form test :expecting :expression)
+ (:statement `(js:if ,(compile-parenscript-form (ps-macroexpand test) :expecting :expression)
,(compile-parenscript-form `(progn ,then))
,@(when else `(:else ,(compile-parenscript-form `(progn ,else))))))
- (:expression `(js:? ,(compile-parenscript-form test :expecting :expression)
- ,(compile-parenscript-form then :expecting :expression)
- ,(compile-parenscript-form else :expecting :expression)))))
+ (:expression `(js:? ,(compile-parenscript-form (ps-macroexpand test) :expecting :expression)
+ ,(compile-parenscript-form (ps-macroexpand then) :expecting :expression)
+ ,(compile-parenscript-form (ps-macroexpand else) :expecting :expression)))))
(define-ps-special-form switch (test-expr &rest clauses)
`(js:switch ,(compile-parenscript-form test-expr :expecting :expression)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; function definition
+
+(defvar *vars-bound-in-enclosing-lexical-scopes* ())
+
(defun compile-function-definition (args body)
- (list (mapcar (lambda (arg) (compile-parenscript-form arg :expecting :symbol)) args)
- (let ((*enclosing-lexical-block-declarations* ()))
- ;; the first compilation will produce a list of variables we need to declare in the function body
- (compile-parenscript-form `(progn ,@body) :expecting :statement)
- ;; now declare and compile
- (compile-parenscript-form `(progn
- ,@(mapcar (lambda (var) `(var ,var)) *enclosing-lexical-block-declarations*)
- ,@body)
- :expecting :statement))))
+ (let ((args (mapcar (lambda (arg) (compile-parenscript-form arg :expecting :symbol)) args)))
+ (list args
+ (let* ((*enclosing-lexical-block-declarations* ())
+ (*vars-bound-in-enclosing-lexical-scopes* (append args
+ *vars-bound-in-enclosing-lexical-scopes*))
+ (body (compile-parenscript-form `(progn ,@body)))
+ (var-decls (compile-parenscript-form
+ `(progn ,@(mapcar (lambda (var) `(var ,var)) *enclosing-lexical-block-declarations*)))))
+ `(js:block ,@(cdr var-decls) ,@(cdr body))))))
(define-ps-special-form %js-lambda (args &rest body)
`(js:lambda ,@(compile-function-definition args body)))
(values (if (symbolp spec) spec (first spec))
(when (listp spec) (second spec))))
-(defpsmacro defaultf (place value)
- `(when (=== ,place undefined)
- (setf ,place ,value)))
+(defpsmacro defaultf (name value suppl)
+ `(progn
+ ,@(when suppl `((var ,suppl t)))
+ (when (=== ,name undefined)
+ (setf ,name ,value ,@(when suppl (list suppl nil))))))
(defun parse-extended-function (lambda-list body &optional name)
"Returns two values: the effective arguments and body for a function with
;; list of variable names, and you have access to the arguments variable inside the function:
;; * standard variables are the mapped directly into the js-lambda list
;; * optional variables' variable names are mapped directly into the lambda list,
- ;; and for each optional variable with name v and default value d, a form is produced
- ;; (defaultf v d)
+ ;; and for each optional variable with name v, default value d, and
+ ;; supplied-p parameter s, a form is produced (defaultf v d s)
;; * keyword variables are not included in the js-lambda list, but instead are
;; obtained from the magic js ARGUMENTS pseudo-array. Code assigning values to
- ;; keyword vars is prepended to the body of the function.
+ ;; keyword vars is prepended to the body of the function. Defaults and supplied-p
+ ;; are handled using the same mechanism as with optional vars.
(declare (ignore name))
(multiple-value-bind (requireds optionals rest? rest keys? keys allow? aux? aux
more? more-context more-count key-object)
(mapcar #'parse-optional-spec optionals))))
(opt-forms
(mapcar #'(lambda (opt-spec)
- (multiple-value-bind (var val) (parse-optional-spec opt-spec)
- `(defaultf ,var ,val)))
+ (multiple-value-bind (var val suppl)
+ (parse-optional-spec opt-spec)
+ `(defaultf ,var ,val ,suppl)))
optionals))
(key-forms
(when keys?
(with-ps-gensyms (n)
(let ((decls nil) (assigns nil) (defaults nil))
(mapc (lambda (k)
- (multiple-value-bind (var init-form keyword-str)
+ (multiple-value-bind (var init-form keyword-str suppl)
(parse-key-spec k)
(push `(var ,var) decls)
(push `(,keyword-str (setf ,var (aref arguments (1+ ,n)))) assigns)
- (push (list 'defaultf var init-form) defaults)))
+ (push (list 'defaultf var init-form suppl) defaults)))
(reverse keys))
`(,@decls
(loop :for ,n :from ,(length requireds)
`(%js-defun ,name ,effective-args
,@effective-body)))
-(defvar *defun-setf-name-prefix* "__setf_")
-
-(defpsmacro defun-setf (setf-name lambda-list &body body)
- (let ((mangled-function-name (intern (concatenate 'string *defun-setf-name-prefix* (symbol-name (second setf-name)))
- (symbol-package (second setf-name))))
- (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords))))
- `(progn (defsetf ,(second setf-name) ,(cdr lambda-list) (store-var)
- `(,',mangled-function-name ,store-var ,@(list ,@function-args)))
- (defun ,mangled-function-name ,lambda-list ,@body))))
-
(defpsmacro lambda (lambda-list &body body)
"An extended defun macro that allows cool things like keyword arguments.
lambda-list::=
`(%js-lambda ,effective-args
,@effective-body)))
-(defpsmacro flet (fn-defs &rest body)
- (flet ((process-fn-def (def)
- `(var ,(car def) (lambda ,@(cdr def)))))
- `(progn ,@(mapcar #'process-fn-def fn-defs) ,@body)))
+(define-ps-special-form flet (fn-defs &rest body)
+ (let ((fn-renames (make-macro-dictionary)))
+ (loop for (fn-name . def) in fn-defs do
+ (setf (gethash fn-name fn-renames) (ps-gensym fn-name)))
+ (let ((fn-defs (compile-parenscript-form
+ `(progn ,@(loop for (fn-name . def) in fn-defs collect
+ `(var ,(gethash fn-name fn-renames) (lambda ,@def))))
+ :expecting expecting))
+ (*ps-local-function-names* (cons fn-renames *ps-local-function-names*)))
+ (append fn-defs (cdr (compile-parenscript-form `(progn ,@body) :expecting expecting))))))
+
+(define-ps-special-form labels (fn-defs &rest body)
+ (with-local-macro-environment (local-fn-renames *ps-local-function-names*)
+ (loop for (fn-name . def) in fn-defs do
+ (setf (gethash fn-name local-fn-renames) (ps-gensym fn-name)))
+ (compile-parenscript-form
+ `(progn ,@(loop for (fn-name . def) in fn-defs collect
+ `(var ,(gethash fn-name local-fn-renames) (lambda ,@def)))
+ ,@body)
+ :expecting expecting)))
+
+(define-ps-special-form function (fn-name)
+ (compile-parenscript-form (maybe-rename-local-function fn-name) :expecting expecting))
-(defpsmacro labels (fn-defs &rest body)
- (flet ((process-fn-def (def)
- `(var ,(car def) (defun ,(car def) ,@(cdr def)))))
- `(progn ,@(mapcar #'process-fn-def fn-defs) ,@body)))
+(defvar *defun-setf-name-prefix* "__setf_")
+
+(defpsmacro defun-setf (setf-name lambda-list &body body)
+ (let ((mangled-function-name (intern (concatenate 'string *defun-setf-name-prefix* (symbol-name (second setf-name)))
+ (symbol-package (second setf-name))))
+ (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords))))
+ (ps* `(defsetf ,(second setf-name) ,(cdr lambda-list) (store-var)
+ `(,',mangled-function-name ,store-var ,@(list ,@function-args))))
+ `(defun ,mangled-function-name ,lambda-list ,@body)))
(defpsmacro defsetf-long (access-fn lambda-list (store-var) form)
- (setf (get-macro-spec access-fn *ps-setf-expanders*)
+ (setf (gethash access-fn *ps-setf-expanders*)
(compile nil
(let ((var-bindings (ordered-set-difference lambda-list lambda-list-keywords)))
`(lambda (access-fn-args store-form)
(defpsmacro defsetf-short (access-fn update-fn &optional docstring)
(declare (ignore docstring))
- (setf (get-macro-spec access-fn *ps-setf-expanders*)
+ (setf (gethash access-fn *ps-setf-expanders*)
(lambda (access-fn-args store-form)
`(,update-fn ,@access-fn-args ,store-form)))
nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; macros
-(defmacro with-temp-macro-environment ((var) &body body)
- `(let* ((,var (make-macro-env-dictionary))
- (*ps-macro-env* (cons ,var *ps-macro-env*)))
- ,@body))
-
(define-ps-special-form macrolet (macros &body body)
- (with-temp-macro-environment (macro-env-dict)
+ (with-local-macro-environment (local-macro-dict *ps-macro-env*)
(dolist (macro macros)
(destructuring-bind (name arglist &body body)
macro
- (setf (get-macro-spec name macro-env-dict)
- (cons nil (eval (make-ps-macro-function arglist body))))))
- (compile-parenscript-form `(progn ,@body))))
+ (setf (gethash name local-macro-dict) (eval (make-ps-macro-function arglist body)))))
+ (compile-parenscript-form `(progn ,@body) :expecting expecting)))
(define-ps-special-form symbol-macrolet (symbol-macros &body body)
- (with-temp-macro-environment (macro-env-dict)
- (dolist (macro symbol-macros)
- (destructuring-bind (name expansion)
- macro
- (setf (get-macro-spec name macro-env-dict)
- (cons t (lambda (x) (declare (ignore x)) expansion)))))
- (compile-parenscript-form `(progn ,@body))))
+ (with-local-macro-environment (local-macro-dict *ps-symbol-macro-env*)
+ (let (local-var-bindings)
+ (dolist (macro symbol-macros)
+ (destructuring-bind (name expansion)
+ macro
+ (setf (gethash name local-macro-dict) (lambda (x) (declare (ignore x)) expansion))
+ (push name local-var-bindings)))
+ (let ((*vars-bound-in-enclosing-lexical-scopes* (append local-var-bindings
+ *vars-bound-in-enclosing-lexical-scopes*)))
+ (compile-parenscript-form `(progn ,@body) :expecting expecting)))))
(define-ps-special-form defmacro (name args &body body) ;; should this be a macro?
(eval `(defpsmacro ,name ,args ,@body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; objects
-(add-ps-literal '{})
+(add-ps-reserved-symbol '{})
(define-ps-symbol-macro {} (create))
(define-ps-special-form create (&rest arrows)
- `(js:object ,@(loop for (key-expr val-expr) on arrows by #'cddr collecting
- (let ((key (compile-parenscript-form key-expr :expecting :expression)))
- (when (keywordp key)
- (setf key `(js:variable ,key)))
- (assert (or (stringp key)
- (numberp key)
- (and (listp key)
- (or (eq 'js:variable (car key))
- (eq 'quote (car key)))))
- ()
- "Slot key ~s is not one of js-variable, keyword, string or number." key)
- (cons key (compile-parenscript-form val-expr :expecting :expression))))))
-
-(define-ps-special-form %js-slot-value (obj slot)
- `(js:slot-value ,(compile-parenscript-form obj :expecting :expression)
- ,(if (and (listp slot) (eq 'quote (car slot)))
- (second slot) ;; assume we're quoting a symbol
- (compile-parenscript-form slot))))
-
+ `(js:object
+ ,@(loop for (key-expr val-expr) on arrows by #'cddr collecting
+ (let ((compiled-key (compile-parenscript-form (ps-macroexpand key-expr)
+ :expecting :expression)))
+ (assert (or (stringp compiled-key)
+ (numberp compiled-key)
+ (keywordp compiled-key)
+ (and (listp compiled-key)
+ (eq 'js:variable (car compiled-key))))
+ ()
+ "Slot key ~s is not one of js-variable, keyword, string or number."
+ compiled-key)
+ (let ((key (aif (ps-reserved-symbol-p (if (listp compiled-key)
+ (second compiled-key)
+ compiled-key))
+ it
+ compiled-key)))
+ (cons key (compile-parenscript-form (ps-macroexpand val-expr)
+ :expecting :expression)))))))
+
(define-ps-special-form instanceof (value type)
`(js:instanceof ,(compile-parenscript-form value :expecting :expression)
,(compile-parenscript-form type :expecting :expression)))
+(define-ps-special-form %js-slot-value (obj slot)
+ (let ((slot (ps-macroexpand slot)))
+ `(js:slot-value ,(compile-parenscript-form (ps-macroexpand obj) :expecting :expression)
+ ,(let ((slot (if (and (listp slot) (eq 'quote (car slot)))
+ (second slot) ;; assume we're quoting a symbol
+ (compile-parenscript-form slot))))
+ (if (and (symbolp slot)
+ (ps-reserved-symbol-p slot))
+ (symbol-name-to-js-string slot)
+ slot)))))
+
(defpsmacro slot-value (obj &rest slots)
(if (null (rest slots))
`(%js-slot-value ,obj ,(first slots))
(t nil)))
(define-ps-special-form setf1% (lhs rhs)
- (let ((lhs (compile-parenscript-form lhs :expecting :expression))
- (rhs (compile-parenscript-form rhs :expecting :expression)))
+ (let ((lhs (compile-parenscript-form (ps-macroexpand lhs) :expecting :expression))
+ (rhs (compile-parenscript-form (ps-macroexpand rhs) :expecting :expression)))
(if (and (listp rhs)
(eq 'js:operator (car rhs))
(member (cadr rhs) '(+ *))
`(js:= ,lhs ,rhs))))
(defpsmacro setf (&rest args)
- (flet ((process-setf-clause (place value-form)
- (if (and (listp place) (get-macro-spec (car place) *ps-setf-expanders*))
- (funcall (get-macro-spec (car place) *ps-setf-expanders*) (cdr place) value-form)
- (let ((exp-place (ps-macroexpand place)))
- (if (and (listp exp-place) (get-macro-spec (car exp-place) *ps-setf-expanders*))
- (funcall (get-macro-spec (car exp-place) *ps-setf-expanders*) (cdr exp-place) value-form)
- `(setf1% ,exp-place ,value-form))))))
- (assert (evenp (length args)) ()
- "~s does not have an even number of arguments." (cons 'setf args))
- `(progn ,@(loop for (place value) on args by #'cddr collect (process-setf-clause place value)))))
+ (assert (evenp (length args)) ()
+ "~s does not have an even number of arguments." `(setf ,args))
+ `(progn ,@(loop for (place value) on args by #'cddr collect
+ (let ((place (ps-macroexpand place)))
+ (aif (and (listp place) (gethash (car place) *ps-setf-expanders*))
+ (funcall it (cdr place) value)
+ `(setf1% ,place ,value))))))
(defpsmacro psetf (&rest args)
- (let ((vars (loop for x in args by #'cddr collect x))
+ (let ((places (loop for x in args by #'cddr collect x))
(vals (loop for x in (cdr args) by #'cddr collect x)))
- (let ((gensyms (mapcar (lambda (x) (declare (ignore x)) (ps-gensym)) vars)))
- `(simple-let* ,(mapcar #'list gensyms vals)
- (setf ,@(mapcan #'list vars gensyms))))))
+ (let ((gensyms (mapcar (lambda (x) (declare (ignore x)) (ps-gensym)) places)))
+ `(let ,(mapcar #'list gensyms vals)
+ (setf ,@(mapcan #'list places gensyms))))))
(defun check-setq-args (args)
(let ((vars (loop for x in args by #'cddr collect x)))
(define-ps-special-form var (name &optional (value (values) value-provided?) documentation)
(declare (ignore documentation))
- `(js:var ,name ,@(when value-provided?
- (list (compile-parenscript-form value :expecting :expression)))))
+ (let ((name (ps-macroexpand name)))
+ (ecase expecting
+ (:statement
+ `(js:var ,name ,@(when value-provided?
+ (list (compile-parenscript-form (ps-macroexpand value) :expecting :expression)))))
+ (:expression
+ (push name *enclosing-lexical-block-declarations*)
+ (when value-provided?
+ (compile-parenscript-form `(setf ,name ,value) :expecting :expression))))))
(defpsmacro defvar (name &optional (value (values) value-provided?) documentation)
- "Note: this must be used as a top-level form, otherwise the result will be undefined behavior."
+ ;; this must be used as a top-level form, otherwise the resulting behavior will be undefined.
+ (declare (ignore documentation))
(pushnew name *ps-special-variables*)
`(var ,name ,@(when value-provided? (list value))))
-(defun make-let-vars (bindings)
- (mapcar (lambda (x) (if (listp x) (car x) x)) bindings))
-
-(defun make-let-vals (bindings)
- (mapcar (lambda (x) (if (or (atom x) (endp (cdr x))) nil (second x))) bindings))
-
-(defpsmacro lexical-let* (bindings &body body)
- `((lambda ()
- (let* ,bindings
- ,@body))))
+(define-ps-special-form let (bindings &body body)
+ (let* (lexical-bindings-introduced-here
+ (normalized-bindings (mapcar (lambda (x)
+ (if (symbolp x)
+ (list x nil)
+ (list (car x) (ps-macroexpand (cadr x)))))
+ bindings))
+ (free-variables-in-binding-value-expressions (mapcan (lambda (x) (flatten (cadr x)))
+ normalized-bindings)))
+ (flet ((maybe-rename-lexical-var (x)
+ (if (or (member x *vars-bound-in-enclosing-lexical-scopes*)
+ (member x free-variables-in-binding-value-expressions))
+ (ps-gensym x)
+ (progn (push x lexical-bindings-introduced-here) nil)))
+ (rename (x) (first x))
+ (var (x) (second x))
+ (val (x) (third x)))
+ (let* ((lexical-bindings (loop for x in normalized-bindings
+ unless (ps-special-variable-p (car x))
+ collect (cons (maybe-rename-lexical-var (car x)) x)))
+ (dynamic-bindings (loop for x in normalized-bindings
+ when (ps-special-variable-p (car x))
+ collect (cons (ps-gensym (format nil "~A_~A" (car x) 'tmp-stack)) x)))
+ (renamed-body `(symbol-macrolet ,(loop for x in lexical-bindings
+ when (rename x) collect
+ `(,(var x) ,(rename x)))
+ ,@body))
+ (*vars-bound-in-enclosing-lexical-scopes* (append lexical-bindings-introduced-here
+ *vars-bound-in-enclosing-lexical-scopes*)))
+ (compile-parenscript-form
+ `(progn
+ ,@(mapcar (lambda (x) `(var ,(or (rename x) (var x)) ,(val x))) lexical-bindings)
+ ,(if dynamic-bindings
+ `(progn ,@(mapcar (lambda (x) `(var ,(rename x))) dynamic-bindings)
+ (try (progn (setf ,@(loop for x in dynamic-bindings append
+ `(,(rename x) ,(var x)
+ ,(var x) ,(val x))))
+ ,renamed-body)
+ (:finally
+ (setf ,@(mapcan (lambda (x) `(,(var x) ,(rename x))) dynamic-bindings)))))
+ renamed-body))
+ :expecting expecting)))))
-(defpsmacro lexical-let (bindings &body body)
- `((lambda ,(make-let-vars bindings)
- ,@body)
- ,@(make-let-vals bindings)))
-
-(defpsmacro simple-let* (bindings &body body)
+(defpsmacro let* (bindings &body body)
(if bindings
- (let ((var (if (listp (car bindings)) (caar bindings) (car bindings))))
- `(,(if (member var *ps-special-variables*) 'let1-dynamic 'let1) ,(car bindings)
- (simple-let* ,(cdr bindings) ,@body)))
+ `(let (,(car bindings))
+ (let* ,(cdr bindings)
+ ,@body))
`(progn ,@body)))
-(defpsmacro simple-let (bindings &body body)
- (let ((vars (mapcar (lambda (x) (if (atom x) x (first x))) bindings))
- (vals (mapcar (lambda (x) (if (or (atom x) (endp (cdr x))) nil (second x))) bindings)))
- (let ((gensyms (mapcar (lambda (x) (ps-gensym (format nil "_js_~a" x))) vars)))
- `(simple-let* ,(mapcar #'list gensyms vals)
- (simple-let* ,(mapcar #'list vars gensyms)
- ,@(mapcar (lambda (x) `(delete ,x)) gensyms)
- ,@body)))))
-
-(defpsmacro let* (bindings &body body)
- `(simple-let* ,bindings ,@body))
-
-(defpsmacro let (bindings &body body)
- `(,(if (= 1 (length bindings)) 'simple-let* 'simple-let) ,bindings ,@body))
-
-(define-ps-special-form let1 (binding &rest body)
- (ecase expecting
- (:statement
- (compile-parenscript-form `(progn ,(if (atom binding) `(var ,binding) `(var ,@binding)) ,@body) :expecting :statement))
- (:expression
- (let ((var (if (atom binding) binding (car binding)))
- (variable-assignment (when (listp binding) (cons 'setf binding))))
- (push var *enclosing-lexical-block-declarations*)
- (compile-parenscript-form `(progn ,variable-assignment ,@body) :expecting :expression)))))
-
-(defpsmacro let1-dynamic ((var value) &rest body)
- (with-ps-gensyms (temp-stack-var)
- `(progn (var ,temp-stack-var)
- (try (progn (setf ,temp-stack-var ,var)
- (setf ,var ,value)
- ,@body)
- (:finally
- (setf ,var ,temp-stack-var)
- (delete ,temp-stack-var))))))
-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; iteration
(defun make-for-vars/inits (init-forms)
(mapcar (lambda (x)
- (cons (compile-parenscript-form (if (atom x) x (first x)) :expecting :symbol)
- (compile-parenscript-form (if (atom x) nil (second x)) :expecting :expression)))
+ (cons (compile-parenscript-form (ps-macroexpand (if (atom x) x (first x))) :expecting :symbol)
+ (compile-parenscript-form (ps-macroexpand (if (atom x) nil (second x))) :expecting :expression)))
init-forms))
(define-ps-special-form labeled-for (label init-forms cond-forms step-forms &rest body)
`(js:for ,label
,(make-for-vars/inits init-forms)
- ,(mapcar (lambda (x) (compile-parenscript-form x :expecting :expression)) cond-forms)
- ,(mapcar (lambda (x) (compile-parenscript-form x :expecting :expression)) step-forms)
+ ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x) :expecting :expression)) cond-forms)
+ ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x) :expecting :expression)) step-forms)
,(compile-parenscript-form `(progn ,@body))))
(defpsmacro for (init-forms cond-forms step-forms &body body)
,(do-make-iter-psteps decls)))))
(define-ps-special-form for-in ((var object) &rest body)
- `(js:for-in ,(compile-parenscript-form `(var ,var) :expecting :expression)
- ,(compile-parenscript-form object :expecting :expression)
+ `(js:for-in ,(compile-parenscript-form var :expecting :expression)
+ ,(compile-parenscript-form (ps-macroexpand object) :expecting :expression)
,(compile-parenscript-form `(progn ,@body))))
(define-ps-special-form while (test &rest body)
(define-ps-special-form lisp (lisp-form)
;; (ps (foo (lisp bar))) is in effect equivalent to (ps* `(foo ,bar))
;; when called from inside of ps*, lisp-form has access only to the dynamic environment (like for eval)
- `(js:escape (ps1* ,lisp-form)))
+ `(js:escape (compiled-form-to-string (compile-parenscript-form ,lisp-form :expecting ,expecting))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; eval-when
+(define-ps-special-form eval-when (situation-list &body body)
+ "(eval-when (situation*) body-form*)
+
+The body forms are evaluated only during the given SITUATION. The accepted SITUATIONS are
+:load-toplevel, :compile-toplevel, and :execute. The code in BODY-FORM is assumed to be
+COMMON-LISP code in :compile-toplevel and :load-toplevel sitations, and parenscript code in
+:execute. "
+ (when (and (member :compile-toplevel situation-list)
+ (member *ps-compilation-level* '(:toplevel :inside-toplevel-form)))
+ (eval `(progn ,@body)))
+ (if (member :execute situation-list)
+ (compile-parenscript-form `(progn ,@body) :expecting expecting)
+ (compile-parenscript-form `(progn) :expecting expecting)))
\ No newline at end of file