public object VisitExprAST([NotNull] ExprASTContext context) { Visit(context.term(0)); for (int i = 1; i < context.term().Length; i++) { Visit(context.term(i)); string instruction = "BINARY_"; if (context.addop(i - 1).GetText() == "+") { instruction += "ADD"; } else { instruction += "SUBSTRACT"; } AddLine(instruction); } return(null); }
public object VisitExprAST([NotNull] ExprASTContext context) { if (context.term().Length == 1) { string type = (string)Visit(context.term(0)); if (type != "int") { if (context.SUB() == null) { return(type); } else if (type != null) { InsertError(context.SUB().Symbol, "La expresión '" + context.term()[0].GetText() + "'no es un número y no se puede negar"); return(null); } } else { return(type); } } else if (context.term().Length > 1) { bool allInts = context.term().ToList().All(term => { string type = (string)Visit(term); if (type != "int") { InsertError(term.Start, $"El término '{term.GetText()}' no es un número entero. Solo se permite el uso de '+' o '-' con números enteros"); } return(type == "int"); }); return(allInts ? "int" : null); } return(null); }
public object VisitDesignatorAST([NotNull] DesignatorASTContext context) { if (ExistIdent(context.ident()[0].GetText(), false)) { Identifier identifier = identificationTable.Find(context.ident()[0].GetText(), false); if (context.ident().Length == 1 && context.SQUAREBL().Length == 0 && context.DOT().Length == 0) { Visit(context.ident()[0]); return(identifier); } else if (identifier is ArrayIdentifier || identifier is InstanceIdentifier) { Visit(context.ident()[0]); bool arrayFound = false; bool error = false; Identifier currentIdentifier = identifier; context.GetRuleContexts <ParserRuleContext>().Skip(1).ToList().ForEach(r => { if (error) { return; } if (arrayFound) { InsertError(r.Start, "No se puede acceder a arreglos o propiedades de un elemento de un arreglo porque solo son de tipos simples"); error = true; return; } if (r is ExprASTContext) { arrayFound = true; ExprASTContext expr = r as ExprASTContext; string type = Visit(expr) as string; if (type == "int") { if (!(currentIdentifier is ArrayIdentifier)) { InsertError(r.Start, $"El identificador {currentIdentifier.Id} no es un arreglo"); error = true; return; } else { currentIdentifier = (currentIdentifier as ArrayIdentifier).Identifiers[0]; } } else { InsertError(expr.Start, "Solo se permiten números enteros cuando se accede a una posición del arreglo"); error = true; return; } } else if (r is IdentASTContext) { IdentASTContext ident = r as IdentASTContext; if (currentIdentifier is InstanceIdentifier) { currentIdentifier = (currentIdentifier as InstanceIdentifier).Identifiers.Find(i => ident.GetText() == i.Id); if (currentIdentifier == null) { InsertError(ident.Start, $"El identificador {ident.IDENT().Symbol.Text} no existe en la instancia"); error = true; return; } } else { InsertError(ident.Start, $"El identificador {currentIdentifier.Id} no es una instancia de una clase"); error = true; return; } } }); if (!error) { return(currentIdentifier); } } else { InsertError(context.Start, "No se puede acceder a posiciones o propiedades de un identificador que no es una clase ni un arreglo"); } } else { InsertError(context.ident()[0].Start, $"El identificador {context.ident()[0].GetText()} no ha sido declarado"); } return(null); }