KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > standard > define_variable


1 package kawa.standard;
2 import kawa.lang.*;
3 import gnu.mapping.*;
4 import gnu.expr.*;
5 import gnu.lists.*;
6
7 /** "define-variable" is like define, but ignored if variable already bound. */
8
9 public class define_variable extends Syntax
10 {
11   public static final define_variable define_variable = new define_variable();
12   static { define_variable.setName("define-variable"); }
13
14   public boolean scanForDefinitions (Pair st, java.util.Vector JavaDoc forms,
15                                      ScopeExp defs, Translator tr)
16   {
17     if (! (st.cdr instanceof Pair))
18       return super.scanForDefinitions(st, forms, defs, tr);
19     Pair p = (Pair) st.cdr;
20     if (p.car instanceof String JavaDoc || p.car instanceof Symbol)
21       {
22     Object JavaDoc sym = p.car;
23     if (! (defs instanceof ModuleExp))
24       tr.error('e', getName() + " must be at module level");
25     Declaration decl = defs.lookup(sym);
26     if (decl != null)
27       tr.error('e', "duplicate declaration for '"+sym+"'");
28     ModuleExp mod = defs.currentModule();
29     decl = mod.addDeclaration(sym);
30     tr.push(decl);
31     decl.setSimple(false);
32     decl.setPrivate(true);
33     decl.setFlag(Declaration.IS_DYNAMIC);
34     if (mod.isStatic())
35       decl.setFlag(Declaration.STATIC_SPECIFIED);
36     decl.setCanRead(true);
37     decl.setCanWrite(true);
38     decl.setIndirectBinding(true);
39     p = Translator.makePair(p, decl, p.cdr);
40     st = Translator.makePair(st, this, p);
41       }
42     forms.addElement (st);
43     return true;
44   }
45
46   public Expression rewriteForm (Pair form, Translator tr)
47   {
48     Object JavaDoc obj = form.cdr;
49     Expression value = null;
50     Declaration decl = null;
51
52     if (obj instanceof Pair)
53       {
54     Pair p1 = (Pair) obj;
55     obj = p1.car;
56     if (obj instanceof String JavaDoc || obj instanceof Symbol)
57       return tr.syntaxError(getName() + " is only allowed in a <body>");
58     if (obj instanceof Declaration)
59       {
60         decl = (Declaration) p1.car;
61         obj = p1.cdr;
62         if (obj instanceof Pair
63         && (p1 = (Pair) obj).cdr == LList.Empty)
64           value = tr.rewrite (p1.car);
65         else if (obj != LList.Empty)
66           decl = null;
67       }
68       }
69     if (decl == null)
70       return tr.syntaxError ("invalid syntax for "+getName());
71     if (value == null)
72       return QuoteExp.voidExp;
73     SetExp sexp = new SetExp (decl, value);
74     sexp.setDefining (true);
75     sexp.setSetIfUnbound(true);
76     
77     if (decl != null)
78       {
79     sexp.setBinding(decl);
80     if (decl.context instanceof ModuleExp
81         && decl.getCanWrite())
82       value = null;
83     decl.noteValue(value);
84       }
85     return sexp;
86   }
87 }
88
Popular Tags