01: package kawa.standard;
02:
03: import kawa.lang.*;
04: import gnu.bytecode.*;
05: import gnu.lists.*;
06: import gnu.expr.*;
07:
08: public class syntax extends kawa.lang.Quote {
09: public static final syntax syntax = new syntax("syntax", false);
10: public static final syntax quasiSyntax = new syntax("quasisyntax",
11: true);
12:
13: public syntax(String name, boolean isQuasi) {
14: super (name, isQuasi);
15: }
16:
17: protected boolean expandColonForms() {
18: return false;
19: }
20:
21: static final ClassType typeTemplateScope = ClassType
22: .make("kawa.lang.TemplateScope");
23: static final Method makeTemplateScopeMethod = typeTemplateScope
24: .getDeclaredMethod("make", 0);
25:
26: public Expression rewriteForm(Pair form, Translator tr) {
27: if (!(form.cdr instanceof Pair)
28: || (form = (Pair) (form.cdr)).cdr != LList.Empty)
29: return tr
30: .syntaxError("syntax forms requires a single form");
31: Declaration saveTemplateScopeDecl = tr.templateScopeDecl;
32: if (saveTemplateScopeDecl == null) {
33: tr.letStart();
34: Expression init = new ApplyExp(makeTemplateScopeMethod,
35: Expression.noExpressions);
36: Declaration templateScopeDecl = tr.letVariable(null,
37: typeTemplateScope, init);
38: templateScopeDecl.setCanRead();
39: tr.templateScopeDecl = templateScopeDecl;
40: tr.letEnter();
41: }
42:
43: try {
44: Expression body = coerceExpression(expand(form.car,
45: isQuasi ? 1 : Quote.QUOTE_DEPTH, tr), tr);
46: return saveTemplateScopeDecl == null ? tr.letDone(body)
47: : body;
48: } finally {
49: tr.templateScopeDecl = saveTemplateScopeDecl;
50: }
51: }
52:
53: protected Expression leaf(Object val, Translator tr) {
54: return makeSyntax(val, tr);
55: }
56:
57: static Expression makeSyntax(Object form, Translator tr) {
58: SyntaxTemplate template = new SyntaxTemplate(form, null, tr);
59: Expression matchArray = QuoteExp.nullExp;
60: PatternScope patternScope = tr.patternScope;
61: if (patternScope != null && patternScope.matchArray != null)
62: matchArray = new ReferenceExp(patternScope.matchArray);
63: Expression[] args = { new QuoteExp(template), matchArray,
64: new ReferenceExp(tr.templateScopeDecl) };
65: return new ApplyExp(ClassType.make("kawa.lang.SyntaxTemplate")
66: .getDeclaredMethod("execute", 2), args);
67: }
68: }
|