Esempio n. 1
0
        public void Visit(CodeBlockAstNode visitable)
        {
            _context.CreateScope();
            foreach (var statement in visitable.Statements)
            {
                Visit(statement);
            }

            _context.DestroyScope();
        }
Esempio n. 2
0
        public void TestCodeBlock(CodeBlockAstNode expect, CodeBlockAstNode actual,
                                  string traceMessage)
        {
            Assert.AreEqual(expect.Statements.Length, actual.Statements.Length);
            var len = expect.Statements.Length;

            for (var i = 0; i < len; i++)
            {
                TestGeneral(expect.Statements[i], actual.Statements[i],
                            $"{traceMessage} -> statements[{i}]");
            }
        }
 public void Visit(CodeBlockAstNode visitable)
 {
     throw new System.NotImplementedException();
 }
        public void TestParseAst1()
        {
            using var sourceStream = new FileStream("TestCode/TestParse1.rbl",
                                                    FileMode.Open);
            ILanguageLexer  lexer       = new RbLexer(sourceStream);
            ILanguageParser parser      = new RbParser(lexer);
            var             actualAst   = parser.Parse();
            var             expectedAst = new CodeBlockAstNode(new BasicAstNode[] {
                new FunctionAstNode(
                    new FunctionPrototypeAstNode(
                        new[] {
                    new FunctionParameter("str", "s")
                }, "WriteLine", "void", new[] { "@import" }
                        ), null
                    ),
                new VariableDefineAstNode("str", "greeting",
                                          new StringAstNode("\"Hello world!\\nI'm running on CLR!\"")),
                new FunctionAstNode(
                    new FunctionPrototypeAstNode(
                        new[] {
                    new FunctionParameter("i64", "n")
                }, "fib", "i64", null
                        ), new CodeBlockAstNode(
                        new BasicAstNode[] {
                    new IfElseAstNode(
                        new BinaryOperatorAstNode("==",
                                                  new IdentifierAstNode("n"),
                                                  new IntegerAstNode(1)),
                        new CodeBlockAstNode(new BasicAstNode[] {
                        new ReturnAstNode(new IntegerAstNode(1))
                    }), null
                        ),
                    new ReturnAstNode(new BinaryOperatorAstNode("*",
                                                                new IdentifierAstNode("n"),
                                                                new FunctionCallingAstNode("fib", new BasicAstNode[] {
                        new BinaryOperatorAstNode("-",
                                                  new IdentifierAstNode("n"), new IntegerAstNode(1))
                    })
                                                                ))
                }
                        )
                    ),
                new FunctionAstNode(
                    new FunctionPrototypeAstNode(
                        new[] {
                    new FunctionParameter("str", "s")
                }, "foo", "i64", null
                        ), new CodeBlockAstNode(new BasicAstNode[] {
                    new FunctionCallingAstNode("WriteLine",
                                               new BasicAstNode[] { new IdentifierAstNode("s") }),
                    new VariableDefineAstNode("i64", "isNull",
                                              new BinaryOperatorAstNode("+",
                                                                        new IntegerAstNode(10),
                                                                        new UnaryOperatorAstNode("not",
                                                                                                 new UnaryOperatorAstNode("address_of",
                                                                                                                          new IdentifierAstNode("s")
                                                                                                                          )
                                                                                                 )
                                                                        )
                                              ),
                    new ReturnAstNode(
                        new FunctionCallingAstNode("fib", new BasicAstNode[] {
                        new IntegerAstNode(10)
                    }))
                })
                    ),
                new FunctionCallingAstNode("foo", new BasicAstNode[] {
                    new IdentifierAstNode("greeting")
                })
            });
            var tester = new TestAstHelper(expectedAst, actualAst);

            tester.Test();
        }