1 9 package org.nfunk.jep; 10 import java.util.*; 11 12 36 public class SymbolTable extends Hashtable 37 { 38 protected VariableFactory vf; 39 40 public SymbolTable(VariableFactory varFac) 41 { 42 vf = varFac; 43 } 44 45 private SymbolTable() {} 46 47 50 public Object get(Object key) { return getValue(key); } 51 52 53 55 public Object getValue(Object key) 56 { 57 Variable var = (Variable) super.get(key); 58 if(var==null) return null; 59 return var.getValue(); 60 } 61 62 64 public Variable getVar(String name) 65 { 66 return (Variable) super.get(name); 67 } 68 69 72 public Object put(Object key,Object val) 73 { 74 return makeVarIfNeeded((String ) key,val); 75 } 76 77 81 public boolean setVarValue(String name,Object val) 82 { 83 Variable var = (Variable) super.get(name); 84 if(var != null) 85 { 86 return var.setValue(val); 87 } 88 else return false; 89 } 90 91 94 public Variable addVariable(String name,Object val) 95 { 96 Variable var = (Variable) super.get(name); 97 if(var != null) return null; 98 else 99 { 100 var = vf.createVariable(name,val); 101 super.put(name,var); 102 } 103 var.setValidValue(true); 104 return var; 105 } 106 107 110 public Variable addConstant(String name,Object val) 111 { 112 Variable var = addVariable(name,val); 113 if(var != null) var.setIsConstant(true); 114 return var; 115 } 116 117 121 public Variable makeVarIfNeeded(String name,Object val) 122 { 123 Variable var = (Variable) super.get(name); 124 if(var != null) 125 { 126 var.setValue(val); 127 return var; 128 } 129 else 130 { 131 var = vf.createVariable(name,val); 132 super.put(name,var); 133 return var; 134 } 135 } 136 137 141 public Variable makeVarIfNeeded(String name) 142 { 143 Variable var = (Variable) super.get(name); 144 if(var != null) return var; 145 146 var = vf.createVariable(name,null); 147 super.put(name,var); 148 return var; 149 } 150 151 154 public String toString() 155 { 156 StringBuffer sb = new StringBuffer (); 157 for(Enumeration e = this.elements(); e.hasMoreElements(); ) 158 { 159 Variable var = (Variable) e.nextElement(); 160 sb.append(var.toString()); 161 sb.append("\n"); 162 } 163 return sb.toString(); 164 } 165 166 171 public void clearValues() 172 { 173 for(Enumeration e = this.elements(); e.hasMoreElements(); ) 174 { 175 Variable var = (Variable) e.nextElement(); 176 if(!var.isConstant()) var.setValidValue(false); 177 } 178 } 179 182 public VariableFactory getVariableFactory() { 183 return vf; 184 } 185 186 } 187 | Popular Tags |