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>
Thu, 17 Sep 2009 23:59:06 +0000 (19:59 -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 9fbba55..1d9dc7d 100644 (file)
@@ -68,6 +68,11 @@ lexical block.")
        (not (ps-special-form-p form))
        (not (null (op-precedence (first form))))))
 
        (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)
 (defun funcall-form-p (form)
   (and form
        (listp form)
@@ -265,6 +270,12 @@ the form cannot be compiled to a symbol."
                             (ps-compile-expression (ps-macroexpand form)))
                           (cdr form))))
 
                             (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
     ,(ps-compile-expression (if (symbolp (car form))
 (defun compile-funcall-form (form)
   `(js:funcall
     ,(ps-compile-expression (if (symbolp (car form))
@@ -293,9 +304,11 @@ the form cannot be compiled to a symbol."
                    (compile-op-form form))))
             ((op-form-p form)
              (compile-op-form form))
                    (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))
             ((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))
 
 (defun ps-compile-statement (form)
   (let ((compile-expression? nil))