This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
Re: Strange interaction with stream evaluation and define-syntax?
- From: Per Bothner <per at bothner dot com>
- To: Stephen Peters <portnoy at portnoy dot org>
- Cc: kawa at sources dot redhat dot com
- Date: Thu, 22 Aug 2002 23:58:19 -0700
- Subject: Re: Strange interaction with stream evaluation and define-syntax?
- References: <us54rdnaqj4.fsf@portnoy.org>
Stephen Peters wrote:
I think this might be a bug, but I wanted to make sure that I'm not
doing something boneheaded.
It's a bug, which prevents a macro from being expanded in the same
compilation unit as it it defined.
I've attached a patch, and also checked it into CVS. I also
checked in a testcase, based on your bug report.
--
--Per Bothner
per@bothner.com http://www.bothner.com/per/
Index: kawa/lang/Macro.java
===================================================================
RCS file: /cvs/kawa/kawa/kawa/lang/Macro.java,v
retrieving revision 1.16
diff -u -r1.16 Macro.java
--- kawa/lang/Macro.java 29 May 2002 06:20:38 -0000 1.16
+++ kawa/lang/Macro.java 23 Aug 2002 06:34:35 -0000
@@ -7,7 +7,7 @@
public class Macro extends Syntax implements Printable, Externalizable
{
- public Expression expander;
+ public Object expander;
java.util.Vector capturedIdentifiers;
@@ -21,11 +21,6 @@
* The binding (if any) for templateIdentifiers[i] is capturedDeclarations[i]. */
Object[] capturedDeclarations;
- public Expression getExpander()
- {
- return expander;
- }
-
public static Macro make (Declaration decl)
{
Macro mac = new Macro(decl.getName());
@@ -142,8 +137,29 @@
{
try
{
- Procedure pr
- = (Procedure) getExpander().eval(tr.getGlobalEnvironment());
+ Procedure pr;
+ Object exp = expander;
+ if (exp instanceof Procedure)
+ pr = (Procedure) exp;
+ else
+ {
+ if (! (exp instanceof Expression))
+ {
+ Macro savedMacro = tr.currentMacroDefinition;
+ tr.currentMacroDefinition = this;
+ try
+ {
+ exp = tr.rewrite(exp);
+ expander = exp;
+ }
+ finally
+ {
+ tr.currentMacroDefinition = savedMacro;
+ }
+ }
+ pr = (Procedure)
+ ((Expression) exp).eval(tr.getGlobalEnvironment());
+ }
SyntaxForm sform = new SyntaxForm();
sform.form = form;
sform.tr = tr;
Index: kawa/standard/define_syntax.java
===================================================================
RCS file: /cvs/kawa/kawa/kawa/standard/define_syntax.java,v
retrieving revision 1.20
diff -u -r1.20 define_syntax.java
--- kawa/standard/define_syntax.java 29 May 2002 06:20:39 -0000 1.20
+++ kawa/standard/define_syntax.java 23 Aug 2002 06:34:35 -0000
@@ -3,6 +3,7 @@
import gnu.expr.*;
import gnu.bytecode.ClassType;
import gnu.bytecode.Method;
+import gnu.mapping.Procedure;
import gnu.lists.*;
public class define_syntax extends Syntax
@@ -30,27 +31,36 @@
if (! (pair.cdr instanceof Pair))
return tr.syntaxError("Missing transformation for "+form.car);
pair = (Pair) pair.cdr;
- Macro savedMacro = tr.currentMacroDefinition;
- tr.currentMacroDefinition = macro;
- Expression rule = tr.rewrite(pair.car);
- tr.currentMacroDefinition = savedMacro;
- macro.expander = rule;
+ Object expander = macro.expander;;
+ if (! (expander instanceof Procedure)
+ && ! (expander instanceof Expression))
+ {
+ Macro savedMacro = tr.currentMacroDefinition;
+ tr.currentMacroDefinition = macro;
+ expander = tr.rewrite(macro.expander);
+ tr.currentMacroDefinition = savedMacro;
+ macro.expander = expander;
+ }
if (! (decl.context instanceof ModuleExp))
{
return QuoteExp.voidExp;
}
else
{
- if (! (rule instanceof QuoteExp)
- || ! (((QuoteExp) rule).getValue() instanceof java.io.Externalizable))
+ if (expander instanceof QuoteExp)
+ expander = ((QuoteExp) expander).getValue();
+ Expression rule;
+ if (expander instanceof Procedure
+ && expander instanceof java.io.Externalizable)
+ rule = new QuoteExp(macro);
+ else
{
Expression args[] = new Expression[2];
args[0] = new QuoteExp(name);
- args[1] = rule;
+ args[1] = expander instanceof Expression ? (Expression) expander
+ : new QuoteExp (expander);
rule = new ApplyExp(new PrimProcedure(makeMethod), args);
}
- else
- rule = new QuoteExp(macro);
SetExp result = new SetExp (decl, rule);
result.setDefining (true);
return result;
@@ -76,7 +86,7 @@
Declaration decl = defs.getDefine(name, 'w', tr);
Macro macro = Macro.make(decl);
-
+ macro.expander = p.car;
p = tr.makePair(st, this, new Pair(decl, p));
forms.addElement (p);