1 package gnu.commonlisp.lang; 2 import kawa.lang.*; 3 import gnu.lists.*; 4 import gnu.expr.*; 5 import gnu.mapping.Symbol; 6 7 public class defvar extends Syntax 8 { 9 10 boolean force; 11 12 public defvar(boolean force) 13 { 14 this.force = force; 15 } 16 17 public boolean scanForDefinitions (Pair st, java.util.Vector forms, 18 ScopeExp defs, Translator tr) 19 { 20 if (! (st.cdr instanceof Pair)) 21 return super.scanForDefinitions(st, forms, defs, tr); 22 Pair p = (Pair) st.cdr; 23 Object name = p.car; 24 if (name instanceof String || name instanceof Symbol) 25 { 26 Declaration decl = defs.lookup(name); 27 if (decl == null) 28 { 29 decl = new Declaration(name); 30 decl.setFlag(Declaration.IS_DYNAMIC); 31 defs.addDeclaration(decl); 32 } 33 else 34 tr.error('w', "duplicate declaration for `" + name + "'"); 35 p = Translator.makePair(p, decl, p.cdr); 36 st = Translator.makePair(st, this, p); 37 if (defs instanceof ModuleExp) 38 { 39 decl.setCanRead(true); 40 decl.setCanWrite(true); 41 } 42 } 43 forms.addElement (st); 44 return true; 45 } 46 47 public Expression rewriteForm (Pair form, Translator tr) 48 { 49 Object obj = form.cdr; 50 Object name = null; 51 Expression value = null; 52 Declaration decl = null; 53 54 if (obj instanceof Pair) 55 { 56 Pair p1 = (Pair) obj; 57 if (p1.car instanceof Declaration) 58 { 59 decl = (Declaration) p1.car; 60 name = decl.getSymbol(); 61 if (p1.cdr instanceof Pair) 62 { 63 Pair p2 = (Pair) p1.cdr; 64 value = tr.rewrite (p2.car); 65 if (p2.cdr != LList.Empty) 66 { 67 } 69 } 70 else if (p1.cdr != LList.Empty) 71 name = null; 72 } 73 } 74 if (name == null) 75 return tr.syntaxError ("invalid syntax for "+getName()); 76 if (value == null) 77 { 78 if (force) 79 value = CommonLisp.nilExpr; 80 else 81 return new QuoteExp(name); 82 } 83 SetExp sexp = new SetExp (name, value); 84 if (! force) 85 sexp.setSetIfUnbound(true); 86 sexp.setDefining (true); 87 if (decl != null) 88 { 89 sexp.setBinding(decl); 90 if (decl.context instanceof ModuleExp 91 && decl.getCanWrite()) 92 value = null; 93 decl.noteValue(value); 94 } 95 return sexp; 96 } 97 98 } 99 | Popular Tags |