Contexto.java

Go to the documentation of this file.
00001 package plp.expressions2.memory;
00002 
00003 import java.util.HashMap;
00004 import java.util.Stack;
00005 
00006 import plp.expressions2.expression.Id;
00007 
00014 public class Contexto<T> {
00018         private Stack<HashMap<Id,T>> pilha;
00019 
00023         public Contexto() {
00024                 pilha = new Stack<HashMap<Id,T>>();
00025         }
00026 
00027         public void incrementa(){
00028                 pilha.push(new HashMap<Id,T>());
00029         }
00030 
00031         public void restaura(){
00032                 pilha.pop();
00033         }
00034 
00041         public void map(Id idArg, T valorId) throws VariavelJaDeclaradaException {
00042                         HashMap<Id,T> aux =  pilha.peek();
00043                 if (aux.put(idArg, valorId) != null) {
00044                         throw new VariavelJaDeclaradaException (idArg);
00045                 }
00046         }
00047 
00054         public T get(Id idArg) throws VariavelNaoDeclaradaException {
00055                 try {
00056                         T result = null;
00057                         Stack<HashMap<Id,T>> auxStack = new Stack<HashMap<Id,T>>();
00058                         while (result == null && !pilha.empty()) {
00059                                 HashMap<Id,T> aux = pilha.pop();
00060                                 auxStack.push(aux);
00061                                 result = (T) aux.get(idArg);
00062                         }
00063                         while (!auxStack.empty()) {
00064                                 pilha.push(auxStack.pop());
00065                         }
00066                         if (result == null) {
00067                                 throw new IdentificadorNaoDeclaradoException();
00068                         } 
00069                         
00070                         return result;
00071                 } catch (IdentificadorNaoDeclaradoException e) {
00072                         throw new VariavelNaoDeclaradaException(idArg);
00073                 }
00074         }
00075 
00080         public Stack<HashMap<Id,T>> getPilha() {
00081                 return pilha;
00082         }
00083 
00088         public void setPilha(Stack<HashMap<Id,T>> pilha) {
00089                 this.pilha = pilha;
00090         }
00091 
00092 }

Generated on Tue Sep 12 21:36:02 2006 for PLP by  doxygen 1.4.7