1 package kawa.lang; 2 import gnu.expr.*; 3 import gnu.mapping.Symbol; 4 import java.io.*; 5 6 7 8 public class SyntaxForm implements Externalizable 9 { 10 public Object form; 11 12 public TemplateScope scope; 13 14 16 20 21 private SyntaxForm () 22 { 23 } 24 25 public static SyntaxForm make (Object form, TemplateScope scope) 26 { 27 SyntaxForm sf = new SyntaxForm(); 28 sf.form = form; 29 sf.scope = scope; 30 return sf; 31 } 32 33 public String toString () 34 { 35 return "#<syntax " 36 + form 37 + " in #"+scope.id 40 +">"; 42 } 43 44 48 public SyntaxForm fromDatum (Object form) 49 { 50 SyntaxForm sf = new SyntaxForm(); 51 sf.form = form; 52 sf.scope = this.scope; 53 return sf; 54 } 55 56 63 public static Object makeWithTemplate (Object template, Object form) 64 { 65 if (form instanceof SyntaxForm) 66 return (SyntaxForm) form; 67 if (template instanceof SyntaxForm) 68 { 69 SyntaxForm sform = (SyntaxForm) template; 70 if (form == sform.form) 71 return sform; 72 return sform.fromDatum(form); 73 } 74 return form; 75 } 76 77 public SyntaxForm fromDatumIfNeeded (Object form) 78 { 79 if (form == this.form) 80 return this; 81 else if (form instanceof SyntaxForm) 82 return (SyntaxForm) form; 83 else 84 return fromDatum(form); 85 } 86 87 public static Expression rewrite (Object x) 88 { 89 Translator tr = (Translator) Compilation.getCurrent(); 90 return tr.rewrite(x); 91 } 92 93 public static Expression rewriteBody (Object x) 94 { 95 Translator tr = (Translator) Compilation.getCurrent(); 96 return tr.rewrite_body(x); 97 } 98 99 public boolean isIdentifier () 100 { 101 return form instanceof String || form instanceof Symbol; 102 } 103 104 public static boolean freeIdentifierEquals (SyntaxForm id1, SyntaxForm id2) 105 { 106 Translator tr = (Translator) Compilation.getCurrent(); 107 return tr.lexical.lookup(id1.form, -1) == tr.lexical.lookup(id2.form, -1); 108 } 109 110 public void writeExternal(ObjectOutput out) throws IOException 111 { 112 out.writeObject(form); 113 out.writeObject(scope); 114 } 115 116 public void readExternal(ObjectInput in) 117 throws IOException, ClassNotFoundException 118 { 119 form = in.readObject(); 120 scope = (TemplateScope) in.readObject(); 121 } 122 } 123 | Popular Tags |