static int dibujar_nodo(nodo_expresion raiz) { int nodo = get_cluster(); String parentesis = ""; if (raiz.rol.Equals("llamar_funcion")) { parentesis = "( )"; } String cadena = "nodo" + nodo + "[label=\"" + raiz.valor + parentesis + "\"];\n"; if (raiz.rol.Equals("llamar_funcion")) { foreach (arbol_expresion arbol in raiz.parametros) { cadena += "nodo" + nodo + "--" + "{ nodo" + dibujar_nodo(arbol.raiz) + "} ;\n"; } } if (raiz.izq != null) { cadena += "nodo" + nodo + "--" + "nodo" + dibujar_nodo(raiz.izq) + ";\n"; } if (raiz.der != null) { cadena += "nodo" + nodo + "--" + "nodo" + dibujar_nodo(raiz.der) + ";\n"; } nodos += cadena; return(nodo); }
private static nodo_expresion para(sentencia sentencia) { //declarar variable inicial ejecutar_sentencia(sentencia.inicial); //hacer ciclo String condicion = sentencia.caminos.ElementAt(0).condicion.ejecutar_arbol().valor; while (condicion.Equals("1")) { nodo_expresion resultado = ejecutar_sentencia_control(sentencia.caminos.ElementAt(0).funcion); if (resultado.rol.Equals("retorno")) { return(resultado); } else if (resultado.rol.Equals("continuar")) { ejecutar_sentencia(sentencia.caminos.ElementAt(0).funcion.sentencias.Last()); //continue; } else if (resultado.rol.Equals("detener")) { break; } condicion = sentencia.caminos.ElementAt(0).condicion.ejecutar_arbol().valor; } return(@const.VOID); }
public static void dibujar_expresion(nodo_expresion raiz, String nombre) { //cluster = 0; nodos = ""; String cod = "graph \"\" \n { \n label = \"" + nombre + "\"; \n subgraph cluster01{\n label = \"\" \n"; dibujar_nodo(raiz); cod += nodos; cod += " } \n } "; System.IO.StreamWriter file = new System.IO.StreamWriter(nombre + ".dot"); file.WriteLine(cod); file.Close(); String comando = "dot -Tpng " + nombre + ".dot > " + @const.RUTA.Replace("\\", "\\\\") + "\\EXP_" + nombre + ".png"; System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + comando); procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; procStartInfo.CreateNoWindow = false; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); string result = proc.StandardOutput.ReadToEnd(); Console.WriteLine(result); }
public static nodo_expresion ejecutar_sentencia_control(funcion funcion) { variables variables = new variables(); variables_actuales.Push(variables); funcion_actual.Push(funcion); foreach (sentencia s in funcion.sentencias) { nodo_expresion respuesta = ejecutar_sentencia(s); if (respuesta.rol.Equals("retorno")) { variables_actuales.Pop(); funcion_actual.Pop(); return(respuesta); } else if (respuesta.rol.Equals("detener")) { variables_actuales.Pop(); funcion_actual.Pop(); return(respuesta); } else if (respuesta.rol.Equals("continuar")) { variables_actuales.Pop(); funcion_actual.Pop(); return(respuesta); } } variables_actuales.Pop(); funcion_actual.Pop(); return(@const.VOID); }
private static nodo_expresion retorno(sentencia sentencia) { if (sentencia.expresion.raiz != null) { nodo_expresion respuesta = sentencia.expresion.ejecutar_arbol(); respuesta.rol = "retorno"; return(respuesta); } else { nodo_expresion respuesta = new nodo_expresion("", "retorno", "void", sentencia.fila, sentencia.columna); return(respuesta); } }
static nodo_expresion asignacion(sentencia sentencia) { nodo_expresion valor = sentencia.expresion.ejecutar_arbol(); String id = sentencia.ids.ElementAt(0); String tipo = valor.tipo; variable variable = null; variable = buscar_variable(id); if (variable != null) { String tipo_id = variable.tipo; String tipo_exp = valor.tipo; if (tipo.Equals("error")) { return(@const.VOID); } if (tipo_id.Equals("number")) { if (tipo_exp.Equals("string")) { errores.errores.add_error("Error Semantico", "casteo indefinido string a number", sentencia.fila, sentencia.columna); return(@const.VOID); } } else if (tipo_id.Equals("bool")) { if (!tipo_exp.Equals("bool")) { errores.errores.add_error("Error Semantico", "casteo indefinido " + tipo_exp + " a bool", sentencia.fila, sentencia.columna); return(@const.VOID); } } variable.valor = valor.valor; return(@const.VOID); } else { //REPORTAR ERROR errores.errores.add_error("Error Semantico", "Variable \"" + id + "\" no definida", sentencia.fila, sentencia.columna); return(@const.VOID); } }
static nodo_expresion declarar(sentencia sentencia) { //valor a ingresar String valor = ""; if (sentencia.expresion.raiz != null) { nodo_expresion nodo_valor = sentencia.expresion.ejecutar_arbol(); if (!nodo_valor.tipo.Equals("error")) { valor = nodo_valor.valor; } if (sentencia.tipo_dato.Equals("number")) { if (nodo_valor.tipo.Equals("string")) { errores.errores.add_error("Error Semantico", "casteo indefinido string a number", sentencia.fila, sentencia.columna); valor = ""; } } else if (sentencia.tipo_dato.Equals("bool")) { if (!nodo_valor.tipo.Equals("bool")) { errores.errores.add_error("Error Semantico", "casteo indefinido " + nodo_valor.tipo + " a bool", sentencia.fila, sentencia.columna); valor = ""; } } } //lista de ids; foreach (String id in sentencia.ids) { nodo_expresion respuesta = variables_actuales.Peek().add_variable(sentencia.tipo_dato, id, valor); if (!respuesta.valor.Equals("1")) { errores.errores.add_error(respuesta.rol, respuesta.valor, sentencia.fila, sentencia.columna); } } return(@const.OK); }
private static nodo_expresion selecciona(sentencia sentencia) { Boolean entro = false; foreach (camino camino in sentencia.caminos) { String condicion = ""; if (camino.condicion.raiz != null) { condicion = camino.condicion.ejecutar_arbol().valor; } else { condicion = "1"; } funcion flujo = camino.funcion; if (condicion.Equals("1") || entro == true) { entro = true; nodo_expresion respuesta = ejecutar_sentencia_control(flujo); if (respuesta.rol.Equals("detener")) { break; } else if (respuesta.rol.Equals("retorno")) { return(respuesta); } else if (respuesta.rol.Equals("continuar")) { return(respuesta); } } } return(@const.VOID); }
public static nodo_expresion mostrar(sentencia sentencia) { List <String> expresiones = new List <String>(); foreach (arbol_expresion expresion in sentencia.expresion.raiz.parametros) { nodo_expresion respuesta = expresion.ejecutar_arbol(); if (respuesta.valor.Equals("error")) { errores.errores.add_error("Error Semantico", "Parametros incorrectos en funcion Mostrar", sentencia.fila, sentencia.columna); return(respuesta); } else { expresiones.Add(respuesta.valor); } } String cadena = expresiones.ElementAt(0); cadena = cadena.Replace("\\n", "\r\n"); cadena = cadena.Replace("\\t", "\t"); expresiones.RemoveAt(0); String[] parametros = expresiones.ToArray(); try { String imprimir = String.Format(cadena, parametros); Program.form1.get_consola().AppendText("\r\n>" + imprimir); } catch (Exception e) { errores.errores.add_error("Error Semantico", "Parametros incorrectos en funcion Mostrar", sentencia.fila, sentencia.columna); return(new nodo_expresion("error", "error", "error", sentencia.fila, sentencia.columna)); } return(@const.VOID); }
private static nodo_expresion si(sentencia sentencia) { nodo_expresion si_cond = sentencia.caminos.ElementAt(0).condicion.ejecutar_arbol(); funcion si = sentencia.caminos.ElementAt(0).funcion; funcion sino = null; if (sentencia.caminos.Count > 1) { sino = sentencia.caminos.ElementAt(1).funcion; } if (si_cond.valor.Equals("1")) { return(ejecutar_sentencia_control(si)); } else if (sino != null) { return(ejecutar_sentencia_control(sino)); } else { return(@const.VOID); } }
static int dibujar_sentencia(sentencia s) { String tipo = s.tipo; int no_sentencia = get_cluster(); if (tipo.Equals("declarar") || tipo.Equals("asignacion")) { String ids = "|{"; String tipo_dato = ""; if (s.tipo_dato != null) { tipo_dato = "\\n" + s.tipo_dato; } foreach (String id in s.ids) { ids += id + "|"; } if (ids.Length > 0) { ids = ids.Substring(0, ids.Length - 1) + "}"; } String asigna = ""; if (s.expresion.raiz != null) { asigna = "|expresion"; } String cadena = "node" + no_sentencia + "[label=\"{" + tipo + tipo_dato + ids + asigna + "}\"]\n"; //sentencias += cadena; write(cadena); } else if (tipo.Equals("retorno")) { String expresion = ""; if (s.expresion.raiz != null) { expresion = "|expresion"; } String cadena = "node" + no_sentencia + "[label=\"{retorno " + expresion + "}\"];"; //sentencias += cadena; write(cadena); } else if (tipo.Equals("continuar")) { String cadena = "node" + no_sentencia + "[label=\"{continuar}\"];"; //sentencias += cadena; write(cadena); } else if (tipo.Equals("detener")) { String cadena = "node" + no_sentencia + "[label=\"{detener}\"];"; //sentencias += cadena; write(cadena); } else if (tipo.Equals("call_funcion")) { nodo_expresion llamada = s.expresion.raiz; String parametros = "|{"; foreach (arbol_expresion parametro in llamada.parametros) { parametros += "exp" + "|"; } if (parametros.Length > 0) { parametros = parametros.Substring(0, parametros.Length - 1) + "}"; } String nombre = llamada.valor + "( )"; String cadena = "node" + no_sentencia + "[label=\"{llamar funcion|" + nombre + parametros + "}\"]\n"; //sentencias += cadena; write(cadena); } else if (tipo.Equals("si")) { int sentencia_anterior = pila_nodos.Pop(); int punto = get_cluster(); write("node" + sentencia_anterior + "->node" + no_sentencia + "\n"); write("subgraph cluster_" + get_subgraph() + "{ label = \"\"\nrankdir=UD\nstyle=filled\ncolor = " + get_color_grafo() + "\nnode[color = white shape = record]\n"); write("node" + no_sentencia + "[label=\"si\" color=white shape=diamond];"); write("node" + punto + "[shape=point];"); //ci pila_nodos.Push(no_sentencia); funcion si = s.caminos.ElementAt(0).funcion; for (int i = 0; i < si.sentencias.Count; i++) { sentencia sent = si.sentencias.ElementAt(i); dibujar_sentencia(sent); if (i == 0) { write("[label=si];"); } } int pre = pila_nodos.Pop(); write("node" + pre + "->node" + punto + "\n"); pila_nodos.Push(punto); //no if (s.caminos.Count > 1) { pila_nodos.Push(no_sentencia); funcion no = s.caminos.ElementAt(1).funcion; for (int i = 0; i < no.sentencias.Count; i++) { sentencia sent = no.sentencias.ElementAt(i); dibujar_sentencia(sent); if (i == 0) { write("[label=sino];"); } } int presino = pila_nodos.Pop(); write("node" + presino + "->node" + punto + "\n"); } else { write("node" + no_sentencia + "->node" + punto + "[label=\"sino\"];\n"); } write("\n}"); return(-1); } else if (tipo.Equals("selecciona")) { int sentencia_anterior = pila_nodos.Pop(); int punto = get_cluster(); write("node" + sentencia_anterior + "->node" + no_sentencia + "\n"); write("subgraph cluster_" + get_subgraph() + "{ label = \"\"\nstyle=filled\ncolor = " + get_color_grafo() + "\nnode[color = white shape = record]\n"); write("node" + no_sentencia + "[label=\"selecciona\" color=white shape=diamond];"); write("node" + punto + "[shape=point];"); //insertar caminos foreach (camino c in s.caminos) { write("subgraph cluster_" + get_subgraph() + "{ label = \"\"\nrankdir=UD\nstyle=filled\ncolor = " + get_color_grafo() + "\nnode[color = white shape = record]\n"); pila_nodos.Push(no_sentencia); funcion flujo = c.funcion; for (int i = 0; i < flujo.sentencias.Count; i++) { sentencia sent = flujo.sentencias.ElementAt(i); dibujar_sentencia(sent); if (i == 0) { if (c.condicion.raiz != null) { write("[label=\"" + c.condicion.raiz.der.valor + "\"];"); } else { write("[label=\"Defecto\"];"); } } } int precamino = pila_nodos.Pop(); write("node" + precamino + "->node" + punto + "\n"); write("\n}\n"); } pila_nodos.Push(punto); write("\n}"); return(-1); } else if (tipo.Equals("para")) { int sentencia_anterior = pila_nodos.Pop(); int punto = get_cluster(); write("node" + sentencia_anterior + "->node" + no_sentencia + "\n"); write("subgraph cluster_" + get_subgraph() + "{ label = \"\"\nstyle=filled\ncolor = " + get_color_grafo() + "\nrankdir=UD\n\nnode[color = white shape = record]\n"); write("node" + no_sentencia + "[label=\"para\" color=white shape=diamond];"); write("node" + punto + "[shape=point];"); pila_nodos.Push(no_sentencia); dibujar_sentencia(s.inicial); no_sentencia = get_cluster(); sentencia_anterior = pila_nodos.Pop(); //int punto = get_cluster(); write("node" + sentencia_anterior + "->node" + no_sentencia + "\n"); write("node" + no_sentencia + "[label=\"cumple\" color=white shape=diamond];"); //write("node" + punto + "[shape=point];"); //insertar caminos foreach (camino c in s.caminos) { pila_nodos.Push(no_sentencia); funcion flujo = c.funcion; for (int i = 0; i < flujo.sentencias.Count; i++) { sentencia sent = flujo.sentencias.ElementAt(i); dibujar_sentencia(sent); if (i == 0) { write("[label=\"Si\"];"); } } int precamino = pila_nodos.Pop(); write("node" + precamino + "->node" + no_sentencia + "\n"); } pila_nodos.Push(punto); write("node" + no_sentencia + "->node" + punto + "[label=\"no\"]\n"); write("\n}"); return(-1); } else if (tipo.Equals("hasta")) { int sentencia_anterior = pila_nodos.Pop(); int punto = get_cluster(); write("node" + sentencia_anterior + "->node" + no_sentencia + "\n"); write("subgraph cluster_" + get_subgraph() + "{ label = \"\"\nstyle=filled\ncolor = " + get_color_grafo() + "\nnode[color = white shape = record]\n"); write("node" + no_sentencia + "[label=\"hasta\" color=white shape=diamond];"); write("node" + punto + "[shape=point];"); //insertar caminos foreach (camino c in s.caminos) { pila_nodos.Push(no_sentencia); funcion flujo = c.funcion; for (int i = 0; i < flujo.sentencias.Count; i++) { sentencia sent = flujo.sentencias.ElementAt(i); dibujar_sentencia(sent); if (i == 0) { write("[label=\"No cumple\"];"); } } int ultimo = pila_nodos.Pop(); write("node" + ultimo + "->node" + no_sentencia + "\n"); } write("node" + no_sentencia + "->node" + punto + "[label=\"cumple\"]\n"); pila_nodos.Push(punto); write("\n}"); return(-1); } else if (tipo.Equals("mientras")) { int sentencia_anterior = pila_nodos.Pop(); int punto = get_cluster(); write("node" + sentencia_anterior + "->node" + no_sentencia + "\n"); write("subgraph cluster_" + get_subgraph() + "{ label = \"\"\nstyle=filled\ncolor = " + get_color_grafo() + "\nnode[color = white shape = record]\n"); write("node" + no_sentencia + "[label=\"mientras\" color=white shape=diamond];"); write("node" + punto + "[shape=point];"); //insertar caminos foreach (camino c in s.caminos) { pila_nodos.Push(no_sentencia); funcion flujo = c.funcion; for (int i = 0; i < flujo.sentencias.Count; i++) { sentencia sent = flujo.sentencias.ElementAt(i); dibujar_sentencia(sent); if (i == 0) { write("[label=\"cumple\"];"); } } int ultimo = pila_nodos.Pop(); write("node" + ultimo + "->node" + no_sentencia + "\n"); } write("node" + no_sentencia + "->node" + punto + "[label=\"no cumple\"]\n"); pila_nodos.Push(punto); write("\n}"); return(-1); } //ESTO YA NO ES DE LOS IFS int ant = pila_nodos.Pop(); write("node" + ant + "->node" + no_sentencia + "\n"); pila_nodos.Push(no_sentencia); return(no_sentencia); }
public nodo_expresion extraer_arbol(ParseTreeNode raiz) { String termino = "", token = ""; if (raiz.Term != null) { termino = raiz.Term.ToString(); } if (raiz.Token != null) { token = raiz.Token.Text; } if (termino.Equals("NUMERO") || termino.Equals("CADENA") || termino.Equals("True") || termino.Equals("False") || termino.Equals("ID")) { String tipo = ""; if (termino.Equals("NUMERO")) { tipo = "number"; } else if (termino.Equals("CADENA")) { tipo = "string"; } else if (termino.Equals("True")) { token = "1"; tipo = "bool"; } else if (termino.Equals("False")) { token = "0"; tipo = "bool"; } else { tipo = "id"; } nodo_expresion nuevo = new nodo_expresion(token.Replace("\"", ""), "terminal", tipo, raiz.Token.Location.Line, raiz.Token.Location.Column); return(nuevo); } else if (termino.Equals("llamar_funcion")) { nodo_expresion funcion = new nodo_expresion(raiz.ChildNodes.ElementAt(0).Token.Text, "llamar_funcion", "llamar_funcion", raiz.ChildNodes.ElementAt(0).Token.Location.Line, raiz.ChildNodes.ElementAt(0).Token.Location.Column); List <arbol_expresion> expresiones = new List <arbol_expresion>(); ParseTreeNode lista = raiz.ChildNodes.ElementAt(1); for (int i = 0; i < lista.ChildNodes.Count; i++) { arbol_expresion arbol = new arbol_expresion(); arbol.raiz = extraer_arbol(lista.ChildNodes.ElementAt(i)); expresiones.Add(arbol); } funcion.parametros = expresiones; //METER PARAMETROS AL NODO return(funcion); } else if (termino.Equals("exprecion")) { if (raiz.ChildNodes.Count == 3) { nodo_expresion izq = extraer_arbol(raiz.ChildNodes.ElementAt(0)); nodo_expresion der = extraer_arbol(raiz.ChildNodes.ElementAt(2)); nodo_expresion op = extraer_arbol(raiz.ChildNodes.ElementAt(1)); op.izq = izq; op.der = der; return(op); } else if (raiz.ChildNodes.Count == 2) { nodo_expresion izq = extraer_arbol(raiz.ChildNodes.ElementAt(1)); nodo_expresion op = extraer_arbol(raiz.ChildNodes.ElementAt(0)); op.izq = izq; return(op); } else { nodo_expresion nodo = extraer_arbol(raiz.ChildNodes.ElementAt(0)); return(nodo); } } else if (termino.Equals("relacion")) { nodo_expresion izq = extraer_arbol(raiz.ChildNodes.ElementAt(0)); nodo_expresion der = extraer_arbol(raiz.ChildNodes.ElementAt(2)); nodo_expresion op = extraer_arbol(raiz.ChildNodes.ElementAt(1)); op.izq = izq; op.der = der; return(op); } else if (termino.Equals("condicion")) { if (raiz.ChildNodes.Count == 3) { nodo_expresion izq = extraer_arbol(raiz.ChildNodes.ElementAt(0)); nodo_expresion der = extraer_arbol(raiz.ChildNodes.ElementAt(2)); nodo_expresion op = extraer_arbol(raiz.ChildNodes.ElementAt(1)); op.izq = izq; op.der = der; return(op); } else if (raiz.ChildNodes.Count == 2) { nodo_expresion izq = extraer_arbol(raiz.ChildNodes.ElementAt(1)); nodo_expresion op = extraer_arbol(raiz.ChildNodes.ElementAt(0)); op.izq = izq; return(op); } else { nodo_expresion nodo = extraer_arbol(raiz.ChildNodes.ElementAt(0)); return(nodo); } } else if (!termino.Equals("exprecion")) { nodo_expresion nuevo; try { nuevo = new nodo_expresion(token, "operador", "operador", raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } catch (Exception e) { nuevo = new nodo_expresion(token, "operador", "operador", -1, -1); } return(nuevo); } return(null); //Console.WriteLine(termino + " , " + token); }
void recorrer_arbol(ParseTreeNode raiz) { String termino = "", token = ""; if (raiz.Term != null) { termino = raiz.Term.ToString(); } if (raiz.Token != null) { token = raiz.Token.ToString(); } if (termino.Equals("incerteza")) { String numero = raiz.ChildNodes.ElementAt(1).Token.Text; Double incerteza = Double.Parse(numero.Replace('.', ',')); this.interprete.set_incerteza(incerteza); } else if (termino.Equals("ruta")) { String ruta = raiz.ChildNodes.ElementAt(1).Token.Text; this.interprete.set_ruta(ruta); } else if (termino.Equals("incluye")) { String texto = ""; String incluye = raiz.ChildNodes.ElementAt(1).Token.Text; try { String ruta = contenedor.SelectedTab.Name; String[] partes = ruta.Split('\\'); ruta = ""; for (int i = 0; i < partes.Length - 1; i++) { ruta += partes[i] + "\\"; } System.IO.StreamReader file = new System.IO.StreamReader(ruta + incluye); texto = file.ReadToEnd(); file.Close(); analizar(texto); } catch (Exception e) { errores.errores.add_error("Error Semantico", "Archivo " + incluye + " inaccesible", raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); return; } /* * String incluye = raiz.ChildNodes.ElementAt(1).Token.Text; * Console.WriteLine("deberÃa de incluir " + incluye); * analizar("Define 666 Define \"nuevo\" Principal () { alo= polisia; }"); */ } else if (termino.Equals("declaracion")) { String tipo = raiz.ChildNodes.ElementAt(0).Token.Text; List <String> ids = new List <string>(); for (int i = 0; i < raiz.ChildNodes.ElementAt(1).ChildNodes.Count; i++) { ids.Add(raiz.ChildNodes.ElementAt(1).ChildNodes.ElementAt(i).Token.Text); } arbol_expresion arbol = new arbol_expresion(); if (raiz.ChildNodes.ElementAt(2).ChildNodes.Count > 0) { arbol.raiz = extraer_arbol(raiz.ChildNodes.ElementAt(2).ChildNodes.ElementAt(1)); //@const.dibujar_expresion(arbol.raiz, "prueba_expresion"); /*nodo_expresion resultado= arbol.ejecutar_arbol(); * Console.WriteLine("----------------------"); * Console.WriteLine(resultado.valor + " " + resultado.tipo + " " + resultado.rol); * Console.WriteLine("----------------------"); */ } interprete.add_sentencia("declarar", ids, arbol, tipo.ToLower(), raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } else if (termino.Equals("declarar_funcion")) { if (raiz.ChildNodes.ElementAt(0).Token.Text.Equals("Principal")) { interprete.funcion_nueva("Principal", interprete.get_funcion(), "void"); raiz.ChildNodes.ForEach(recorrer_arbol); interprete.set_principal(); return; } else { interprete.funcion_nueva(raiz.ChildNodes.ElementAt(1).Token.Text, interprete.get_funcion(), raiz.ChildNodes.ElementAt(0).Token.Text); raiz.ChildNodes.ForEach(recorrer_arbol); interprete.agregar_nombre(pars); pars = ""; interprete.pop_funcion(); return; } } else if (termino.Equals("parametro")) { String tipo = raiz.ChildNodes.ElementAt(0).Token.Text; String nombre = raiz.ChildNodes.ElementAt(1).Token.Text; interprete.add_parametro(tipo, nombre); pars += "#" + tipo.ToLower(); } else if (termino.Equals("asignacion")) { String id = raiz.ChildNodes.ElementAt(0).Token.Text; List <String> lista = new List <string>(); lista.Add(id); nodo_expresion expresion = extraer_arbol(raiz.ChildNodes.ElementAt(2)); arbol_expresion arbol = new arbol_expresion(); arbol.raiz = expresion; interprete.add_sentencia("asignacion", lista, arbol, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } else if (termino.Equals("retorno_noterminal")) { arbol_expresion arbol = new arbol_expresion(); if (raiz.ChildNodes.Count > 1) { arbol.raiz = extraer_arbol(raiz.ChildNodes.ElementAt(1)); } interprete.add_sentencia("retorno", null, arbol, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } else if (termino.Equals("sentencia_continuar")) { interprete.add_sentencia("continuar", null, new arbol_expresion(), raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } else if (termino.Equals("sentencia_detener")) { interprete.add_sentencia("detener", null, new arbol_expresion(), raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } else if (termino.Equals("call_funcion")) { arbol_expresion arbol = new arbol_expresion(); arbol.raiz = extraer_arbol(raiz.ChildNodes.ElementAt(0)); interprete.add_sentencia("call_funcion", null, arbol, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } else if (termino.Equals("flujo_si")) { List <camino> caminos = new List <camino>(); //extrae la condicion nodo_expresion cond = extraer_arbol(raiz.ChildNodes.ElementAt(0)); //inserta funcion para anidar interprete.funcion_nueva("si", interprete.get_funcion(), "void"); //inserta sentencias recorrer_arbol(raiz.ChildNodes.ElementAt(1)); //obtiene el camino si funcion si = interprete.extraer_funcion(); //agrega primer camino caminos.Add(new camino(cond, si)); //obtener el else if (raiz.ChildNodes.ElementAt(2).ChildNodes.Count > 0) { //si hay else interprete.funcion_nueva("Sino", interprete.get_funcion(), "void"); recorrer_arbol(raiz.ChildNodes.ElementAt(2).ChildNodes.ElementAt(0)); funcion sino = interprete.extraer_funcion(); caminos.Add(new camino(null, sino)); } interprete.add_sentencia("si", new arbol_expresion(), caminos, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); return; } else if (termino.Equals("flujo_selecciona")) { List <camino> caminos = new List <camino>(); //extrae la expresion a comparar nodo_expresion expresion = extraer_arbol(raiz.ChildNodes.ElementAt(0)); ParseTreeNode pares = raiz.ChildNodes.ElementAt(1); int caminos_cant = pares.ChildNodes.Count; for (int i = 0; i < caminos_cant; i++) { ParseTreeNode par = pares.ChildNodes.ElementAt(i); //hacer condicion nodo_expresion condicion = new nodo_expresion("==", "operador", "operador", raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); condicion.izq = expresion; //extraer valor nodo_expresion valor = extraer_arbol(par.ChildNodes.ElementAt(0)); condicion.der = valor; //extraer el camino interprete.funcion_nueva("camino", interprete.get_funcion(), "void"); recorrer_arbol(par.ChildNodes.ElementAt(1)); funcion flujo = interprete.extraer_funcion(); //insertar el camino caminos.Add(new camino(condicion, flujo)); } //obtener el Defecto if (raiz.ChildNodes.ElementAt(2).ChildNodes.Count > 0) { //si hay defecto interprete.funcion_nueva("defecto", interprete.get_funcion(), "void"); recorrer_arbol(raiz.ChildNodes.ElementAt(2).ChildNodes.ElementAt(0)); funcion defecto = interprete.extraer_funcion(); caminos.Add(new camino(null, defecto)); } interprete.add_sentencia("selecciona", new arbol_expresion(), caminos, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); return; } else if (termino.Equals("flujo_para")) { //crear sentencia de asignacion inicial List <String> id = new List <string>(); id.Add(raiz.ChildNodes.ElementAt(1).Token.Text); arbol_expresion expresion = new arbol_expresion(); expresion.raiz = extraer_arbol(raiz.ChildNodes.ElementAt(3)); sentencia asignacion_inicial = new sentencia("declarar", id, expresion, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); asignacion_inicial.tipo_dato = "number"; //extraer condicion nodo_expresion condicion = extraer_arbol(raiz.ChildNodes.ElementAt(4)); //sacar flujo List <camino> caminos = new List <camino>(); //inserta funcion para anidar interprete.funcion_nueva("para", interprete.get_funcion(), "void"); //inserta sentencias recorrer_arbol(raiz.ChildNodes.ElementAt(6)); //obtiene el camino funcion para = interprete.extraer_funcion(); //meter paso al flujo nodo_expresion op = new nodo_expresion("", "", "", -1, -1); nodo_expresion var = new nodo_expresion(id.ElementAt(0), "terminal", "id", raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); nodo_expresion uno = new nodo_expresion("1", "terminal", "NUMERO", raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); if (raiz.ChildNodes.ElementAt(5).Token.Text.Equals("++")) { op = new nodo_expresion("+", "operador", "operador", raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } else if (raiz.ChildNodes.ElementAt(5).Token.Text.Equals("--")) { op = new nodo_expresion("-", "operador", "operador", raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); } op.izq = var; op.der = uno; arbol_expresion asignacion = new arbol_expresion(); asignacion.raiz = op; sentencia paso = new sentencia("asignacion", id, asignacion, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); para.add_sentencia(paso); //agrega primer camino caminos.Add(new camino(condicion, para)); //meter sentencia interprete.add_sentencia("para", asignacion_inicial, caminos, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); return; } else if (termino.Equals("flujo_hasta")) { //extraer condicion nodo_expresion condicion = extraer_arbol(raiz.ChildNodes.ElementAt(0)); //sacar flujo List <camino> caminos = new List <camino>(); //inserta funcion para anidar interprete.funcion_nueva("hasta", interprete.get_funcion(), "void"); //inserta sentencias recorrer_arbol(raiz.ChildNodes.ElementAt(1)); //obtiene el camino funcion hasta = interprete.extraer_funcion(); //agrega primer camino caminos.Add(new camino(condicion, hasta)); //meter sentencia interprete.add_sentencia("hasta", new arbol_expresion(), caminos, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); return; } else if (termino.Equals("flujo_mientras")) { //extraer condicion nodo_expresion condicion = extraer_arbol(raiz.ChildNodes.ElementAt(0)); //sacar flujo List <camino> caminos = new List <camino>(); //inserta funcion para anidar interprete.funcion_nueva("mientras", interprete.get_funcion(), "void"); //inserta sentencias recorrer_arbol(raiz.ChildNodes.ElementAt(1)); //obtiene el camino funcion mientras = interprete.extraer_funcion(); //agrega primer camino caminos.Add(new camino(condicion, mientras)); //meter sentencia interprete.add_sentencia("mientras", new arbol_expresion(), caminos, raiz.FindToken().Location.Line, raiz.FindToken().Location.Column); return; } //Console.WriteLine("-"+termino+"-"+ token+"-"); raiz.ChildNodes.ForEach(recorrer_arbol); }
private static nodo_expresion detener(sentencia sentencia) { nodo_expresion respuesta = new nodo_expresion("", "detener", "void", sentencia.fila, sentencia.columna); return(respuesta); }
public static nodo_expresion ejecutar_flujo(funcion funcion, List <arbol_expresion> parametros) { variables variables = new variables(); variables_actuales.Push(variables); funcion_actual.Push(funcion); //DECLARAR PARAMETROS AUN NO HECHA int indice = 0; foreach (String parametro in funcion.parametros) { String tipo = parametro.Split(',')[0].ToLower(); String id = parametro.Split(',')[1]; //nodo_expresion valor = parametros.ElementAt(indice).ejecutar_arbol(); nodo_expresion valor = parametros.ElementAt(indice).ejecutar_arbol(); if (valor.tipo.Equals("error")) { return(new nodo_expresion("Error Semantico", "Parametro incorrecto", "error", -1, -1)); } else { variables_actuales.Peek().add_variable(tipo, id, valor.valor); } indice++; } foreach (sentencia s in funcion.sentencias) { nodo_expresion respuesta = ejecutar_sentencia(s); if (respuesta.rol.Equals("retorno")) { if (respuesta.tipo.Equals(funcion.tipo.ToLower())) { variables_actuales.Pop(); funcion_actual.Pop(); return(respuesta); } else { errores.errores.add_error("Error Semantico", "Error en retorno de tipo " + respuesta.tipo + " en funcion \"" + funcion.nombre.Split('#')[0] + "\" de tipo " + funcion.tipo, s.fila, s.columna); variables_actuales.Pop(); funcion_actual.Pop(); return(new nodo_expresion("error", "error", "error", s.fila, s.columna)); } } else if (respuesta.rol.Equals("continuar") || respuesta.rol.Equals("detener")) { errores.errores.add_error("Error Semantico", "Sentencia " + respuesta.rol + " fuera de ciclo, en funcion \"" + funcion.nombre.Split('#')[0] + "\" de tipo " + funcion.tipo, s.fila, s.columna); variables_actuales.Pop(); funcion_actual.Pop(); return(new nodo_expresion("error", "error", "error", s.fila, s.columna)); } } //reporte de variables //foreach (variable variable in variables_actuales.Peek().lista) { // Console.WriteLine(variable.tipo + " " + variable.nombre + " " + variable.valor); //} if ([email protected]) { variables_actuales.Pop(); } else { funcion.variables = variables_actuales.Pop(); } funcion_actual.Pop(); if ("void".Equals(funcion.tipo.ToLower())) { return(@const.VOID); } else { errores.errores.add_error("Error Semantico", "Error en retorno de tipo " + "void" + " en funcion \"" + funcion.nombre.Split('#')[0] + "\" de tipo " + funcion.tipo, funcion.sentencias.Last().fila, funcion.sentencias.Last().columna); return(new nodo_expresion("error", "error", "error", funcion.sentencias.Last().fila, funcion.sentencias.Last().columna)); } }
public camino(nodo_expresion raiz, funcion funcion) { this.condicion = new arbol_expresion(); this.condicion.raiz = raiz; this.funcion = funcion; }