Ejemplo n.º 1
0
        public object VisitAssignStatementAST([NotNull] AssignStatementASTContext context)
        {
            Visit(context.expr());

            string scope = Visit(context.designator()) as string;
            string ident = context.designator().GetText();

            AddLine($"STORE_{scope} {ident}");
            WhiteLine();
            return(null);
        }
Ejemplo n.º 2
0
        public object VisitAssignStatementAST([NotNull] AssignStatementASTContext context)
        {
            var identifier = Visit(context.designator()) as Identifier;

            if (identifier != null)
            {
                if (identifier is VarIdentifier || identifier is InstanceIdentifier)
                {
                    var type = Visit(context.expr()) as string;
                    if (identifier.Type.Equals(type))
                    {
                        return(new List <Pair <string, IToken> >());
                    }
                    else
                    {
                        var tmpExpr = context.expr();
                        InsertError(tmpExpr.Start,
                                    $"No se puede asignar un valor de tipo '{type}' a una variable de tipo '{identifier.Type}'.");
                    }
                }
                else if (identifier is ArrayIdentifier)
                {
                    var arr  = identifier as ArrayIdentifier;
                    var expr = context.expr() as ExprASTContext;

                    if ((expr.term()[0] as TermASTContext).factor()[0] is NewFactorASTContext)
                    {
                        var nf     = (expr.term()[0] as TermASTContext).factor()[0] as NewFactorASTContext;
                        var nfType = Visit(nf) as string;

                        if (arr.Type.Equals(nfType))
                        {
                            if (nf.SQUAREBL() != null && nf.SQUAREBR() != null)
                            {
                                var arrExpr = nf.expr() as ExprASTContext;
                                if (arrExpr != null && (Visit(arrExpr) as string) == "int")
                                {
                                    if (arrExpr.SUB() == null)
                                    {
                                        return(new List <Pair <string, IToken> >());
                                    }
                                    else
                                    {
                                        InsertError(arrExpr.SUB().Symbol, "El tamaño del arreglo no puede ser negativo.");
                                    }
                                }
                                else
                                {
                                    InsertError(arrExpr.Start, "Se esperaba un valor entero.");
                                }
                            }
                        }
                        else
                        {
                            InsertError(nf.Start, $"No se puede asignar un valor de tipo '{nfType}' a una variable de tipo '{identifier.Type}'.");
                        }
                    }
                    else
                    {
                        InsertError(expr.Start, "Sólo se puede asignar valores a arreglos usando el modificador 'new'.");
                    }
                }
                else if (identifier is ConstIdentifier)
                {
                    InsertError(context.EQUAL().Symbol,
                                "No es posible modificar el valor de una constante después de su declaración.");
                }
            }
            else
            {
                InsertError(context.designator().Start,
                            $"La variable '{context.designator()}' no ha sido declarada.");
            }

            return(new List <Pair <string, IToken> >());;
        }