Beispiel #1
0
 public object VisitDesignatorAST([NotNull] DesignatorASTContext context)
 {
     if (context.ident(0).decl.Parent is ProgramASTContext)
     {
         return("GLOBAL");
     }
     else
     {
         return("FAST");
     }
 }
        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);
        }