KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > standard > define


1 package kawa.standard;
2 import kawa.lang.*;
3 import gnu.mapping.*;
4 import gnu.expr.*;
5 import gnu.lists.*;
6
7 /**
8  * The Syntax transformer that re-writes the "%define" internal form.
9  * This is used to implement define, define-private, and define-constant.
10  * Syntax: <code>(%define name code type value)</code>.
11  * The <code>name</code> is an identifier (<code>String</code> or
12  * <code>Symbol</code>) or </code>Declaration</code>.
13  * The <code>code</code> is an integer mask,
14  * where 1 means type specified, 2 means a function definition,
15  * 4 means private, and 8 means constant.
16  * The <code>type</code> is the declarated type or <code>null</code>.
17  * The <code>value</code> is the initializing value. *
18  * @author Per Bothner
19  */

20
21 public class define extends Syntax
22 {
23   public static final define defineRaw = new define(Scheme.lambda);
24
25   Lambda lambda;
26
27   String JavaDoc getName (int options)
28   {
29     if ((options & 4) != 0)
30       return "define-private";
31     else if ((options & 8) != 0)
32       return "define-constant";
33     else
34       return "define";
35   }
36
37   public define(Lambda lambda)
38   {
39     this.lambda = lambda;
40   }
41
42   public void scanForm (Pair st, ScopeExp defs, Translator tr)
43   {
44     Pair p1 = (Pair) st.cdr;
45     Pair p2 = (Pair) p1.cdr;
46     Pair p3 = (Pair) p2.cdr;
47     Pair p4 = (Pair) p3.cdr;
48     SyntaxForm nameSyntax = null;
49     Object JavaDoc name = p1.car;
50     while (name instanceof SyntaxForm)
51       {
52     nameSyntax = (SyntaxForm) name;
53     name = nameSyntax.form;
54       }
55     int options = ((Number JavaDoc) Translator.stripSyntax(p2.car)).intValue();
56     boolean makePrivate = (options & 4) != 0;
57     boolean makeConstant = (options & 8) != 0;
58
59     ScopeExp scope = tr.currentScope();
60     name = tr.namespaceResolve(name);
61     if (! (name instanceof String JavaDoc || name instanceof Symbol))
62       {
63         tr.error('e', "'"+name+"' is not a valid identifier");
64         name = null;
65       }
66
67     Object JavaDoc savePos = tr.pushPositionOf(p1);
68     Declaration decl = tr.define(name, nameSyntax, defs);
69     tr.popPositionOf(savePos);
70     name = decl.getSymbol();
71     if (makePrivate)
72       {
73     decl.setFlag(Declaration.PRIVATE_SPECIFIED);
74     decl.setPrivate(true);
75       }
76     if (makeConstant)
77       decl.setFlag(Declaration.IS_CONSTANT);
78
79     if ((options & 2) != 0)
80       {
81     LambdaExp lexp = new LambdaExp();
82     decl.setProcedureDecl(true);
83         decl.setType(Compilation.typeProcedure);
84     lexp.setSymbol(name);
85     lexp.nameDecl = decl;
86     Object JavaDoc formals = p4.car;
87     Object JavaDoc body = p4.cdr;
88     Translator.setLine(lexp, p1);
89     lambda.rewriteFormals(lexp, formals, tr, null);
90     Object JavaDoc realBody = lambda.rewriteAttrs(lexp, body, tr);
91     if (realBody != body)
92       p2 = new Pair(p2.car, new Pair(p3.car, new Pair(formals, realBody)));
93     decl.noteValue(lexp);
94       }
95
96     if (defs instanceof ModuleExp)
97       {
98     if (! makePrivate)
99       {
100         decl.setCanRead(true);
101         // (define (f) ...) defaults f to being read-only,
102
// unless f is assigned to in this module.
103
if (! makeConstant
104         && ((options & 2) == 0
105             || ! Compilation.inlineOk))
106           decl.setCanWrite(true);
107       }
108       }
109
110     if ((options & 1) != 0)
111       {
112     decl.setType(tr.exp2Type(p3));
113     decl.setFlag(Declaration.TYPE_SPECIFIED);
114       }
115
116     st = Translator.makePair(st, this,
117                  Translator.makePair(p1, decl, p2));
118     Translator.setLine(decl, p1);
119
120     tr.formStack.addElement(st);
121   }
122
123   public Expression rewriteForm (Pair form, Translator tr)
124   {
125     Pair p1 = (Pair) form.cdr;
126     Pair p2 = (Pair) p1.cdr;
127     Pair p3 = (Pair) p2.cdr;
128     Pair p4 = (Pair) p3.cdr;
129     Object JavaDoc name = Translator.stripSyntax(p1.car);
130     int options = ((Number JavaDoc) Translator.stripSyntax(p2.car)).intValue();
131     boolean makePrivate = (options & 4) != 0;
132
133     if (! (name instanceof Declaration))
134       return tr.syntaxError(getName(options) + " is only allowed in a <body>");
135     Declaration decl = (Declaration) name;
136
137     Expression value;
138     if ((options & 2) != 0)
139       {
140     LambdaExp lexp = (LambdaExp) decl.getValue();
141     Object JavaDoc body = p4.cdr;
142     lambda.rewriteBody(lexp, body, tr);
143     value = lexp;
144       }
145     else
146       {
147     value = tr.rewrite (p4.car);
148     decl.noteValue((decl.context instanceof ModuleExp && ! makePrivate
149             && decl.getCanWrite())
150                ? null : value);
151       }
152     SetExp sexp = new SetExp(decl, value);
153     sexp.setDefining (true);
154     if (makePrivate && ! (tr.currentScope() instanceof ModuleExp))
155       tr.error('w', "define-private not at top level "
156            +tr.currentScope());
157     return sexp;
158   }
159 }
160
Popular Tags