(defmethod compile-parenscript-form ((symbol symbol) &key expecting)
(declare (ignore expecting))
- (cond ((ps-special-form-p (list symbol))
+ (cond ((keywordp symbol) symbol)
+ ((ps-special-form-p (list symbol))
(if (ps-literal-p symbol)
(funcall (get-ps-special-form symbol) :symbol)
(error "Attempting to use Parenscript special form ~a as variable" symbol)))
"Compiles a bunch of Parenscript forms from a funcall form to an effective set of
Javascript arguments. The only extra processing this does is makes :keyword arguments
into a single options argument via CREATE."
- (flet ((keyword-arg (arg)
- "If the given compiled expression is supposed to be a keyword argument, returns
-the keyword for it."
- (when (and (listp arg) (eql (first arg) 'ps-quote)) (second arg))))
- (let ((compiled-args (mapcar (lambda (arg) (compile-parenscript-form arg :expecting :expression))
- arg-forms)))
- (do ((effective-expressions nil)
- (expressions-subl compiled-args))
- ((not expressions-subl) (reverse effective-expressions))
- (let ((arg-expr (first expressions-subl)))
- (if (keyword-arg arg-expr)
- (progn (when (oddp (length expressions-subl))
- (error "Odd number of keyword arguments: ~A." arg-forms))
- (push (list 'js-object (loop for (name val) on expressions-subl by #'cddr
- collect (list name val)))
- effective-expressions)
- (setf expressions-subl nil))
- (progn (push arg-expr effective-expressions)
- (setf expressions-subl (rest expressions-subl)))))))))
+ (let ((compiled-args (mapcar (lambda (arg) (compile-parenscript-form arg :expecting :expression))
+ arg-forms)))
+ (do ((effective-expressions nil)
+ (expressions-subl compiled-args))
+ ((not expressions-subl) (reverse effective-expressions))
+ (let ((arg-expr (first expressions-subl)))
+ (if (keywordp arg-expr)
+ (progn (when (oddp (length expressions-subl))
+ (error "Odd number of keyword arguments: ~A." arg-forms))
+ (push (list 'js-object (loop for (name val) on expressions-subl by #'cddr
+ collect (list (list 'js-variable name) val)))
+ effective-expressions)
+ (setf expressions-subl nil))
+ (progn (push arg-expr effective-expressions)
+ (setf expressions-subl (rest expressions-subl))))))))
(defun ps-convert-op-name (op)
(case (ensure-ps-symbol op)
(test-ps-js simple-slot-value
(let* ((foo (create :a 1)))
- (alert (slot-value foo 'a)))
+ (alert (slot-value foo 'a)))
"var foo = { a : 1 };
alert(foo.a);")
(test-ps-js buggy-slot-value
(let* ((foo (create :a 1))
- (slot-name "a"))
+ (slot-name "a"))
(alert (slot-value foo slot-name)))
" var foo = { a : 1 };
var slotName = 'a';
return foo + bar + _js1.baz;
}")
+(test-ps-js defun-keyword2
+ (defun zoo (&key baz) (return (* baz baz)))
+ "function zoo(_js1) {
+ if (_js1 === undefined) {
+ _js1 = { };
+ };
+ return _js1.baz * _js1.baz;
+}")
+
+(test-ps-js defun-keyword3
+ (defun zoo (&key baz (bar 4)) (return (* baz bar)))
+ "function zoo(_js1) {
+ if (_js1 === undefined) {
+ _js1 = { };
+ };
+ if (_js1.bar === undefined) {
+ _js1.bar = 4;
+ };
+ return _js1.baz * _js1.bar;
+}")
+
+(test-ps-js keyword-funcall1
+ (func :baz 1)
+ "func({ baz : 1 })")
+
+(test-ps-js keyword-funcall2
+ (func :baz 1 :bar foo)
+ "func({ baz : 1, bar : foo })")
+
+(test-ps-js keyword-funcall3
+ (fun a b :baz c)
+ "fun(a, b, { baz : c })")
+
(test-ps-js cond1
(cond ((= x 1) 1))
"if (x == 1) {
(test-ps-js keyword-consistent
:x
- "x")
+ "'x'")
(test-ps-js simple-symbol-macrolet
(symbol-macrolet ((x 1)) x)