Beispiel #1
0
        public static List <CFG> Analyze(Node n)
        {
            var symbols = new SymbolTable();
            var scope   = new Scope
            {
                ScopeType   = ScopeType.Program,
                SymbolTable = symbols
            };

            symbols.AddSymbol(new BuiltinFunctionVariable
            {
                Name  = "$main$",
                Scope = scope
            });
            symbols.AddSymbol(new BuiltinFunctionVariable
            {
                Name  = "writeln",
                Scope = scope
            });
            symbols.AddSymbol(new BuiltinFunctionVariable
            {
                Name  = "read",
                Scope = scope
            });
            symbols.AddSymbol(new BuiltinVariable
            {
                Name          = "true",
                PrimitiveType = PrimitiveType.Boolean,
                SubType       = PrimitiveType.Void,
                Scope         = scope
            });
            symbols.AddSymbol(new BuiltinVariable
            {
                Name          = "false",
                PrimitiveType = PrimitiveType.Boolean,
                SubType       = PrimitiveType.Void,
                Scope         = scope
            });


            var visitors = new Visitor[]
            {
                new ScopeVisitor(scope),
                new BuiltinVisitor(scope),
                new TypeVisitor(scope),
                new ExpressionVisitor(scope),
            };

            foreach (var v in visitors)
            {
                n.Accept(v);
            }

            var cfgVisitor = new CfgVisitor(scope, new List <CFG>());
            var cfg        = n.Accept(cfgVisitor);

            Console.WriteLine(string.Join("\n", cfg));
            return(cfg);
        }
Beispiel #2
0
        public override dynamic Visit(ProgramNode node)
        {
            var mainVisitor = new CfgVisitor(CurrentScope, _result);

            mainVisitor.Visit((FunctionDeclarationNode)node.MainBlock);
            // _result.AddRange(mainVisitor._result);
            // node.MainBlock.Accept(this);

            node.DeclarationBlock.Accept(this);

            return(_result);
        }
Beispiel #3
0
        public override dynamic Visit(FunctionDeclarationNode node)
        {
            var functionVisitor = new CfgVisitor(CurrentScope, _result);

            functionVisitor.Visit((StatementListNode)node.Statement);
            var cfg = new CFG
            {
                Blocks   = functionVisitor._blocks,
                Function = node.Function
            };

            _result.Add(cfg);
            //node.Statement.Accept(this);

            return(null);
        }