示例#1
0
        public object VisitConstDeclAST([NotNull] ConstDeclASTContext context)
        {
            string varName = context.ident().GetText();

            string scope = "LOCAL";

            if (context.Parent is ProgramASTContext)
            {
                scope = "GLOBAL";
            }

            string type = Visit(context.type()) as string;


            string declCode = $"PUSH_{scope}_{type} {varName}";

            AddLine(declCode);

            if (scope.Equals("LOCAL"))
            {
                scope = "FAST";
            }

            string constT;

            if (context.NUM() != null)
            {
                constT = context.NUM().GetText();
            }
            else if (context.CHARCONST() != null)
            {
                constT = context.CHARCONST().GetText();
            }
            else
            {
                constT = context.STRING().GetText();
            }

            string loadCode = $"LOAD_CONST {constT}";

            AddLine(loadCode);

            string assignCode = $"STORE_{scope} {varName}";

            AddLine(assignCode);

            WhiteLine();
            return(null);
        }
示例#2
0
        public object VisitConstDeclAST([NotNull] ConstDeclASTContext context)
        {
            string type = (string)Visit(context.type());

            if (type != null)
            {
                if (type != "int" && type != "char")
                {
                    InsertError(context.Start,
                                "Los tipo para una constante solo pueden ser int o char");
                    return(null);
                }
                IdentASTContext ident = (IdentASTContext)Visit(context.ident());
                if (ident != null)
                {
                    if (!ExistIdent(ident.IDENT().Symbol.Text, true))
                    {
                        Identifier identifier = new ConstIdentifier(ident.IDENT().GetText(), ident.IDENT().Symbol, identificationTable.getLevel(), type, context);
                        identificationTable.Insert(identifier);



                        if ((context.NUM() != null &&
                             ((context.NUM().GetText().Split('.').Length > 1 && type != "float") ||
                              (context.NUM().GetText().Split('.').Length == 1 && type != "int")))
                            ||
                            (context.STRING() != null && type != "string") ||
                            (context.CHARCONST() != null && type != "char"))
                        {
                            InsertError(context.EQUAL().Symbol,
                                        "El tipo para la constante " + ident.GetText() + " no coincide con el tipo de " + context.GetText().Split('=').Last());
                        }
                    }
                    else
                    {
                        InsertError(ident.IDENT().Symbol, "El identificador " + ident.IDENT().Symbol.Text + " ya fue declarado en este scope");
                    }
                }
            }
            return(null);
        }