コード例 #1
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void ParserShould_GenerateAstFor_FunctionCallWithTwoActuals_R41()
        {
            // arrange
            var input = @"
main(x : integer, y : integer) : integer 
    secondary(x, y)
secondary(x : integer, y : integer) : integer
    1";

            // act
            var parser = new Parser()
            {
                EnableStackTrace = true
            };
            var program = (Program)parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(program.Definitions[0].Body.Expr, Is.AstEqual(new FunctionCall
                                                                      (
                                                                          identifier: new Identifier(0, "secondary"),
                                                                          actuals: new List <Actual>
            {
                new Actual(new Identifier(0, "x")),
                new Actual(new Identifier(0, "y"))
            }
                                                                      )));
        }
コード例 #2
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void ParserShould_GenerateMethod_WithTwoPrintExpression()
        {
            // arrange
            var input = @"
main(x: integer, y : integer) : integer 
    print(x)
    print(y)
    x+1";

            // act
            var parser = new Parser()
            {
                EnableStackTrace = true
            };
            var errorRecord = new ErrorRecord(input);
            var program     = (Program)parser.Parse(new Tokenizer(input), errorRecord);

            // assert
            if (program == null)
            {
                Console.WriteLine(errorRecord.ToString());
            }
            Assert.That(program.Definitions[0].Body.Prints, Is.AstEqual(new ReadOnlyCollection <Print>(new List <Print>
            {
                new Print(0, new Identifier(0, "x")),
                new Print(0, new Identifier(0, "y")),
            })));
        }
コード例 #3
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void Program_WithTwoDefinitions_ShouldBeConstructedCorrectly()
        {
            // arrange
            var input = @"
main() : boolean
    true                      
subsidiary() : integer
    1";

            // act
            var parser = new Parser()
            {
                EnableStackTrace = true
            };
            var ast = parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(ast, Is.AstEqual(new Program(
                                             new Definition
                                             (
                                                 identifier: new Identifier(0, "main"),
                                                 typeDeclaration: new BooleanTypeDeclaration(0),
                                                 formals: new List <Formal>(),
                                                 body: new Body(expr: new BooleanLiteral(0, true))
                                             ),
                                             new Definition
                                             (
                                                 identifier: new Identifier(0, "subsidiary"),
                                                 typeDeclaration: new IntegerTypeDeclaration(0),
                                                 formals: new List <Formal>(),
                                                 body: new Body(expr: new IntegerLiteral(0, "1"))
                                             ))));
        }
コード例 #4
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void ParserShould_GetPrecedence_OfBracketedExpressionCorrect_R34()
        {
            // arrange
            var input = @"main(x: integer, y : integer, z : integer) : integer (x + y) * z";

            // act
            var parser = new Parser()
            {
                EnableStackTrace = true
            };
            var program = (Program)parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(program.Definitions[0].Body.Expr, Is.AstEqual(new TimesOperator
                                                                      (
                                                                          position: 0,
                                                                          left: new PlusOperator
                                                                          (
                                                                              position: 0,
                                                                              left: new Identifier(0, "x"),
                                                                              right: new Identifier(0, "y")
                                                                          ),
                                                                          right: new Identifier(0, "z")
                                                                      )));
        }
コード例 #5
0
        public void Program_WithTwoDefinitions_ShouldBeConstructedCorrectly()
        {
            // arrange
            var input = @"main() : boolean
subsidiary() : integer";

            // act
            var parser = new Parser(DeclarationGrammarParsingTableFactory.Create(), new DeclarationGrammarAstFactory())
            {
                EnableStackTrace = true
            };
            var ast = parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(ast, Is.AstEqual(new Program(
                                             new Definition
                                             (
                                                 identifier: new Identifier(0, "main"),
                                                 typeDeclaration: new BooleanTypeDeclaration(0),
                                                 formals: new List <Formal>(),
                                                 body: null
                                             ),
                                             new Definition
                                             (
                                                 identifier: new Identifier(0, "subsidiary"),
                                                 typeDeclaration: new IntegerTypeDeclaration(0),
                                                 formals: new List <Formal>(),
                                                 body: null
                                             ))));
        }
コード例 #6
0
        public void ParserShould_GenerateAst_WithLeftAssociativeAddition()
        {
            // arrange
            var input = @"x + y + z";

            // act
            var parser = new Parser(ArithmeticGrammarParserTableFactory.Create(), new ArithmeticGrammarAstFactory())
            {
                EnableStackTrace = true
            };
            var ast = parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(ast, Is.AstEqual(new PlusOperator
                                         (
                                             position: 0,
                                             left: new PlusOperator
                                             (
                                                 position: 0,
                                                 left: new Identifier(0, "x"),
                                                 right: new Identifier(0, "y")
                                             ),
                                             right: new Identifier(0, "z")
                                         )));
        }
