From 4267e51b95191cbb22c5976fd99689fe0cc5b1f4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20V=C3=A1zquez?= Date: Fri, 5 Jul 2013 14:10:19 +0200 Subject: [PATCH] Codegen JS macros --- src/compiler-codegen.lisp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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))) -- 1.7.10.4