KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > lispexpr > LispLanguage


1 // Copyright (c) 2001, 2004, 2005 Per M.A. Bothner
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.lispexpr;
5 import gnu.expr.*;
6 import gnu.mapping.*;
7 import gnu.text.*;
8 import gnu.lists.*;
9 import gnu.bytecode.Access;
10 import gnu.bytecode.Field;
11 import gnu.mapping.EnvironmentKey;
12 import gnu.kawa.reflect.StaticFieldLocation;
13 import kawa.lang.Translator; // FIXME
14
import kawa.lang.Syntax; // FIXME
15

16 /** Language sub-class for Lisp-like languages (including Scheme). */
17
18 public abstract class LispLanguage extends Language
19 {
20   static public final String JavaDoc quote_sym = "quote";
21   static public final String JavaDoc unquote_sym = "unquote";
22   static public final String JavaDoc unquotesplicing_sym = "unquote-splicing";
23   static public final String JavaDoc quasiquote_sym = "quasiquote";
24   /** Used for Kawa infix ':' operator. */
25   static public final String JavaDoc lookup_sym = "$lookup$";
26
27   /** The default <code>ReadTable</code> for this language. */
28   public ReadTable defaultReadTable = createReadTable();
29
30   /** Create a fresh <code>ReadTable</code> appropriate for this language. */
31   public abstract ReadTable createReadTable ();
32
33   public Lexer getLexer(InPort inp, SourceMessages messages)
34   {
35     return new LispReader(inp, messages);
36   }
37
38   public Compilation getCompilation (Lexer lexer, SourceMessages messages)
39   {
40     return new Translator (this, messages);
41   }
42
43   public boolean parse (Compilation comp, int options)
44     throws java.io.IOException JavaDoc, gnu.text.SyntaxException
45   {
46     kawa.lang.Translator tr = (kawa.lang.Translator) comp;
47     Lexer lexer = tr.lexer;
48     ModuleExp mexp = tr.mainLambda;
49     Values forms = new Values();
50     LispReader reader = (LispReader) lexer;
51     Compilation save_comp = Compilation.getCurrent();
52     try
53       {
54         Compilation.setCurrent(tr);
55         if (tr.pendingForm != null)
56           {
57             tr.scanForm(tr.pendingForm, mexp);
58             tr.pendingForm = null;
59           }
60         for (;;)
61           {
62             Object JavaDoc sexp = reader.readCommand();
63             if (sexp == Sequence.eofValue)
64               {
65                 if ((options & PARSE_ONE_LINE) != 0)
66                   return false; // FIXME
67
break;
68               }
69             tr.scanForm(sexp, mexp);
70             if ((options & PARSE_ONE_LINE) != 0)
71               break;
72             if ((options & PARSE_PROLOG) != 0
73                 && tr.getState() >= Compilation.PROLOG_PARSED)
74               {
75                 return true;
76               }
77           }
78         if (lexer.peek() == ')')
79           lexer.fatal("An unexpected close paren was read.");
80
81         // Must be done before any other module imports this module.
82
tr.finishModule(mexp);
83
84         if ((options & PARSE_PROLOG) == 0)
85           {
86             tr.firstForm = 0;
87           }
88         tr.setState(Compilation.BODY_PARSED);
89       }
90     finally
91       {
92         Compilation.setCurrent(save_comp);
93       }
94     return true;
95   }
96
97   /** Resolve names and other post-parsing processing. */
98   public void resolve (Compilation comp)
99   {
100     Translator tr = (Translator) comp;
101     tr.resolveModule(tr.getModule());
102   }
103
104   public Declaration declFromField (ModuleExp mod, Object JavaDoc fvalue, Field fld)
105   {
106     Declaration fdecl = super.declFromField(mod, fvalue, fld);
107     boolean isFinal = (fld.getModifiers() & Access.FINAL) != 0;
108     if (isFinal && fvalue instanceof Syntax) // FIXME - should check type? not value?
109
fdecl.setSyntax();
110     return fdecl;
111   }
112
113   /** Declare in the current Environment a Syntax bound to a static field.
114    * @param name the procedure's source-level name.
115    * @param cname the name of the class containing the field.
116    * @param fname the name of the field, which should be a static
117    * final field whose type extends kawa.lang.Syntax.
118    */

119   protected void defSntxStFld(String JavaDoc name, String JavaDoc cname, String JavaDoc fname)
120   {
121     Object JavaDoc property
122       = hasSeparateFunctionNamespace() ? EnvironmentKey.FUNCTION : null;
123     StaticFieldLocation loc =
124       StaticFieldLocation.define(environ, environ.getSymbol(name), property,
125                  cname, fname);
126     loc.setSyntax();
127   }
128
129   protected void defSntxStFld(String JavaDoc name, String JavaDoc cname)
130   {
131     defSntxStFld(name, cname, Compilation.mangleNameIfNeeded(name));
132   }
133
134   /** Combine a <body> consisting of a list of expression. */
135   public Expression makeBody(Expression[] exps)
136   {
137     return new BeginExp (exps);
138   }
139
140   public Expression makeApply (Expression func, Expression[] args)
141   {
142     return new ApplyExp(func, args);
143   }
144
145   public boolean selfEvaluatingSymbol (Object JavaDoc obj)
146   {
147     return obj instanceof Keyword;
148   }
149
150   /** Convert the Language's idea of a symbol to a gnu.mapping.Symbol. */
151   public static Symbol langSymbolToSymbol (Object JavaDoc sym)
152   {
153     return ((LispLanguage) Language.getDefaultLanguage()).fromLangSymbol(sym);
154   }
155
156   protected Symbol fromLangSymbol (Object JavaDoc sym)
157   {
158     if (sym instanceof String JavaDoc)
159       return getSymbol((String JavaDoc) sym);
160     return (Symbol) sym;
161   }
162 }
163
Popular Tags