Reimplement (.method object . args) syntax
authorClinton Ebadi <clinton@unknownlamer.org>
Thu, 17 Sep 2009 23:59:06 +0000 (19:59 -0400)
committerClinton Ebadi <clinton@unknownlamer.org>
Fri, 18 Sep 2009 20:51:52 +0000 (16:51 -0400)
Using PS:FUNCALL, PS:CHAIN, or ((PS:SLOT-VALUE ...) ...) for method
invocation is rather unwieldy for something so common in
javascript. This reenables the convenient (.method ...) call syntax in
a way that seems reasonably clean.

This may have subtle issues with the lexical-let variable renamer and
such, but my cursory skimming of the new compiler source doesn't seem
to indicate any serious issues.

src/compiler.lisp

index a7ae857..8a98dda 100644 (file)
@@ -68,6 +68,11 @@ lexical block.")
        (not (ps-special-form-p form))
        (not (null (op-precedence (first form))))))
 
+(defun method-call-form-p (form)
+  (and (listp form)
+       (symbolp (car form))
+       (char= #\. (char (symbol-name (car form)) 0))))
+
 (defun funcall-form-p (form)
   (and form
        (listp form)
@@ -264,6 +269,12 @@ the form cannot be compiled to a symbol."
                             (ps-compile-expression (ps-macroexpand form)))
                           (cdr form))))
 
+(defun compile-method-call-form (form)
+  (compile-funcall-form
+   `((js:slot-value ,(second form)
+                    ',(make-symbol (subseq (symbol-name (first form)) 1)))
+     ,@(cddr form))))
+
 (defun compile-funcall-form (form)
   `(js:funcall
     ,(if (symbolp (car form))
@@ -292,9 +303,11 @@ the form cannot be compiled to a symbol."
                    (compile-op-form form))))
             ((op-form-p form)
              (compile-op-form form))
+            ((method-call-form-p form)
+            (compile-method-call-form form))
             ((funcall-form-p form)
              (compile-funcall-form form))
-            (t (error "Cannot compile ~S to a ParenScript form." form))))))
+           (t (error "Cannot compile ~S to a ParenScript form." form))))))
 
 (defun ps-compile-statement (form)
   (let ((compile-expression? nil))