1 4 package kawa.standard; 5 import kawa.lang.*; 6 import gnu.lists.*; 7 import gnu.expr.*; 8 import gnu.math.*; 9 import gnu.bytecode.*; 10 11 public class define_unit extends Syntax 12 { 13 public static final define_unit define_unit = new define_unit(false); 14 static { define_unit.setName("define-unit"); } 15 public static final define_unit define_base_unit = new define_unit(true); 16 static { define_base_unit.setName("define-base-unit"); } 17 18 19 boolean base; 20 21 public define_unit (boolean base) 22 { 23 this.base = base; 24 } 25 26 public boolean scanForDefinitions (Pair st, java.util.Vector forms, 27 ScopeExp defs, Translator tr) 28 { 29 if (st.cdr instanceof Pair) 30 { 31 Pair p = (Pair) st.cdr; 32 Object q = p.car; 33 if (q instanceof String ) 34 { 35 String name = (String ) q; 36 String sym = (name + "$unit").intern(); 37 Declaration decl = defs.getDefine(sym, 'w', tr); 38 tr.push(decl); 39 Translator.setLine(decl, p); 40 decl.setFlag(Declaration.IS_CONSTANT); 41 if (defs instanceof ModuleExp) 42 decl.setCanRead(true); 43 Unit unit = null; 44 if (base && p.cdr == LList.Empty) 45 unit = BaseUnit.make(name, (String ) null); 46 else if (p.cdr instanceof Pair) 47 { 48 Object v = ((Pair) p.cdr).car; 49 if (base && v instanceof FString) 50 unit = BaseUnit.make(name, v.toString()); 51 else if (! base && v instanceof Quantity) 52 unit = Unit.make(name, (Quantity) v); 53 } 54 if (unit != null) 55 decl.noteValue(new QuoteExp(unit)); 56 p = Translator.makePair(p, decl, p.cdr); 57 st = Translator.makePair(st, this, p); 58 forms.addElement (st); 59 return true; 60 } 61 } 62 tr.error('e', "missing name in define-unit"); 63 return false; 64 } 65 66 public Expression rewriteForm (Pair form, Translator tr) 67 { 68 Object obj = form.cdr; 69 Expression value = null; 70 Pair p1; 71 72 if (! (obj instanceof Pair) 73 || ! ((p1 = (Pair) obj).car instanceof Declaration)) 74 return tr.syntaxError ("invalid syntax for "+getName()); 75 Declaration decl = (Declaration) p1.car; 76 String name = decl.getName(); 77 String unit = name.substring(0, name.length() - 5).intern(); ClassType unitType = ClassType.make("gnu.math.Unit"); 79 decl.setType(unitType); 80 if ((value = decl.getValue()) instanceof QuoteExp 81 && ((QuoteExp) value).getValue() instanceof Unit) 82 ; 83 else if (base) 84 { 85 String dimension = null; 86 if (p1.cdr != LList.Empty) 87 { 88 dimension = ((Pair) p1.cdr).car.toString(); 89 } 90 BaseUnit bunit = BaseUnit.make(unit, dimension); 91 value = new QuoteExp(bunit); 92 } 93 else 94 { 95 if (! (p1.cdr instanceof Pair)) 96 return tr.syntaxError("missing value for define-unit"); 97 Pair p2 = (Pair) p1.cdr; 98 value = tr.rewrite (p2.car); 99 Object quantity; 100 if (value instanceof QuoteExp 101 && (quantity = ((QuoteExp) value).getValue()) instanceof Quantity) 102 { 103 value = new QuoteExp(Unit.make(unit, (Quantity) quantity)); 104 } 105 else 106 { 107 Expression[] args = new Expression[2]; 108 args[0] = new QuoteExp(unit); 109 args[1] = value; 110 value = gnu.kawa.reflect.Invoke.makeInvokeStatic(unitType, "make", 111 args); 112 } 113 } 114 SetExp sexp = new SetExp(decl, value); 115 sexp.setDefining (true); 116 decl.noteValue(value); 117 return sexp; 118 } 119 } 120 | Popular Tags |