-(in-package :parenscript)
+(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))))
+ (list 'js:literal ,string))))
(defpsliteral this "this")
(defpsliteral t "true")
(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) `',x) x)))
- (null '(make-array))
- (symbol (symbol-to-js-string x))
- (number x)
- (string x)))
+ (def-for-literal break js:break)
+ (def-for-literal continue js:continue))
+
+(define-ps-special-form quote (x)
+ (ps-compile-expression
+ (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))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; unary operators
(let ((op (if (listp op) (car op) op))
(spacep (if (listp op) (second op) nil)))
`(define-ps-special-form ,op (x)
- (list 'unary-operator ',op
- (compile-parenscript-form x :expecting :expression)
+ (list 'js:unary-operator ',op
+ (ps-compile-expression (ps-macroexpand x))
:prefix t :space ,spacep))))
ops))))
(def-unary-ops ~ ! (new t) (delete t) (void t) (typeof t)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; statements
(define-ps-special-form return (&optional value)
- (list 'js-return (compile-parenscript-form value :expecting :expression)))
+ `(js:return ,(ps-compile-expression (ps-macroexpand value))))
(define-ps-special-form throw (value)
- (list 'js-throw (compile-parenscript-form value :expecting :expression)))
+ `(js:throw ,(ps-compile-expression (ps-macroexpand value))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; arrays
(define-ps-special-form array (&rest values)
- (cons 'array-literal (mapcar (lambda (form) (compile-parenscript-form form :expecting :expression))
- values)))
+ `(js:array ,@(mapcar (lambda (form) (ps-compile-expression (ps-macroexpand form)))
+ values)))
(define-ps-special-form aref (array &rest coords)
- (list 'js-aref (compile-parenscript-form array :expecting :expression)
- (mapcar (lambda (form)
- (compile-parenscript-form form :expecting :expression))
- coords)))
+ `(js:aref ,(ps-compile-expression (ps-macroexpand array))
+ ,(mapcar (lambda (form)
+ (ps-compile-expression (ps-macroexpand form)))
+ coords)))
(defpsmacro list (&rest values)
`(array ,@values))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; operators
(define-ps-special-form incf (x &optional (delta 1))
- (if (equal delta 1)
- (list 'unary-operator '++ (compile-parenscript-form x :expecting :expression) :prefix t)
- (list 'operator '+= (list (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:++ ,(ps-compile-expression x) :prefix t)
+ `(js:operator js:+= ,(ps-compile-expression x)
+ ,(ps-compile-expression delta)))))
(define-ps-special-form decf (x &optional (delta 1))
- (if (equal delta 1)
- (list 'unary-operator '-- (compile-parenscript-form x :expecting :expression) :prefix t)
- (list 'operator '-= (list (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:-- ,(ps-compile-expression x) :prefix t)
+ `(js:operator js:-= ,(ps-compile-expression x)
+ ,(ps-compile-expression delta)))))
(define-ps-special-form - (first &rest rest)
- (if (null rest)
- (list 'unary-operator '- (compile-parenscript-form first :expecting :expression) :prefix t)
- (list 'operator '- (mapcar (lambda (val) (compile-parenscript-form val :expecting :expression))
- (cons first rest)))))
+ (let ((first (ps-macroexpand first))
+ (rest (mapcar #'ps-macroexpand rest)))
+ (if rest
+ `(js:operator js:- ,@(mapcar (lambda (val) (ps-compile-expression val))
+ (cons first rest)))
+ `(js:unary-operator js:- ,(ps-compile-expression first) :prefix t))))
(define-ps-special-form not (x)
- (let ((form (compile-parenscript-form x :expecting :expression))
- (not-op nil))
- (if (and (eql (first form) 'operator)
- (= (length (third form)) 2)
- (setf not-op (case (second form)
- (== '!=)
- (< '>=)
- (> '<=)
- (<= '>)
- (>= '<)
- (!= '==)
- (=== '!==)
- (!== '===)
- (t nil))))
- (list 'operator not-op (third form))
- (list 'unary-operator '! form :prefix t))))
+ (let ((form (ps-compile-expression (ps-macroexpand x)))
+ inverse-op)
+ (if (and (eq (car form) 'js:operator)
+ (= (length (cddr form)) 2)
+ (setf inverse-op (case (cadr form)
+ (== '!=)
+ (< '>=)
+ (> '<=)
+ (<= '>)
+ (>= '<)
+ (!= '==)
+ (=== '!==)
+ (!== '===))))
+ `(js:operator ,inverse-op ,@(cddr form))
+ `(js:unary-operator js:! ,form :prefix t))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; control structures
(defun flatten-blocks (body)
(when body
(if (and (listp (car body))
- (eql 'js-block (caar body)))
- (append (third (car body)) (flatten-blocks (cdr body)))
+ (eq 'js:block (caar body)))
+ (append (cdr (car body)) (flatten-blocks (cdr body)))
(cons (car body) (flatten-blocks (cdr body))))))
(defun constant-literal-form-p (form)
(or (numberp form)
(stringp form)
(and (listp form)
- (eql 'js-literal (car form)))))
+ (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)
- (list 'js-block
- expecting
- (let* ((block (mapcar (lambda (form)
- (compile-parenscript-form form :expecting expecting))
- body))
- (clean-block (remove nil block))
- (flat-block (flatten-blocks clean-block))
- (reachable-block (append (remove-if #'constant-literal-form-p (butlast flat-block))
- (last flat-block))))
- reachable-block))))
+ (let ((body (mapcar #'ps-macroexpand body)))
+ (if (and compile-expression? (= 1 (length body)))
+ (ps-compile-expression (car body))
+ `(,(if compile-expression? 'js:|,| 'js:block)
+ ,@(let* ((block (flatten-blocks (remove nil (mapcar #'ps-compile body)))))
+ (append (remove-if #'constant-literal-form-p (butlast block)) (last block)))))))
(define-ps-special-form cond (&rest clauses)
- (ecase expecting
- (:statement (list 'js-cond-statement
- (mapcar (lambda (clause)
- (destructuring-bind (test &rest body)
- clause
- (list (compile-parenscript-form test :expecting :expression)
- (compile-parenscript-form `(progn ,@body) :expecting :statement))))
- clauses)))
- (:expression (make-cond-clauses-into-nested-ifs clauses))))
+ (if compile-expression?
+ (make-cond-clauses-into-nested-ifs clauses)
+ `(js:if ,(ps-compile-expression (caar clauses))
+ ,(ps-compile-statement `(progn ,@(cdar clauses)))
+ ,@(loop for (test . body) in (cdr clauses) appending
+ (if (eq t test)
+ `(:else ,(ps-compile-statement `(progn ,@body)))
+ `(:else-if ,(ps-compile-expression test)
+ ,(ps-compile-statement `(progn ,@body))))))))
(defun make-cond-clauses-into-nested-ifs (clauses)
(if clauses
(destructuring-bind (test &rest body)
(car clauses)
(if (eq t test)
- (compile-parenscript-form `(progn ,@body) :expecting :expression)
- `(js:? ,(compile-parenscript-form test :expecting :expression)
- ,(compile-parenscript-form `(progn ,@body) :expecting :expression)
+ (ps-compile-expression `(progn ,@body))
+ `(js:? ,(ps-compile-expression test)
+ ,(ps-compile-expression `(progn ,@body))
,(make-cond-clauses-into-nested-ifs (cdr clauses)))))
- (compile-parenscript-form nil :expecting :expression)))
+ (ps-compile-expression nil)))
(define-ps-special-form if (test then &optional else)
- (ecase expecting
- (:statement `(js:if ,(compile-parenscript-form test :expecting :expression)
- ,(compile-parenscript-form `(progn ,then))
- ,(when 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)))))
+ (if compile-expression?
+ `(js:? ,(ps-compile-expression (ps-macroexpand test))
+ ,(ps-compile-expression (ps-macroexpand then))
+ ,(ps-compile-expression (ps-macroexpand else)))
+ `(js:if ,(ps-compile-expression (ps-macroexpand test))
+ ,(ps-compile-statement `(progn ,then))
+ ,@(when else `(:else ,(ps-compile-statement `(progn ,else)))))))
(define-ps-special-form switch (test-expr &rest clauses)
- (let ((clauses (mapcar (lambda (clause)
- (let ((val (car clause))
- (body (cdr clause)))
- (cons (if (and (symbolp val)
- (eq (ensure-ps-symbol val) 'default))
- 'default
- (compile-parenscript-form val :expecting :expression))
- (mapcar (lambda (statement) (compile-parenscript-form statement :expecting :statement))
- body))))
- clauses))
- (expr (compile-parenscript-form test-expr :expecting :expression)))
- (list 'js-switch expr clauses)))
+ `(js:switch ,(ps-compile-expression test-expr)
+ ,(loop for (val . body) in clauses collect
+ (cons (if (eq val 'default)
+ 'default
+ (ps-compile-expression val))
+ (mapcar (lambda (x) (ps-compile-statement x))
+ body)))))
(defpsmacro case (value &rest clauses)
(labels ((make-clause (val body more)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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 ,@(loop for var in *enclosing-lexical-block-declarations* collect `(var ,var))
- ,@body) :expecting :statement))))
+ (let ((args (mapcar #'ps-compile-symbol args)))
+ (list args
+ (let* ((*enclosing-lexical-block-declarations* ())
+ (*vars-bound-in-enclosing-lexical-scopes* (append args
+ *vars-bound-in-enclosing-lexical-scopes*))
+ (body (ps-compile-statement `(progn ,@body)))
+ (var-decls (ps-compile-statement
+ `(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)
- (cons 'js-lambda (compile-function-definition args body)))
+ `(js:lambda ,@(compile-function-definition args body)))
(define-ps-special-form %js-defun (name args &rest body)
- (append (list 'js-defun name) (compile-function-definition args body)))
+ `(js:defun ,name ,@(compile-function-definition args body)))
(defun parse-function-body (body)
(let* ((docstring
(values body-forms docstring)))
(defun parse-key-spec (key-spec)
- "parses an &key parameter. Returns 4 values:
+ "parses an &key parameter. Returns 5 values:
var, init-form, keyword-name, supplied-p-var, init-form-supplied-p.
Syntax of key spec:
"
(let* ((var (cond ((symbolp key-spec) key-spec)
((and (listp key-spec) (symbolp (first key-spec))) (first key-spec))
- ((and (listp key-spec) (listp (first key-spec))) (second key-spec))))
+ ((and (listp key-spec) (listp (first key-spec))) (second (first key-spec)))))
(keyword-name (if (and (listp key-spec) (listp (first key-spec)))
(first (first key-spec))
(intern (string var) :keyword)))
(init-form (if (listp spec) (second spec)))
(supplied-p-var (if (listp spec) (third spec))))
(values var init-form supplied-p-var)))
-
+
(defun parse-aux-spec (spec)
"Returns two values: variable and init-form"
;; [&aux {var | (var [init-form])}*])
(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
the given lambda-list and body."
- ;; The lambda list is transformed as follows, since a javascript lambda list is just a
+ ;; The lambda list is transformed as follows, since a javascript lambda list is just a
;; 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)
- ;; * when any keyword variables are in the lambda list, a single 'optional-args' variable is
- ;; appended to the js-lambda list as the last argument. WITH-SLOTS is used for all
- ;; the variables with inside the body of the function,
- ;; a (with-slots ((var-name key-name)) optional-args ...)
+ ;; 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. 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)
(parse-lambda-list lambda-list)
- (declare (ignore allow? aux? aux more? more-context more-count))
- (let* ((options-var (or key-object (ps-gensym)))
- ;; optionals are of form (var default-value)
+ (declare (ignore allow? aux? aux more? more-context more-count key-object))
+ (let* (;; optionals are of form (var default-value)
(effective-args
(remove-if
#'null
(append requireds
- (mapcar #'parse-optional-spec optionals)
- (when keys (list options-var)))))
- ;; an alist of arg -> default val
- (initform-pairs
- (remove
- nil
- (append
- ;; optional arguments first
- (mapcar #'(lambda (opt-spec)
- (multiple-value-bind (var val) (parse-optional-spec opt-spec)
- (cons var val)))
- optionals)
- (if keys? (list (cons options-var '(create))))
- (mapcar #'(lambda (key-spec)
- (multiple-value-bind (var val x y specified?) (parse-key-spec key-spec)
- (declare (ignore x y))
- (when specified? (cons var val))))
- keys))))
- (body-paren-forms (parse-function-body body)) ; remove documentation
- (initform-forms
- (mapcar #'(lambda (default-pair)
- `(defaultf ,(car default-pair) ,(cdr default-pair)))
- initform-pairs))
+ (mapcar #'parse-optional-spec optionals))))
+ (opt-forms
+ (mapcar #'(lambda (opt-spec)
+ (multiple-value-bind (var val suppl)
+ (parse-optional-spec opt-spec)
+ `(defaultf ,var ,val ,suppl)))
+ optionals))
+ (key-forms
+ (when keys?
+ (if (< *js-target-version* 1.6)
+ (with-ps-gensyms (n)
+ (let ((decls nil) (assigns nil) (defaults nil))
+ (mapc (lambda (k)
+ (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 suppl) defaults)))
+ (reverse keys))
+ `(,@decls
+ (loop :for ,n :from ,(length requireds)
+ :below (length arguments) :by 2 :do
+ (case (aref arguments ,n) ,@assigns))
+ ,@defaults)))
+ (mapcar (lambda (k)
+ (multiple-value-bind (var init-form keyword-str)
+ (parse-key-spec k)
+ (with-ps-gensyms (x)
+ `(let ((,x ((@ *Array prototype index-of call) arguments ,keyword-str ,(length requireds))))
+ (var ,var (if (= -1 ,x) ,init-form (aref arguments (1+ ,x))))))))
+ keys))))
(rest-form
(if rest?
(with-ps-gensyms (i)
`(progn (var ,rest (array))
- (dotimes (,i (- arguments.length ,(length effective-args)))
+ (dotimes (,i (- (slot-value arguments 'length) ,(length effective-args)))
(setf (aref ,rest ,i) (aref arguments (+ ,i ,(length effective-args)))))))
`(progn)))
- (effective-body (append initform-forms (list rest-form) body-paren-forms))
- (effective-body
- (if keys?
- (list `(with-slots ,(mapcar #'(lambda (key-spec)
- (multiple-value-bind (var x key-name)
- (parse-key-spec key-spec)
- (declare (ignore x))
- (list var key-name)))
- keys)
- ,options-var
- ,@effective-body))
- effective-body)))
+ (body-paren-forms (parse-function-body body)) ; remove documentation
+ (effective-body (append opt-forms key-forms (list rest-form) body-paren-forms)))
(values effective-args effective-body))))
(defpsmacro defun (name lambda-list &body body)
"An extended defun macro that allows cool things like keyword arguments.
lambda-list::=
- (var*
- [&optional {var | (var [init-form [supplied-p-parameter]])}*]
- [&rest var]
- [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
+ (var*
+ [&optional {var | (var [init-form [supplied-p-parameter]])}*]
+ [&rest var]
+ [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
[&aux {var | (var [init-form])}*])"
(if (symbolp name)
`(defun-function ,name ,lambda-list ,@body)
- (progn (assert (and (= (length name) 2) (eq 'setf (ensure-ps-symbol (car name)))) ()
+ (progn (assert (and (listp name) (= (length name) 2) (eq 'setf (car name))) ()
"(defun ~s ~s ...) needs to have a symbol or (setf symbol) for a name." name lambda-list)
`(defun-setf ,name ,lambda-list ,@body))))
`(%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::=
- (var*
- [&optional {var | (var [init-form [supplied-p-parameter]])}*]
- [&rest var]
- [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
+ (var*
+ [&optional {var | (var [init-form [supplied-p-parameter]])}*]
+ [&rest var]
+ [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
[&aux {var | (var [init-form])}*])"
(multiple-value-bind (effective-args effective-body)
(parse-extended-function lambda-list body)
`(%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) in fn-defs do
+ (setf (gethash fn-name fn-renames) (ps-gensym fn-name)))
+ (let ((fn-defs (ps-compile
+ `(progn ,@(loop for (fn-name . def) in fn-defs collect
+ `(var ,(gethash fn-name fn-renames) (lambda ,@def))))))
+ (*ps-local-function-names* (cons fn-renames *ps-local-function-names*)))
+ (append fn-defs (cdr (ps-compile `(progn ,@body)))))))
+
+(define-ps-special-form labels (fn-defs &rest body)
+ (with-local-macro-environment (local-fn-renames *ps-local-function-names*)
+ (loop for (fn-name) in fn-defs do
+ (setf (gethash fn-name local-fn-renames) (ps-gensym fn-name)))
+ (ps-compile
+ `(progn ,@(loop for (fn-name . def) in fn-defs collect
+ `(var ,(gethash fn-name local-fn-renames) (lambda ,@def)))
+ ,@body))))
+
+(define-ps-special-form function (fn-name)
+ (ps-compile (maybe-rename-local-function fn-name)))
-(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)))))
+ (ps-compile `(progn ,@body))))
(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))))
-
-(define-ps-special-form defmacro (name args &body 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*)))
+ (ps-compile `(progn ,@body))))))
+
+(define-ps-special-form defmacro (name args &body body) ;; should this be a macro?
(eval `(defpsmacro ,name ,args ,@body))
nil)
-(define-ps-special-form define-symbol-macro (name expansion)
+(define-ps-special-form define-symbol-macro (name expansion) ;; should this be a macro?
(eval `(define-ps-symbol-macro ,name ,expansion))
nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; objects
-(add-ps-literal '{})
+(add-ps-reserved-symbol '{})
(define-ps-symbol-macro {} (create))
(define-ps-special-form create (&rest arrows)
- (list '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 (list '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)
- (list '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 (ps-compile-expression (ps-macroexpand key-expr))))
+ (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 (ps-compile-expression (ps-macroexpand val-expr))))))))
(define-ps-special-form instanceof (value type)
- (list 'js-instanceof (compile-parenscript-form value :expecting :expression)
- (compile-parenscript-form type :expecting :expression)))
+ `(js:instanceof ,(ps-compile-expression value)
+ ,(ps-compile-expression type)))
+
+(define-ps-special-form %js-slot-value (obj slot)
+ (let ((slot (ps-macroexpand slot)))
+ `(js:slot-value ,(ps-compile-expression (ps-macroexpand obj))
+ ,(let ((slot (if (and (listp slot) (eq 'quote (car slot)))
+ (second slot) ;; assume we're quoting a symbol
+ (ps-compile-expression 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))
(/ '/=)
(t nil)))
-(defun smart-setf (lhs rhs)
- (if (and (listp rhs)
- (eql 'operator (car rhs))
- (member lhs (third rhs) :test #'equalp))
- (let ((args-without-first (remove lhs (third rhs) :count 1 :end 1 :test #'equalp)))
- (cond ((and (assignment-op (second rhs))
- (member (second rhs) '(+ *))
- (equalp lhs (first (third rhs))))
- (list 'operator (assignment-op (second rhs))
- (list lhs (list 'operator (second rhs) args-without-first))))
- (t (list 'js-assign lhs rhs))))
- (list 'js-assign lhs rhs)))
-
(define-ps-special-form setf1% (lhs rhs)
- (smart-setf (compile-parenscript-form lhs :expecting :expression) (compile-parenscript-form rhs :expecting :expression)))
+ (let ((lhs (ps-compile-expression (ps-macroexpand lhs)))
+ (rhs (ps-compile-expression (ps-macroexpand rhs))))
+ (if (and (listp rhs)
+ (eq 'js:operator (car rhs))
+ (member (cadr rhs) '(+ *))
+ (equalp lhs (caddr rhs)))
+ `(js:operator ,(assignment-op (cadr rhs)) ,lhs (js:operator ,(cadr rhs) ,@(cdddr 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)))
(check-setq-args args)
`(psetf ,@args))
-(define-ps-special-form var (name &rest value)
- (append (list 'js-var name)
- (when value
- (assert (= (length value) 1) () "Wrong number of arguments to var: ~s" `(var ,name ,@value))
- (list (compile-parenscript-form (car value) :expecting :expression)))))
-
-(defpsmacro defvar (name &rest value)
- "Note: this must be used as a top-level form, otherwise the result will be undefined behavior."
+(define-ps-special-form var (name &optional (value (values) value-provided?) documentation)
+ (declare (ignore documentation))
+ (let ((name (ps-macroexpand name)))
+ (if compile-expression?
+ (progn (push name *enclosing-lexical-block-declarations*)
+ (when value-provided?
+ (ps-compile-expression `(setf ,name ,value))))
+ `(js:var (js:variable ,name) ,@(when value-provided?
+ (list (ps-compile-expression (ps-macroexpand value))))))))
+
+(defpsmacro defvar (name &optional (value (values) value-provided?) documentation)
+ ;; this must be used as a top-level form, otherwise the resulting behavior will be undefined.
+ (declare (ignore documentation))
(pushnew name *ps-special-variables*)
- (assert (or (null value) (= (length value) 1)) () "Wrong number of arguments to defvar: ~s" `(defvar ,name ,@value))
- `(var ,name ,@value))
-
-(defun make-let-vars (bindings)
- (mapcar (lambda (x) (if (listp x) (car x) x)) bindings))
+ `(var ,name ,@(when value-provided? (list value))))
+
+(define-ps-special-form let (bindings &body body)
+ `(js:let ,(mapcar #'car bindings)
+ ,@(let ((body-forms (if compile-expression?
+ `((progn
+ ,@(mapcar (lambda (bind)
+ `(var ,(car bind) ,(cadr bind)))
+ bindings)
+ ,@body))
+ `(,@(mapcar (lambda (bind)
+ `(var ,(car bind) ,(cadr bind)))
+ bindings)
+ ,@body))))
+ (mapcar #'ps-compile body-forms))))
-(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))))
-
-(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 (ps-compile-symbol (ps-macroexpand (if (atom x) x (first x))))
+ (ps-compile-expression (ps-macroexpand (if (atom x) nil (second x))))))
init-forms))
(define-ps-special-form labeled-for (label init-forms cond-forms step-forms &rest body)
- (let ((vars (make-for-vars/inits init-forms))
- (steps (mapcar (lambda (x) (compile-parenscript-form x :expecting :expression)) step-forms))
- (tests (mapcar (lambda (x) (compile-parenscript-form x :expecting :expression)) cond-forms))
- (body (compile-parenscript-form `(progn ,@body))))
- (list 'js-for label vars tests steps body)))
+ `(js:for ,label
+ ,(make-for-vars/inits init-forms)
+ ,(mapcar (lambda (x) (ps-compile-expression (ps-macroexpand x))) cond-forms)
+ ,(mapcar (lambda (x) (ps-compile-expression (ps-macroexpand x))) step-forms)
+ ,(ps-compile-statement `(progn ,@body))))
(defpsmacro for (init-forms cond-forms step-forms &body body)
`(labeled-for nil ,init-forms ,cond-forms ,step-forms ,@body))
,@body
,(do-make-iter-psteps decls)))))
-(define-ps-special-form for-in (decl &rest body)
- (list 'js-for-in
- (compile-parenscript-form (first decl) :expecting :expression)
- (compile-parenscript-form (second decl) :expecting :expression)
- (compile-parenscript-form `(progn ,@body))))
-
-(defpsmacro doeach ((var array &optional (result (values) result?)) &body body)
- "Iterates over `array'. If `var' is a symbol, binds `var' to each
-element key. If `var' is a list, it must be a list of two
-symbols, (key value), which will be bound to each successive key/value
-pair in `array'."
- (if result?
- (if (consp var)
- (destructuring-bind (key val) var
- `((lambda ()
- (let* (,val)
- (for-in ((var ,key) ,array)
- (setf ,val (aref ,array ,key))
- ,@body)
- (return ,result)))))
- `((lambda ()
- (for-in ((var ,var) ,array)
- ,@body)
- (return ,result))))
- (if (consp var)
- (destructuring-bind (key val) var
- `(progn
- (let* (,val)
- (for-in ((var ,key) ,array)
- (setf ,val (aref ,array ,key))
- ,@body))))
- `(progn
- (for-in ((var ,var) ,array) ,@body)))))
+(define-ps-special-form for-in ((var object) &rest body)
+ `(js:for-in ,(ps-compile-expression var)
+ ,(ps-compile-expression (ps-macroexpand object))
+ ,(ps-compile-statement `(progn ,@body))))
(define-ps-special-form while (test &rest body)
- (list 'js-while (compile-parenscript-form test :expecting :expression)
- (compile-parenscript-form `(progn ,@body))))
+ `(js:while ,(ps-compile-expression test)
+ ,(ps-compile-statement `(progn ,@body))))
(defpsmacro dotimes ((var count &optional (result nil result?)) &rest body)
`(do* ((,var 0 (1+ ,var)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; misc
(define-ps-special-form with (expression &rest body)
- (list 'js-with (compile-parenscript-form expression :expecting :expression)
- (compile-parenscript-form `(progn ,@body))))
+ `(js:with ,(ps-compile-expression expression)
+ ,(ps-compile-statement `(progn ,@body))))
(define-ps-special-form try (form &rest clauses)
(let ((catch (cdr (assoc :catch clauses)))
(assert (not (cdar catch)) nil "Sorry, currently only simple catch forms are supported.")
(assert (or catch finally) ()
"Try form should have either a catch or a finally clause or both.")
- (list 'js-try (compile-parenscript-form `(progn ,form))
- :catch (when catch (list (compile-parenscript-form (caar catch) :expecting :symbol)
- (compile-parenscript-form `(progn ,@(cdr catch)))))
- :finally (when finally (compile-parenscript-form `(progn ,@finally))))))
+ `(js:try ,(ps-compile-statement `(progn ,form))
+ :catch ,(when catch (list (ps-compile-symbol (caar catch))
+ (ps-compile-statement `(progn ,@(cdr catch)))))
+ :finally ,(when finally (ps-compile-statement `(progn ,@finally))))))
(define-ps-special-form cc-if (test &rest body)
- (list 'cc-if test (mapcar #'compile-parenscript-form body)))
+ `(js:cc-if ,test ,@(mapcar #'ps-compile-statement body)))
(define-ps-special-form regex (regex)
- (list 'js-regex (string regex)))
+ `(js:regex ,(string regex)))
(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)
- (list 'js-escape lisp-form))
+ `(js:escape (compiled-form-to-string (let ((compile-expression? ,compile-expression?))
+ (ps-compile ,lisp-form)))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; 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)
+ (ps-compile `(progn ,@body))
+ (ps-compile `(progn))))