001: package kawa.lang;
002:
003: import gnu.expr.*;
004: import gnu.mapping.Symbol;
005: import java.io.*;
006:
007: /** A "syntatic closure" - a syntax form with its compilation environment. */
008:
009: public class SyntaxForm implements Externalizable {
010: public Object form;
011:
012: public TemplateScope scope;
013:
014: // PairPosition pos;
015:
016: /*DEBUGGING:
017: static int counter;
018: int id = ++counter;
019: */
020:
021: private SyntaxForm() {
022: }
023:
024: public static SyntaxForm make(Object form, TemplateScope scope) {
025: SyntaxForm sf = new SyntaxForm();
026: sf.form = form;
027: sf.scope = scope;
028: return sf;
029: }
030:
031: public String toString() {
032: return "#<syntax " + form
033: ///* DEBUGGING:
034: // + " #" id +
035: + " in #" + scope.id
036: //*/
037: + ">";
038: }
039:
040: /** Make a SyntaxForm object with the same contextual information as this.
041: * @param form which used for the new syntax value.
042: * Corresponds to the <code>datum->syntax-object</code> function.
043: */
044: public SyntaxForm fromDatum(Object form) {
045: SyntaxForm sf = new SyntaxForm();
046: sf.form = form;
047: sf.scope = this .scope;
048: return sf;
049: }
050:
051: /** Create a syntax object with specified form, and given syntatic context.
052: * Used to implement datum->syntax-object in the syntax-case API.
053: * @param template If this is a SyntaxForm, use its scope;
054: * otherwise use the current Compilation's current scope.
055: * (This means just returning the form as-is.)
056: * @param form The value (S-expression form) to use.
057: */
058: public static Object makeWithTemplate(Object template, Object form) {
059: if (form instanceof SyntaxForm)
060: return (SyntaxForm) form;
061: if (template instanceof SyntaxForm) {
062: SyntaxForm sform = (SyntaxForm) template;
063: if (form == sform.form)
064: return sform;
065: return sform.fromDatum(form);
066: }
067: return form;
068: }
069:
070: public SyntaxForm fromDatumIfNeeded(Object form) {
071: if (form == this .form)
072: return this ;
073: else if (form instanceof SyntaxForm)
074: return (SyntaxForm) form;
075: else
076: return fromDatum(form);
077: }
078:
079: public static Expression rewrite(Object x) {
080: Translator tr = (Translator) Compilation.getCurrent();
081: return tr.rewrite(x);
082: }
083:
084: public static Expression rewriteBody(Object x) {
085: Translator tr = (Translator) Compilation.getCurrent();
086: return tr.rewrite_body(x);
087: }
088:
089: public boolean isIdentifier() {
090: return form instanceof String || form instanceof Symbol;
091: }
092:
093: public static boolean freeIdentifierEquals(SyntaxForm id1,
094: SyntaxForm id2) {
095: Translator tr = (Translator) Compilation.getCurrent();
096: return tr.lexical.lookup(id1.form, -1) == tr.lexical.lookup(
097: id2.form, -1);
098: }
099:
100: public void writeExternal(ObjectOutput out) throws IOException {
101: out.writeObject(form);
102: out.writeObject(scope);
103: }
104:
105: public void readExternal(ObjectInput in) throws IOException,
106: ClassNotFoundException {
107: form = in.readObject();
108: scope = (TemplateScope) in.readObject();
109: }
110: }
|