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