00001 package plp.orientadaObjetos1.memoria;
00002
00003 import java.util.HashMap;
00004 import java.util.Stack;
00005
00006 import plp.expressions2.expression.Id;
00007 import plp.expressions2.memory.VariavelJaDeclaradaException;
00008 import plp.expressions2.memory.VariavelNaoDeclaradaException;
00009 import plp.orientadaObjetos1.declaracao.procedimento.ListaDeclaracaoParametro;
00010 import plp.orientadaObjetos1.excecao.declaracao.ClasseJaDeclaradaException;
00011 import plp.orientadaObjetos1.excecao.declaracao.ClasseNaoDeclaradaException;
00012 import plp.orientadaObjetos1.excecao.declaracao.ProcedimentoJaDeclaradoException;
00013 import plp.orientadaObjetos1.excecao.declaracao.ProcedimentoNaoDeclaradoException;
00014 import plp.orientadaObjetos1.memoria.colecao.ListaValor;
00015 import plp.orientadaObjetos1.util.Tipo;
00019 public class ContextoCompilacaoOO1 implements AmbienteCompilacaoOO1 {
00020
00025 private Stack<HashMap<Id, Tipo>> pilha;
00026
00030 private Stack<HashMap<Id, ListaDeclaracaoParametro>> pilhaProcedimento;
00031
00032
00037 private HashMap<Id, DefClasse> mapDefClasse;
00038
00042 private ListaValor entrada;
00043
00047 public ContextoCompilacaoOO1(ListaValor entrada){
00048 pilha = new Stack<HashMap<Id, Tipo>>();
00049 pilhaProcedimento = new Stack<HashMap<Id, ListaDeclaracaoParametro>>();
00050 mapDefClasse = new HashMap<Id, DefClasse>();
00051 this.entrada = entrada;
00052 }
00053
00057 public void incrementa(){
00058 pilha.push(new HashMap<Id, Tipo>());
00059 pilhaProcedimento.push(new HashMap<Id, ListaDeclaracaoParametro>());
00060 }
00061
00065 public void restaura(){
00066 pilha.pop();
00067 pilhaProcedimento.pop();
00068 }
00075 public void map(Id idArg, Tipo tipoId)
00076 throws VariavelJaDeclaradaException {
00077 HashMap<plp.expressions2.expression.Id, Tipo> aux = pilha.peek();
00078 if (aux.put(idArg, tipoId) != null) {
00079 throw new VariavelJaDeclaradaException(idArg);
00080 }
00081 }
00082
00090 public void mapParametrosProcedimento(Id idArg, ListaDeclaracaoParametro parametrosId)
00091 throws ProcedimentoJaDeclaradoException {
00092 HashMap<Id, ListaDeclaracaoParametro> aux = pilhaProcedimento.peek();
00093 if (aux.put(idArg, parametrosId) != null) {
00094 throw new ProcedimentoJaDeclaradoException(idArg);
00095 }
00096 }
00097
00104 public void mapDefClasse(Id idArg, DefClasse defClasse)
00105 throws ClasseJaDeclaradaException {
00106 if (mapDefClasse.put(idArg, defClasse) != null) {
00107 throw new ClasseJaDeclaradaException(idArg);
00108 }
00109 }
00110
00117 public Tipo get(Id idArg)
00118 throws VariavelNaoDeclaradaException {
00119 Tipo result = null;
00120 Stack<HashMap<Id, Tipo>> auxStack = new Stack<HashMap<Id, Tipo>>();
00121 while (result == null && !pilha.empty()) {
00122 HashMap<Id, Tipo> aux = pilha.pop();
00123 auxStack.push(aux);
00124 result = aux.get(idArg);
00125 }
00126 while (!auxStack.empty()) {
00127 pilha.push(auxStack.pop());
00128 }
00129 if (result == null) {
00130 throw new VariavelNaoDeclaradaException(idArg);
00131 } else {
00132 return result;
00133 }
00134 }
00135
00145 public ListaDeclaracaoParametro getParametrosProcedimento(Id idArg)
00146 throws ProcedimentoNaoDeclaradoException {
00147 ListaDeclaracaoParametro result = null;
00148 Stack<HashMap<Id, ListaDeclaracaoParametro>> auxStack = new Stack<HashMap<Id, ListaDeclaracaoParametro>>();
00149 while (result == null && !pilhaProcedimento.empty()) {
00150 HashMap<Id, ListaDeclaracaoParametro> aux = pilhaProcedimento.pop();
00151 auxStack.push(aux);
00152 result = aux.get(idArg);
00153 }
00154 while (!auxStack.empty()) {
00155 pilhaProcedimento.push(auxStack.pop());
00156 }
00157 if (result == null) {
00158 throw new ProcedimentoNaoDeclaradoException(idArg);
00159 } else {
00160 return result;
00161 }
00162 }
00163
00171 public DefClasse getDefClasse(Id idArg)
00172 throws ClasseNaoDeclaradaException {
00173 DefClasse result = null;
00174 result = this.mapDefClasse.get(idArg);
00175 if (result == null) {
00176 throw new ClasseNaoDeclaradaException(idArg);
00177 } else {
00178 return result;
00179 }
00180 }
00181
00188 public Tipo getTipoEntrada()
00189 throws VariavelNaoDeclaradaException {
00190 Tipo aux = entrada.getHead().getTipo(this);
00191 entrada = (ListaValor)entrada.getTail();
00192 return aux;
00193 }
00194
00195 }
00196