Beispiel #1
0
        public override ASTNode VisitDeclaration([NotNull] p4Parser.DeclarationContext context)
        {
            IdAstNode id;

            if (context.Assign() != null)
            {
                Antlr4.Runtime.IToken symbol = context.Assign().Symbol;
                ASTNode expr = Visit(context.expr());
                if (context.modifier() != null)
                {
                    id = new IdAstNode(context.Id().Symbol, SALType.Types[Visit(context.valuetype()).Token.Text], Visit(context.modifier()).Token);
                    return(new AssignAstNode(id, symbol, expr));
                }
                else
                {
                    id = new IdAstNode(context.Id().Symbol, SALType.Types[Visit(context.valuetype()).Token.Text]);
                    return(new AssignAstNode(id, symbol, expr));
                }
            }
            else
            {
                if (context.modifier() != null)
                {
                    id = new IdAstNode(context.Id().Symbol, SALType.Types[Visit(context.valuetype()).Token.Text], Visit(context.modifier()).Token);
                    return(new DeclareAstNode(id, null));
                }
                else
                {
                    id = new IdAstNode(context.Id().Symbol, SALType.Types[Visit(context.valuetype()).Token.Text]);
                    return(new DeclareAstNode(id, null));
                }
            }
        }
Beispiel #2
0
        public override ASTNode VisitFuncDcl([NotNull] p4Parser.FuncDclContext context)
        {
            ASTNode returntype = Visit(context.returnsStmt());
            ASTNode functionId = new IdAstNode(context.Id().Symbol, SALType.Types[returntype.Token.Text]);

            return(new FunctionDeclarationAstNode(functionId, Visit(context.@params()), Visit(context.block()), context.Function().Symbol));
        }
Beispiel #3
0
        public override ASTNode VisitMain([NotNull] p4Parser.MainContext context)
        {
            ASTNode returntype = new TypeAstNode(context.VOID().Symbol);
            ASTNode functionId = new IdAstNode(context.Main().Symbol, SALType.Types[returntype.Token.Text]);
            ASTNode @params    = new ParameterListAstNode(new ASTNode[] {}, null);

            return(new FunctionDeclarationAstNode(functionId, @params, Visit(context.block()), context.Function().Symbol));
        }
Beispiel #4
0
        public override ASTNode VisitForeachexpr([NotNull] p4Parser.ForeachexprContext context)
        {
            ASTNode itemId    = new IdAstNode(context.Id()[0].Symbol, SALTypeEnum.undefined);
            ASTNode collectId = new IdAstNode(context.Id()[1].Symbol, SALTypeEnum.undefined);


            return(new ForeachAstNode(itemId, collectId, Visit(context.block()), context.Foreach().Symbol));
        }
Beispiel #5
0
        public override ASTNode VisitAssignment([NotNull] p4Parser.AssignmentContext context)
        {
            IdAstNode id = new IdAstNode(context.Id().Symbol, SALTypeEnum.undefined);

            Antlr4.Runtime.IToken symbol = Visit(context.assnOp()).Token;
            ASTNode expr = Visit(context.expr());

            return(new AssignAstNode(id, symbol, expr));
        }
Beispiel #6
0
        public override ASTNode VisitFunctioncall([NotNull] p4Parser.FunctioncallContext context)
        {
            ASTNode functionId = new IdAstNode(context.Id().Symbol, SALTypeEnum.undefined);

            if (context.arguments() != null)
            {
                return(new FunctioncallAstNode(functionId, Visit(context?.arguments()), null));
            }
            else
            {
                return(new FunctioncallAstNode(functionId, new ArgumentsAstNode(new ASTNode[] {}, null), null));
            }
        }
Beispiel #7
0
        public override ASTNode VisitForexpr([NotNull] p4Parser.ForexprContext context)
        {
            ASTNode Id = new IdAstNode(context.Id().Symbol, SALTypeEnum.number);

            if (context.Step() != null)
            {
                return(new ForAstNode(Id, Visit(context.expr()[0]), Visit(context.expr()[1]), Visit(context.expr()[2]), Visit(context.block()), context.For().Symbol));
            }
            else
            {
                return(new ForAstNode(Id, Visit(context.expr()[0]), Visit(context.expr()[1]), null, Visit(context.block()), context.For().Symbol));
            }
        }
Beispiel #8
0
        public override ASTNode VisitPostExpr([NotNull] p4Parser.PostExprContext context)
        {
            if (context.primExpr() != null)
            {
                return(Visit(context.primExpr()));
            }
            else if (context.Lbrace() != null)
            {
                return(Visit(context.exprList()));
            }
            else if (context.Lbracket() != null)
            {
                ASTNode ArrayId = new IdAstNode(context.Id().Symbol, SALTypeEnum.undefined);

                return(new ArrayAccessAstNode(ArrayId, Visit(context.expr()), null));
            }
            throw new Exception("VisitPostExpr Exception");
        }
Beispiel #9
0
        //Due to the CFG some not so nice (almost spaghetti-like) code has been created
        //(Namely that formal parameters are ID AST nodes and not Declaration Ast Nodes)
        public override InoTypeEnum Visit(IdAstNode node)
        {
            if (IsParam && IsFirstWalk)
            {
                if (node.Symbol.Type == SALTypeEnum.number) //IF IT IS A SAL-NUMBER ADD IT TO THE EVALUATION QUEUE BY SETTING ISPARAM TO TRUE ELSE IGNORE IT
                {
                    node.IsParam = IsParam;
                    VariableTypes.Add(node.Symbol, InoTypeEnum.undefined);
                }
                return(InoTypeEnum.undefined);
            }
            else if (node.Symbol != null && node.Symbol.Type == SALTypeEnum.number)
            {
                VariableTypes.TryGetValue(node.Symbol, out InoTypeEnum val);   //If it is not a parameter-declaration try to find the ino type of the id.
                node.InoType = val;
                return(val);
            }

            return(InoTypeEnum.undefined);
        }
Beispiel #10
0
        public override string Visit(IdAstNode node)
        {
            string IdCode = "";

            if (node.Negation)
            {
                IdCode += "!";
            }

            if (node.Type != SALTypeEnum.undefined && node.Type != SALTypeEnum.number)
            {
                IdCode += node.Type + " ";
            }

            else if (node.Type == SALTypeEnum.number && node.InoType != InoTypeEnum.undefined)
            {
                DeclOrInit.Add(node.Symbol);
                IdCode += node.InoType + " ";
            }

            IdCode += node.Token.Text;

            if (node.ArraySize != null)
            {
                if (node.ArraySize == 0)
                {
                    IdCode += "[]";
                }
                else
                {
                    IdCode += $"[{node.ArraySize}]";
                }
            }

            return(IdCode);
        }