[comp.lang.lisp.x] XLISP Macro speedup

toma@sail.LABS.TEK.COM (Tom Almy) (02/09/91)

One aspect of XLISP that has always bothered me was that macros would get
expanded each time they are encountered, thus wasting significant amount
of execution time. It makes more sense to expand the macro the first time
it is encountered, and then automatically patching the enclosing form
with the expanded macro.

As it turns out, it is trivial to modify XLISP to do just that. Because 
there might be some occasions where replacement is undesirable (such as
during debugging) I made the replacement conditional on a non-nil value
of a new system variable *displace-macros* (name from equivalent feature
of PC-Lisp).

To install this feature, add initiation code for LVAL s_dispmacros 
to the xlinit.c file (following the same pattern as for other variables),
add the global declaration for s_dispmacros to xlglobal.c, 
and then change the eval function as follows.

In file xleval.c, function evform(), change:

        else {
            macroexpand(fun,args,&fun);
            val = xleval(fun);
        }

to:

        else {
            macroexpand(fun,args,&fun);
            if (getvalue(s_dispmacros) != NIL && consp(fun)) { 
                /* substitute back into original form */
                rplaca(form, car(fun));
                rplacd(form, cdr(fun));
            }
            val = xleval(fun);
        }

Now you can feel free to write macros without worrying about the
performance hit.

Tom Almy
toma@sail.labs.tek.com
Standard Disclaimers Apply
-- 
Tom Almy
toma@sail.labs.tek.com <<< Note new address
Standard Disclaimers Apply