00001 package plp.orientadaObjetos1.memoria;
00002
00003 import plp.orientadaObjetos1.declaracao.procedimento.ListaDeclaracaoParametro;
00004 import plp.orientadaObjetos1.excecao.declaracao.ClasseJaDeclaradaException;
00005 import plp.orientadaObjetos1.excecao.declaracao.ClasseNaoDeclaradaException;
00006 import plp.orientadaObjetos1.excecao.declaracao.ProcedimentoJaDeclaradoException;
00007 import plp.orientadaObjetos1.excecao.declaracao.ProcedimentoNaoDeclaradoException;
00008 import plp.orientadaObjetos1.excecao.declaracao.VariavelJaDeclaradaException;
00009 import plp.orientadaObjetos1.excecao.declaracao.VariavelNaoDeclaradaException;
00010 import plp.orientadaObjetos1.expressao.leftExpression.Id;
00011 import plp.orientadaObjetos1.memoria.colecao.HashIdDefClasse;
00012 import plp.orientadaObjetos1.memoria.colecao.HashIdListaDeclaracaoParametro;
00013 import plp.orientadaObjetos1.memoria.colecao.HashIdTipo;
00014 import plp.orientadaObjetos1.memoria.colecao.HashTipoTipo;
00015 import plp.orientadaObjetos1.memoria.colecao.ListaValor;
00016 import plp.orientadaObjetos1.memoria.colecao.StackHashIdDefClasse;
00017 import plp.orientadaObjetos1.memoria.colecao.StackHashIdListaDeclaracaoParametro;
00018 import plp.orientadaObjetos1.memoria.colecao.StackHashIdTipo;
00019 import plp.orientadaObjetos1.memoria.colecao.StackHashTipoTipo;
00020 import plp.orientadaObjetos1.util.Tipo;
00024 public class ContextoCompilacao implements AmbienteCompilacao {
00025
00030 private StackHashIdTipo pilha;
00031
00035 private StackHashTipoTipo pilhaTipoReal;
00036
00040 private StackHashIdListaDeclaracaoParametro pilhaProcedimento;
00041
00042
00046 private StackHashIdDefClasse pilhaDefClasse;
00047
00051 private StackHashIdTipo pilhaGeneric;
00052
00056 private ListaValor entrada;
00057
00061 public ContextoCompilacao(ListaValor entrada){
00062 pilha = new StackHashIdTipo();
00063 pilhaTipoReal = new StackHashTipoTipo();
00064
00065 pilhaGeneric = new StackHashIdTipo();
00066 pilhaProcedimento = new StackHashIdListaDeclaracaoParametro();
00067
00068 pilhaDefClasse = new StackHashIdDefClasse();
00069 pilhaDefClasse.push(new HashIdDefClasse());
00070 this.entrada = entrada;
00071 }
00072
00076 public void incrementa(){
00077 pilha.push(new HashIdTipo());
00078 pilhaProcedimento.push(new HashIdListaDeclaracaoParametro());
00079 pilhaGeneric.push(new HashIdTipo());
00080 pilhaTipoReal.push(new HashTipoTipo());
00081
00082 }
00083
00087 public void restaura(){
00088 pilha.pop();
00089 pilhaProcedimento.pop();
00090 pilhaGeneric.pop();
00091 pilhaTipoReal.pop();
00092
00093 }
00100 public void mapTipo(Id idArg, Tipo tipoId)
00101 throws VariavelJaDeclaradaException {
00102 HashIdTipo aux = (HashIdTipo) pilha.peek();
00103 if (aux.put(idArg, tipoId) != null) {
00104 throw new VariavelJaDeclaradaException(idArg);
00105 }
00106 }
00107
00114 public void mapGeneric(Id idArg, Tipo tipoId) throws ClasseJaDeclaradaException {
00115 HashIdTipo aux = (HashIdTipo) pilhaGeneric.peek();
00116 if (aux.put(idArg, tipoId) != null) {
00117 throw new ClasseJaDeclaradaException(idArg);
00118 }
00119 }
00120
00128 public void mapParametrosProcedimento(Id idArg, ListaDeclaracaoParametro parametrosId)
00129 throws ProcedimentoJaDeclaradoException {
00130 HashIdListaDeclaracaoParametro aux = (HashIdListaDeclaracaoParametro) pilhaProcedimento.peek();
00131 if (aux.put(idArg, parametrosId) != null) {
00132 throw new ProcedimentoJaDeclaradoException(idArg);
00133 }
00134 }
00135
00142 public void mapDefClasse(Id idArg, DefClasse defClasse)
00143 throws ClasseJaDeclaradaException {
00144 HashIdDefClasse aux = (HashIdDefClasse) pilhaDefClasse.peek();
00145 if (aux.put(idArg, defClasse) != null) {
00146 throw new ClasseJaDeclaradaException(idArg);
00147 }
00148 }
00149
00156 public void mapTipoReal(Tipo tipoGeneric, Tipo tipoReal)
00157 throws ClasseJaDeclaradaException {
00158 HashTipoTipo aux = pilhaTipoReal.peek();
00159 if (aux.put(tipoGeneric, tipoReal) != null) {
00160 throw new ClasseJaDeclaradaException(tipoGeneric.getTipo());
00161 }
00162 }
00163
00170 public Tipo getTipo(Id idArg)
00171 throws VariavelNaoDeclaradaException {
00172 Tipo result = null;
00173 StackHashIdTipo auxStack = new StackHashIdTipo();
00174 while (result == null && !pilha.empty()) {
00175 HashIdTipo aux = pilha.pop();
00176 auxStack.push(aux);
00177 result = aux.get(idArg);
00178 }
00179 while (!auxStack.empty()) {
00180 pilha.push(auxStack.pop());
00181 }
00182 if (result == null) {
00183 throw new VariavelNaoDeclaradaException(idArg);
00184 } else {
00185 return result;
00186 }
00187 }
00188
00198 public ListaDeclaracaoParametro getParametrosProcedimento(Id idArg)
00199 throws ProcedimentoNaoDeclaradoException {
00200 ListaDeclaracaoParametro result = null;
00201 StackHashIdListaDeclaracaoParametro auxStack = new StackHashIdListaDeclaracaoParametro();
00202 while (result == null && !pilhaProcedimento.empty()) {
00203 HashIdListaDeclaracaoParametro aux = pilhaProcedimento.pop();
00204 auxStack.push(aux);
00205 result = aux.get(idArg);
00206 }
00207 while (!auxStack.empty()) {
00208 pilhaProcedimento.push(auxStack.pop());
00209 }
00210 if (result == null) {
00211 throw new ProcedimentoNaoDeclaradoException(idArg);
00212 } else {
00213 return result;
00214 }
00215 }
00216
00224 public DefClasse getDefClasse(Id idArg)
00225 throws ClasseNaoDeclaradaException {
00226 DefClasse result = null;
00227 StackHashIdDefClasse auxStack = new StackHashIdDefClasse();
00228 while (result == null && !pilhaDefClasse.empty()) {
00229 HashIdDefClasse aux = pilhaDefClasse.pop();
00230 auxStack.push(aux);
00231 result = aux.get(idArg);
00232 }
00233 while (!auxStack.empty()) {
00234 pilhaDefClasse.push(auxStack.pop());
00235 }
00236 if (result == null) {
00237 throw new ClasseNaoDeclaradaException(idArg);
00238 } else {
00239 return result;
00240 }
00241 }
00242
00249 public Tipo getTipoReal( Tipo tipoGeneric)
00250 throws ClasseNaoDeclaradaException {
00251 Tipo result = null;
00252 StackHashTipoTipo auxStack = new StackHashTipoTipo();
00253 while (result == null && !pilhaTipoReal.empty()) {
00254 HashTipoTipo aux = pilhaTipoReal.pop();
00255 auxStack.push(aux);
00256 result = aux.get(tipoGeneric);
00257 }
00258 while (!auxStack.empty()) {
00259 pilhaTipoReal.push(auxStack.pop());
00260 }
00261 if (result == null) {
00262 throw new ClasseNaoDeclaradaException(tipoGeneric.getTipo());
00263 } else {
00264 return result;
00265 }
00266 }
00267
00268
00275 public Tipo getGeneric(Id idArg)
00276 throws ClasseNaoDeclaradaException {
00277 Tipo result = null;
00278 StackHashIdTipo auxStack = new StackHashIdTipo();
00279 while (result == null && !pilhaGeneric.empty()) {
00280 HashIdTipo aux = pilhaGeneric.pop();
00281 auxStack.push(aux);
00282 result = aux.get(idArg);
00283 }
00284 while (!auxStack.empty()) {
00285 pilhaGeneric.push(auxStack.pop());
00286 }
00287 if (result == null) {
00288 throw new ClasseNaoDeclaradaException(idArg);
00289 } else {
00290 return result;
00291 }
00292 }
00293
00300 public Tipo getTipoEntrada()
00301 throws VariavelNaoDeclaradaException {
00302 Tipo aux = entrada.head().getTipo(this);
00303 entrada = entrada.tail();
00304 return aux;
00305 }
00306
00311 public StackHashTipoTipo getPilhaTipoTipo(){
00312 return this.pilhaTipoReal;
00313 }
00314
00315
00316 }
00317