00001 package plp.expressions2.expression;
00002
00003 import java.util.HashMap;
00004 import java.util.LinkedList;
00005 import java.util.List;
00006 import java.util.Map;
00007
00008 import plp.expressions1.util.Tipo;
00009 import plp.expressions2.declaration.DecVariavel;
00010 import plp.expressions2.memory.AmbienteCompilacao;
00011 import plp.expressions2.memory.AmbienteExecucao;
00012 import plp.expressions2.memory.VariavelJaDeclaradaException;
00013 import plp.expressions2.memory.VariavelNaoDeclaradaException;
00014
00015 public class ExpDeclaracao implements Expressao {
00016
00017 List<DecVariavel> seqdecVariavel;
00018 Expressao expressao;
00019
00020 public ExpDeclaracao(List<DecVariavel> declarations, Expressao expressaoArg) {
00021 seqdecVariavel = declarations;
00022 expressao = expressaoArg;
00023 }
00024
00025 public Valor avaliar(AmbienteExecucao ambiente)
00026 throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException {
00027
00028 ambiente.incrementa();
00029
00030 resolveValueBindings(ambiente);
00031
00032 Valor result = expressao.avaliar(ambiente);
00033 ambiente.restaura();
00034
00035 return result;
00036 }
00037
00038 private void includeValueBindings(AmbienteExecucao ambiente,
00039 Map<Id, Valor> resolvedValues) throws VariavelJaDeclaradaException {
00040 for (Id id : resolvedValues.keySet()) {
00041 Valor valor = resolvedValues.get(id);
00042 ambiente.map(id, valor);
00043 }
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 private void resolveValueBindings(AmbienteExecucao ambiente) throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException {
00059
00060 for (DecVariavel declaration : this.seqdecVariavel) {
00061 ambiente.map(declaration.getId(), declaration.getExpressao().avaliar(ambiente));
00062 }
00063 }
00064
00065
00066
00080 public boolean checaTipo(AmbienteCompilacao ambiente)
00081 throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException {
00082 ambiente.incrementa();
00083 Map<Id, Tipo> resolvedTypes;
00084 boolean result = false;
00085 try {
00086 if (this.checkTypeBindings(ambiente)) {
00087
00088 this.resolveTypeBindings(ambiente);
00089
00090 result = expressao.checaTipo(ambiente);
00091 } else {
00092 result = false;
00093 }
00094 } finally {
00095 ambiente.restaura();
00096 }
00097 return result;
00098 }
00099
00100 private void includeTypeBindings(AmbienteCompilacao ambiente,
00101 Map<Id, Tipo> resolvedTypes) throws VariavelJaDeclaradaException {
00102
00103 for (Id id : resolvedTypes.keySet()) {
00104 Tipo type = resolvedTypes.get(id);
00105 ambiente.map(id, type);
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 private void resolveTypeBindings(AmbienteCompilacao ambiente) throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException {
00124 for (DecVariavel declaration : this.seqdecVariavel) {
00125 ambiente.map(declaration.getId(), declaration.getExpressao().getTipo(ambiente));
00126 }
00127 }
00128
00129 private boolean checkTypeBindings(AmbienteCompilacao ambiente)
00130 throws VariavelJaDeclaradaException, VariavelNaoDeclaradaException {
00131
00132 boolean result = true;
00133
00134 for (DecVariavel declaration : this.seqdecVariavel) {
00135 if (!declaration.getExpressao().checaTipo(ambiente)) {
00136 result = false;
00137 break;
00138 }
00139 }
00140 return result;
00141 }
00142
00155 public Tipo getTipo(AmbienteCompilacao ambiente)
00156 throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException {
00157 ambiente.incrementa();
00158
00159
00160 this.resolveTypeBindings(ambiente);
00161
00162 Tipo result = expressao.getTipo(ambiente);
00163
00164 ambiente.restaura();
00165 return result;
00166 }
00167
00168 }