01: /*
02: * Jatha - a Common LISP-compatible LISP library in Java.
03: * Copyright (C) 1997-2005 Micheal Scott Hewett
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: *
20: * For further information, please contact Micheal Hewett at
21: * hewett@cs.stanford.edu
22: *
23: */
24: /**
25: * $Id: MacroexpandPrimitive.java,v 1.3 2007/03/22 02:12:50 mhewett Exp $
26: */package org.jatha.compile;
27:
28: import org.jatha.Jatha;
29: import org.jatha.dynatype.LispPackage;
30: import org.jatha.dynatype.LispValue;
31: import org.jatha.machine.SECDMachine;
32:
33: /**
34: * <p>Returns the expansion of the form named. Otherwise the form will be returned as is.</p>
35: * <p>(macroexpand form)</p>
36: *
37: * @author <a href="mailto:Ola.Bini@itc.ki.se">Ola Bini</a>
38: * @version $Revision: 1.3 $
39: */
40: public class MacroexpandPrimitive extends LispPrimitive {
41: public MacroexpandPrimitive(final Jatha lisp) {
42: super (lisp, "MACROEXPAND", 1);
43: }
44:
45: public void Execute(final SECDMachine machine) {
46: final LispValue form = machine.S.pop();
47: LispValue now = expand(form);
48: LispValue lastOne = form;
49: while (now != lastOne && now.basic_consp()
50: && !(now == f_lisp.NIL)) {
51: lastOne = now;
52: now = expand(now);
53: }
54: machine.S.push(now);
55: machine.C.pop();
56: }
57:
58: private LispValue expand(final LispValue form) {
59: final LispValue carForm = form.car();
60: if (carForm.fboundp() == f_lisp.T
61: && carForm.symbol_function() != null
62: && carForm.symbol_function().basic_macrop()) {
63: return f_lisp.eval(f_lisp.makeCons(f_lisp.EVAL.intern("%%%"
64: + carForm.symbol_name().toStringSimple(),
65: (LispPackage) f_lisp.findPackage("SYSTEM")),
66: quoteList(form.cdr())));
67: } else {
68: return form;
69: }
70: }
71:
72: private LispValue quoteList(final LispValue intern) {
73: LispValue ret = f_lisp.NIL;
74: for (final java.util.Iterator iter = intern.iterator(); iter
75: .hasNext();) {
76: final LispValue curr = (LispValue) iter.next();
77: ret = f_lisp.makeCons(f_lisp.makeList(f_lisp.QUOTE, curr),
78: ret);
79: }
80: return ret.nreverse();
81: }
82: }// MacroexpandPrimitive
|