01: package gnu.commonlisp.lang;
02:
03: import kawa.lang.*;
04: import gnu.lists.*;
05: import gnu.expr.*;
06: import gnu.mapping.Symbol;
07:
08: public class defvar extends Syntax {
09: /** True for defconst, false for defvar. */
10: boolean force;
11:
12: public defvar(boolean force) {
13: this .force = force;
14: }
15:
16: public boolean scanForDefinitions(Pair st, java.util.Vector forms,
17: ScopeExp defs, Translator tr) {
18: if (!(st.cdr instanceof Pair))
19: return super .scanForDefinitions(st, forms, defs, tr);
20: Pair p = (Pair) st.cdr;
21: Object name = p.car;
22: if (name instanceof String || name instanceof Symbol) {
23: Declaration decl = defs.lookup(name);
24: if (decl == null) {
25: decl = new Declaration(name);
26: decl.setFlag(Declaration.IS_DYNAMIC);
27: defs.addDeclaration(decl);
28: } else
29: tr.error('w', "duplicate declaration for `" + name
30: + "'");
31: p = Translator.makePair(p, decl, p.cdr);
32: st = Translator.makePair(st, this , p);
33: if (defs instanceof ModuleExp) {
34: decl.setCanRead(true);
35: decl.setCanWrite(true);
36: }
37: }
38: forms.addElement(st);
39: return true;
40: }
41:
42: public Expression rewriteForm(Pair form, Translator tr) {
43: Object obj = form.cdr;
44: Object name = null;
45: Expression value = null;
46: Declaration decl = null;
47:
48: if (obj instanceof Pair) {
49: Pair p1 = (Pair) obj;
50: if (p1.car instanceof Declaration) {
51: decl = (Declaration) p1.car;
52: name = decl.getSymbol();
53: if (p1.cdr instanceof Pair) {
54: Pair p2 = (Pair) p1.cdr;
55: value = tr.rewrite(p2.car);
56: if (p2.cdr != LList.Empty) {
57: // Handle documentation string. FIXME.
58: }
59: } else if (p1.cdr != LList.Empty)
60: name = null;
61: }
62: }
63: if (name == null)
64: return tr.syntaxError("invalid syntax for " + getName());
65: if (value == null) {
66: if (force)
67: value = CommonLisp.nilExpr;
68: else
69: return new QuoteExp(name);
70: }
71: SetExp sexp = new SetExp(name, value);
72: if (!force)
73: sexp.setSetIfUnbound(true);
74: sexp.setDefining(true);
75: if (decl != null) {
76: sexp.setBinding(decl);
77: if (decl.context instanceof ModuleExp && decl.getCanWrite())
78: value = null;
79: decl.noteValue(value);
80: }
81: return sexp;
82: }
83:
84: }
|