public Resultado ejecutar(Contexto ctx, int nivel) { var _Si = instruccion.ChildNodes[0]; var _Sino = instruccion.ChildNodes[1]; Resultado cond = new Expresion(_Si.ChildNodes[0].ChildNodes[0]).resolver(ctx); if (cond.Tipo != Constantes.T_BOOL) { ListaErrores.getInstance().setErrorSemantico(instruccion.Token.Location.Line, instruccion.Token.Location.Line, "Error de tipo", Interprete.archivo); return(FabricarResultado.creaFail()); } Resultado res = FabricarResultado.creaOk(); bool cond_b = cond.Boleano; if (cond_b) { Cuerpo cp = new Cuerpo(_Si.ChildNodes[1], permiteInterrupciones); res = cp.ejecutar(ctx, nivel + 1); } else { if (_Sino.ChildNodes.Count > 0) { Cuerpo cp = new Cuerpo(_Sino.ChildNodes[0], permiteInterrupciones); res = cp.ejecutar(ctx, nivel + 1); } } ctx.limpiarContexto(nivel); return(res); }
public override Resultado ejecutar(Contexto ctx, int nivel) { ParseTreeNode fun = Interprete.getFuncion(instruccion, ctx); if (fun == null) { ListaErrores.getInstance().setErrorSemantico(instruccion.Token.Location.Line, instruccion.Token.Location.Line, "El metodo no ha sido identificado", Interprete.archivo); return(FabricarResultado.creaFail()); } Contexto contexto = new Contexto(); Resultado ej; Cuerpo cuerpo; int cantParam = instruccion.ChildNodes[1].ChildNodes.Count; if (cantParam > 0) { ParseTreeNode Params = fun.ChildNodes[2]; for (int i = 0; i < cantParam; i++) { String tipoVar = Params.ChildNodes[i].ChildNodes[0].ToString(); String nombreVar = Params.ChildNodes[i].ChildNodes[1].Token.Text; Resultado valor = new Expresion(instruccion.ChildNodes[1].ChildNodes[i].ChildNodes[0]).resolver(ctx); Variable var = new Variable(nombreVar, valor.Valor, tipoVar, nivel); contexto.setVariable(var); } cuerpo = new Cuerpo(fun.ChildNodes[3], permiteInterrupciones); } else { cuerpo = new Cuerpo(fun.ChildNodes[2], permiteInterrupciones); } ej = cuerpo.ejecutar(contexto, nivel); if (ej.Tipo != fun.ChildNodes[0].ToString()) { ListaErrores.getInstance().setErrorSemantico(instruccion.Token.Location.Line, instruccion.Token.Location.Line, "No retorna el mismo tipo de la funcion", Interprete.archivo); return(FabricarResultado.creaFail()); } return(ej); }
public override Resultado ejecutar(Contexto ctx, int nivel) { Resultado exec = null; while (true) { Resultado condicion = new Expresion(instruccion.ChildNodes[0].ChildNodes[0]).resolver(ctx); if (condicion.Tipo != Constantes.T_BOOL) { ListaErrores.getInstance().setErrorSemantico(instruccion.Token.Location.Line, instruccion.Token.Location.Line, "La condicion no retorna un valor booleando", Interprete.archivo); return(FabricarResultado.creaFail()); } bool cond = condicion.Boleano; if (!cond) { exec = FabricarResultado.creaOk(); break; } Cuerpo cuerpo = new Cuerpo(instruccion.ChildNodes[1], true); exec = cuerpo.ejecutar(ctx, nivel); if (exec.esContinuar()) { continue; } if (exec.esDetener()) { exec = FabricarResultado.creaOk(); break; } if (exec.esRetorno()) { break; } ctx.limpiarContexto(nivel); } ctx.limpiarContexto(nivel); return(exec); }
public override Resultado ejecutar(Contexto ctx, int nivel) { //Obiene nombre de la variable, el valor, si es incremento o decremento y el cuerpo de la funcion String nombreVar = instruccion.ChildNodes[0].Token.Text; String tipoVar = Constantes.T_NUM; Resultado ValorVar = new Expresion(instruccion.ChildNodes[1].ChildNodes[0]).resolver(ctx); ParseTreeNode NodoCuerpo = instruccion.ChildNodes[4]; String inc_dec = instruccion.ChildNodes[3].ToString(); //si el valor de la variable no es el mismo a number se sale String castValue = ""; switch (tipoVar) { case Constantes.T_BOOL: castValue = "" + Convert.ToInt32(Convert.ToBoolean(ValorVar.Valor)); break; case Constantes.T_NUM: castValue = ValorVar.Valor; break; default: return(FabricarResultado.creaFail()); } // Creo variablew Variable var = new Variable(nombreVar, castValue, tipoVar, nivel); //si existe, es error entonces if (ctx.existeVariable(nombreVar)) { return(FabricarResultado.creaFail()); } ctx.setVariable(var); Resultado exec = null; while (true) { Resultado condicionPara = new Expresion(instruccion.ChildNodes[2].ChildNodes[0]).resolver(ctx); if (condicionPara.Tipo != Constantes.T_BOOL) { ListaErrores.getInstance().setErrorSemantico(instruccion.Token.Location.Line, instruccion.Token.Location.Line, "La condicion no retorna un valor booleando", Interprete.archivo); return(FabricarResultado.creaFail()); } bool cond = condicionPara.Boleano; if (!cond) { exec = FabricarResultado.creaOk(); break; } Cuerpo cuerpo = new Cuerpo(NodoCuerpo, true); exec = cuerpo.ejecutar(ctx, nivel + 1); if (exec.esContinuar()) { continue; } if (exec.esDetener()) { exec = FabricarResultado.creaOk(); break; } if (exec.esRetorno()) { break; } Variable asigTemp = ctx.getVariable(nombreVar); if (asigTemp != null) { if (asigTemp.Tipo == Constantes.T_NUM) { double valor = Convert.ToDouble(asigTemp.Valor); if (inc_dec == Constantes.INCREMENTO) { valor++; } else { valor--; } asigTemp.Valor = valor.ToString(); } ctx.ActualizarValor(nombreVar, asigTemp); } ctx.limpiarContexto(nivel + 1); } ctx.limpiarContexto(nivel + 1); ctx.limpiarContexto(nivel); return(exec); }
public override Resultado ejecutar(Contexto ctx, int nivel) { bool cumplio = false; Resultado res = FabricarResultado.creaOk(); Resultado cond = new Expresion(instruccion.ChildNodes[0].ChildNodes[0]).resolver(ctx); ParseTreeNode CuerpoSelecciona = instruccion.ChildNodes[1].ChildNodes[0]; String val = cond.Valor; String tipo = cond.Tipo; if (tipo == Constantes.T_BOOL || tipo == Constantes.T_ERROR) { ListaErrores.getInstance().setErrorSemantico(instruccion.Token.Location.Line, instruccion.Token.Location.Line, "Error de tipo", Interprete.archivo); return(FabricarResultado.creaFail()); } foreach (var ValorN in CuerpoSelecciona.ChildNodes) { String valN = ValorN.ChildNodes[0].Token.Text; String tipoN = ValorN.ChildNodes[0].Term.ToString(); if (tipoN == Constantes.T_STR) { valN = valN.Substring(1, valN.Length - 2); } if (tipo != tipoN) { ListaErrores.getInstance().setErrorSemantico(instruccion.Token.Location.Line, instruccion.Token.Location.Line, "los tipos no coinciden", Interprete.archivo); res = FabricarResultado.creaFail(); break; } if (cumplio == true || val.CompareTo(valN) != 0) { continue; } cumplio = true; Cuerpo cuerpo = new Cuerpo(ValorN.ChildNodes[1], true); res = cuerpo.ejecutar(ctx, nivel); if (res.esRetorno()) { break; } if (res.esDetener()) { res = FabricarResultado.creaOk(); break; } ctx.limpiarContexto(nivel); } if (instruccion.ChildNodes[1].ChildNodes.Count > 1) { if (cumplio == false) { var defecto = instruccion.ChildNodes[1].ChildNodes[1]; Cuerpo cuerpo = new Cuerpo(defecto.ChildNodes[0], true); res = cuerpo.ejecutar(ctx, nivel); if (res.esDetener()) { res = FabricarResultado.creaOk(); } } } return(res); }