00001 package plp.imperative1.memory; 00002 00003 import java.util.HashMap; 00004 import java.util.Stack; 00005 00006 import plp.expressions2.expression.Id; 00007 import plp.expressions2.expression.Valor; 00008 import plp.expressions2.memory.ContextoExecucao; 00009 import plp.expressions2.memory.VariavelNaoDeclaradaException; 00010 00011 public class ContextoExecucaoImperativa extends ContextoExecucao 00012 implements AmbienteExecucaoImperativa { 00013 00017 private ListaValor entrada; 00018 00022 private ListaValor saida; 00023 00027 public ContextoExecucaoImperativa(ListaValor entrada){ 00028 super(); 00029 this.entrada = entrada; 00030 this.saida = new ListaValor(); 00031 } 00032 00033 public Valor read() throws EntradaVaziaException { 00034 if(entrada == null || entrada.getHead() == null) { 00035 throw new EntradaVaziaException(); 00036 } 00037 Valor aux = entrada.getHead(); 00038 entrada = (ListaValor) entrada.getTail(); 00039 return aux; 00040 } 00041 00042 public ListaValor getSaida() { 00043 return saida; 00044 } 00045 00046 public void write(Valor v){ 00047 saida = new ListaValor(v, saida); 00048 } 00049 00056 public void changeValor(Id idArg, Valor valorId) 00057 throws VariavelNaoDeclaradaException { 00058 00059 Object result = null; 00060 Stack<HashMap<Id,Valor>> auxStack = new Stack<HashMap<Id,Valor>>(); 00061 Stack<HashMap<Id,Valor>> stack = this.getPilha(); 00062 00063 while (result == null && !stack.empty()) { 00064 HashMap<Id,Valor> aux = stack.pop(); 00065 auxStack.push(aux); 00066 result = aux.get(idArg); 00067 if (result != null) { 00068 aux.put(idArg, valorId); 00069 } 00070 } 00071 while (!auxStack.empty()) { 00072 stack.push(auxStack.pop()); 00073 } 00074 if (result == null) { 00075 throw new VariavelNaoDeclaradaException(idArg); 00076 } 00077 } 00078 } 00079