コード例 #7
0
        public void ParserShould_GetPrecedence_OfBracketedExpressionCorrect()
        {
            // arrange
            var input = @"(x + y) * z";

            // act
            var parser = new Parser(ArithmeticGrammarParserTableFactory.Create(), new ArithmeticGrammarAstFactory())
            {
                EnableStackTrace = true
            };
            var ast = parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(ast, Is.AstEqual(new TimesOperator
                                         (
                                             position: 0,
                                             left: new PlusOperator
                                             (
                                                 position: 0,
                                                 left: new Identifier(0, "x"),
                                                 right: new Identifier(0, "y")
                                             ),
                                             right: new Identifier(0, "z")
                                         )));
        }
コード例 #8
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void BinaryOperators_ShouldBeLeftAssociative(string op, BinaryOperator bop)
        {
            // arrange
            var input = $"main(x: integer, y : integer, z : integer) : integer x {op} y {op} z";

            // act
            var parser  = new Parser();
            var program = (Program)parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(program.Definitions[0].Body.Expr, Is.AstEqual(bop));
        }
コード例 #9
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void ParserShould_GenerateAst_ForAllBinaryOperators(string op, BinaryOperator bop)
        {
            // arrange
            var input = $"main(x: integer, y : integer) : integer x {op} y";

            // act
            var parser  = new Parser();
            var program = (Program)parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(program.Definitions[0].Body.Expr, Is.AstEqual(bop));
        }
コード例 #10
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void ParserShould_ParseSlightlyMoreComplexProgram()
        {
            // arrange
            var input = @"
main(x: integer):integer
    circularPrimesTo(x)
circularPrimesTo(x: integer):integer
    true";

            // act
            var parser = new Parser();
            var ast    = parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(ast, Is.AstEqual(new Program
                                         (
                                             new Definition
                                             (
                                                 new Identifier(0, "main"),
                                                 new IntegerTypeDeclaration(0),
                                                 new List <Formal>
            {
                new Formal(new Identifier(0, "x"), new IntegerTypeDeclaration(0))
            },
                                                 new Body
                                                 (
                                                     new FunctionCall
                                                     (
                                                         new Identifier(0, "circularPrimesTo"),
                                                         new List <Actual> {
                new Actual(new Identifier(0, "x"))
            }
                                                     )
                                                 )
                                             ),
                                             new Definition
                                             (
                                                 new Identifier(0, "circularPrimesTo"),
                                                 new IntegerTypeDeclaration(0),
                                                 new List <Formal>
            {
                new Formal(new Identifier(0, "x"), new IntegerTypeDeclaration(0))
            },
                                                 new Body
                                                 (
                                                     new BooleanLiteral(0, true)
                                                 )
                                             )
                                         )));
        }
コード例 #11
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void ParserShould_ParseExpression_WithBrackets_R34()
        {
            // arrange
            var input = @"main(x: integer) : integer (x)";

            // act
            var parser = new Parser()
            {
                EnableStackTrace = true
            };
            var program = (Program)parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(program.Definitions[0].Body.Expr, Is.AstEqual(new Identifier(0, "x")));
        }
コード例 #12
0
        public void ParserShould_ParseExpression_WithBrackets()
        {
            // arrange
            var input = @"(x)";

            // act
            var parser = new Parser(ArithmeticGrammarParserTableFactory.Create(), new ArithmeticGrammarAstFactory())
            {
                EnableStackTrace = true
            };
            var ast = parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(ast, Is.AstEqual(new Identifier(0, "x")));
        }
コード例 #13
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void ParserShould_GenerateAstForNegateOperators_R33()
        {
            // arrange
            var input = $"main(x: boolean) : boolean - x";

            // act
            var parser = new Parser()
            {
                EnableStackTrace = true
            };
            var program = (Program)parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(program.Definitions[0].Body.Expr, Is.AstEqual(new NegateOperator
                                                                      (
                                                                          position: 0,
                                                                          right: new Identifier(0, "x")
                                                                      )
                                                                      ));
        }
コード例 #14
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void ParseShould_GenerateAstForIfThenElse_R29()
        {
            // arrange
            var input = @"main(x: boolean, y : integer, z : integer) : integer if x then y else z";

            // act
            var parser = new Parser()
            {
                EnableStackTrace = true
            };
            var program = (Program)parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(program.Definitions[0].Body.Expr, Is.AstEqual(new IfThenElse
                                                                      (
                                                                          position: 0,
                                                                          ifExpr: new Identifier(0, "x"),
                                                                          thenExpr: new Identifier(0, "y"),
                                                                          elseExpr: new Identifier(0, "z"))
                                                                      ));
        }
コード例 #15
0
ファイル: ParserTests.cs プロジェクト: matroberts/Compiler
        public void Parser_ShouldIgnore_Comments()
        {
            //arrange
            var input = @"
//line comment should be ignored
main () : boolean
    true";

            //act
            var parser = new Parser();
            var ast    = parser.Parse(new Tokenizer(input), new ErrorRecord(input));

            // assert
            Assert.That(ast, Is.AstEqual(new Program(
                                             new Definition
                                             (
                                                 identifier: new Identifier(0, "main"),
                                                 typeDeclaration: new BooleanTypeDeclaration(0),
                                                 formals: new List <Formal>(),
                                                 body: new Body(expr: new BooleanLiteral(0, true))
                                             ))));
        }