From: David Vázquez Date: Fri, 5 Jul 2013 12:10:19 +0000 (+0200) Subject: Codegen JS macros X-Git-Url: http://repo.macrolet.net/gitweb/?a=commitdiff_plain;h=4267e51b95191cbb22c5976fd99689fe0cc5b1f4;p=jscl.git Codegen JS macros --- diff --git a/src/compiler-codegen.lisp b/src/compiler-codegen.lisp index 83cb4b9..87a3740 100644 --- a/src/compiler-codegen.lisp +++ b/src/compiler-codegen.lisp @@ -24,6 +24,27 @@ (/debug "loading compiler-codegen.lisp!") +(defvar *js-macros* nil) +(defmacro define-js-macro (name lambda-list &body body) + (let ((form (gensym))) + `(push (cons ',name + (lambda (,form) + (block ,name + (destructuring-bind ,lambda-list ,form + ,@body)))) + *js-macros*))) + +(defun js-macroexpand (js) + (if (and (consp js) (assoc (car js) *js-macros*)) + (let ((expander (cdr (assoc (car js) *js-macros*)))) + (multiple-value-bind (expansion expand-more-p) + (funcall expander (cdr js)) + (if expand-more-p + (js-macroexpand expansion) + expansion))) + js)) + + (defconstant no-comma 12) (defvar *js-output* t) @@ -182,7 +203,8 @@ (reduce (lambda (x y) `(,(car form) ,x ,y)) (cdr form))) ((progn comma) (reduce (lambda (x y) `(comma ,x ,y)) (cdr form) :from-end t)) - (t form)) + (t + (js-macroexpand form))) form)) (defun js-operator-expression (op args precedence associativity operand-order) @@ -351,7 +373,7 @@ (t `(group ,@(cdr form)))))) (t - form))) + (js-macroexpand form)))) (defun js-stmt (form &optional parent) (let ((form (js-expand-stmt form)))