1 package tinybasic; 2 3 import java.util.*; 4 5 public class Context { 6 7 protected Scope theGlobalScope=null; 8 protected Scope theProgramScope=null; 9 protected Stack theScopeStack; 10 11 Hashtable subroutineTable; 12 Hashtable functionTable; 13 Context()14 public Context(){ 15 theGlobalScope=new GlobalScope(null); 16 theScopeStack=new Stack(); 17 theScopeStack.push(theGlobalScope); 18 subroutineTable = new Hashtable(); 19 functionTable = new Hashtable(); 20 } 21 insertSubroutine(String v,DTCodeType t)22 void insertSubroutine(String v,DTCodeType t){ 23 subroutineTable.put(v.toLowerCase(),t); 24 } 25 getSubroutine(String v)26 public DTCodeType getSubroutine(String v){ 27 DTCodeType t=(DTCodeType)subroutineTable.get(v.toLowerCase()); 28 return t; 29 } 30 insertFunction(String v,DTCodeType t)31 void insertFunction(String v,DTCodeType t){ 32 functionTable.put(v.toLowerCase(),t); 33 } 34 getFunction(String v)35 public DTCodeType getFunction(String v){ 36 DTCodeType t=(DTCodeType)functionTable.get(v.toLowerCase()); 37 return t; 38 } 39 40 insertGlobalVariable(String v,DTDataType t)41 void insertGlobalVariable(String v,DTDataType t){ 42 theGlobalScope.insertVariable(v,t); 43 } 44 insertVariable(String v,DTDataType t)45 void insertVariable(String v,DTDataType t){ 46 getCurrentScope().insertVariable(v,t); 47 } 48 getVariable(String var)49 public DTDataType getVariable(String var){ 50 DTDataType t=getCurrentScope().getVariable(var); 51 if(t==null){ 52 t=theGlobalScope.getVariable(var); 53 } 54 return t; 55 } 56 getVariableDimension(String var)57 public int getVariableDimension(String var){ 58 int dim=0; 59 dim=getCurrentScope().getVariableDimension(var); 60 if(dim==0){ 61 dim=theGlobalScope.getVariableDimension(var); 62 } 63 return dim; 64 } 65 getVariableType(String var)66 public int getVariableType(String var){ 67 DTDataType t=null; 68 t=getCurrentScope().getVariable(var); 69 if(t==null){ 70 t=theGlobalScope.getVariable(var); 71 } 72 73 if(t!=null){ 74 return t.getType(); 75 } else { 76 return 0; 77 } 78 } 79 isArrayVariable(String s)80 public boolean isArrayVariable(String s){ 81 return (getVariableDimension(s) > 0); 82 } 83 getPrev()84 public Scope getPrev(){ 85 return getCurrentScope().getPrev(); 86 } 87 pushScope(Scope scope)88 public void pushScope(Scope scope){ 89 theScopeStack.push(scope); 90 } 91 popScope()92 public Scope popScope(){ 93 if( getCurrentScope() == theGlobalScope ){ 94 return theGlobalScope; 95 } else { 96 return (Scope)theScopeStack.pop(); 97 } 98 } 99 getCurrentScope()100 public Scope getCurrentScope(){ 101 return ((Scope)theScopeStack.peek()); 102 } 103 getGlobalScope()104 public Scope getGlobalScope(){ 105 return theGlobalScope; 106 } 107 setProgramScope(Scope scope)108 public void setProgramScope(Scope scope){ 109 theProgramScope=scope; 110 while((Scope)theScopeStack.peek()!=theGlobalScope){ 111 theScopeStack.pop(); 112 } 113 theScopeStack.push(theProgramScope); 114 } 115 getProgramScope()116 protected Scope getProgramScope(){ 117 return theProgramScope; 118 } 119 setDimension(String s,DTDataType i1)120 public void setDimension(String s,DTDataType i1){ 121 DTDataType v=getVariable(s); 122 v.setDimension(i1.getInteger()); 123 } 124 initialize()125 public void initialize(){ 126 setProgramScope(getProgramScope()); 127 } setDimension(String s,DTDataType i1,DTDataType i2)128 public void setDimension(String s,DTDataType i1,DTDataType i2){ 129 DTDataType v=getVariable(s); 130 v.setDimension(i1.getInteger(),i2.getInteger()); 131 } setDimension(String s,DTDataType i1,DTDataType i2,DTDataType i3)132 public void setDimension(String s,DTDataType i1,DTDataType i2,DTDataType i3){ 133 DTDataType v=getVariable(s); 134 v.setDimension(i1.getInteger(),i2.getInteger(),i3.getInteger()); 135 } 136 137 getDTDataType(String s,DTDataType i1)138 public DTDataType getDTDataType(String s,DTDataType i1){ 139 DTDataType t=getVariable(s); 140 return t.getDTDataType(i1); 141 } 142 getDTDataType(String s,DTDataType i1,DTDataType i2)143 public DTDataType getDTDataType(String s,DTDataType i1,DTDataType i2){ 144 DTDataType t=getVariable(s); 145 return t.getDTDataType(i1,i2); 146 } 147 getDTDataType(String s,DTDataType i1,DTDataType i2,DTDataType i3)148 public DTDataType getDTDataType(String s,DTDataType i1,DTDataType i2,DTDataType i3){ 149 DTDataType t=getVariable(s); 150 return t.getDTDataType(i1,i2,i3); 151 } 152 ensureVariable(String s,int t)153 public DTDataType ensureVariable(String s,int t){ 154 DTDataType v = getVariable(s); 155 if(v==null){ 156 v=DTDataType.getOne(t,getCurrentScope()); 157 insertVariable(s,v); 158 } 159 return v; 160 } 161 162 163 164 //public boolean isArrayVariable(String s){ 165 // if(getCurrentScope().isArrayVariable(s){ 166 // return true; 167 // } else { 168 // return theGlobalScope.isArrayVariable(s); 169 //} 170 //} 171 172 } 173