コード例 #1
0
            public void FloatsAreParsedToAConstExpression()
            {
                var parser = new StatementParser(new FakeScanner(new[] { new Token("FLOAT", "2.34", 0, 0, 0) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(ConstantExpr), result.GetType());
            }
コード例 #2
0
            public void IntegersAreParsedToAConstExpression()
            {
                var parser = new StatementParser(new FakeScanner(new[] { new Token("INTEGER", "1", 0, 0, 0) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(ConstantExpr), result.GetType());
            }
コード例 #3
0
 public void ImplementsParse()
 {
     IParser target = new StatementParser(new FakeScanner(new[] { Token.Empty }));
     try
     {
         Expression actual = target.ParseAll();
         Assert.Fail("No tokens to parse");
     }
     catch (ParseException)
     {
     }
 }
コード例 #4
0
            public void MoreComplexIdentifierExpression()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("a"), Get("+"), Get(1), Get("*"), Get("b"), Get("-"), Get(4) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(MinusExpr), result.GetType());

                var expr = (MinusExpr)result;

                Assert.AreEqual(typeof(PlusExpr), expr.Left.GetType());
                Assert.AreEqual(typeof(ConstantExpr), expr.Right.GetType());
            }
コード例 #5
0
            public void SimpleIdentifier()
            {
                var parser = new StatementParser(new FakeScanner(new[]
                {
                    Get("b")
                }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(IdentifierExpr), result.GetType());
                var expr = (IdentifierExpr)result;

                Assert.AreEqual("b", expr.Name);
            }
コード例 #6
0
            public void PowDivOperatorPrecedence()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(3), Get("^"), Get(2), Get("/"), Get(5) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(DivExpr), result.GetType());
            }
コード例 #7
0
            public void SimpleSubtraction()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(6), Get("-"), Get(3) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(MinusExpr), result.GetType());

                var expr = (MinusExpr)result;

                Assert.AreEqual(typeof(ConstantExpr), expr.Left.GetType());
                Assert.AreEqual(typeof(ConstantExpr), expr.Right.GetType());

                Assert.AreEqual(6, ((ConstantExpr)expr.Left).Value);
                Assert.AreEqual(3, ((ConstantExpr)expr.Right).Value);
            }
コード例 #8
0
            public void FunctionCallTest()
            {
                var parser = new StatementParser(new FakeScanner(new[]
                {
                    Get("Test"), Get("("), Get(")"), Get(";"),
                }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(FunctionCallExpr), result.GetType());

                var stmt = (FunctionCallExpr)result;

                Assert.AreEqual("Test", stmt.FunctionName.Name);
                Assert.AreEqual(0, stmt.Arguments.Count());
            }
コード例 #9
0
            public void FunctionCallWithMultipleExpressionParametersTest()
            {
                var parser = new StatementParser(new FakeScanner(new[]
                {
                    Get("Test"), Get("("), Get(1), Get("+"), Get(2), Get("*"), Get(5), Get(","), Get(1), Get("*"), Get(2), Get(")"), Get(";"),
                }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(FunctionCallExpr), result.GetType());

                var stmt = (FunctionCallExpr)result;

                Assert.AreEqual("Test", stmt.FunctionName.Name);
                Assert.AreEqual(2, stmt.Arguments.Count());
                Assert.AreEqual(typeof(PlusExpr), stmt.Arguments.ElementAt(0).GetType());
                Assert.AreEqual(typeof(MultExpr), stmt.Arguments.ElementAt(1).GetType());
            }
コード例 #10
0
            public void BlockStatement()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("{"), Get("a"), Get("="), Get("b"), Get("+"), Get(1), Get(";"), Get("}") }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(ScopeBlockStmt), result.GetType());
            }
コード例 #11
0
            public void IfElseStatementNoBlock()
            {
                var parser = new StatementParser(new FakeScanner(new[]
                {
                    Get("if"), Get("("), Get(1), Get("!="), Get(3), Get(")"),
                        Get("a"), Get("="), Get("b"), Get("+"), Get(1), Get(";"),
                    Get("else"),
                        Get("c"), Get("="), Get("d"), Get("+"), Get(2), Get(";"),
                }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(IfStmt), result.GetType());

                var stmt = (IfStmt)result;

                Assert.AreEqual(typeof(NotEqualsExpr), stmt.Condition.GetType());
                Assert.AreEqual(typeof(AssignmentExpr), stmt.ThenExpression.GetType());
                Assert.AreEqual(typeof(AssignmentExpr), stmt.ElseExpression.GetType());

                Assert.AreNotSame(stmt.ThenExpression, stmt.ElseExpression);
            }
コード例 #12
0
            public void LogicalNotTest()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("!"), Get(false) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(NotExpr), result.GetType());

                var expr = (NotExpr)result;

                Assert.AreEqual(typeof(ConstantExpr), expr.Right.GetType());
                Assert.AreEqual(false, ((ConstantExpr)expr.Right).Value);
            }
コード例 #13
0
            public void WhileStatementSingle()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("while"), Get("("), Get(1), Get("!="), Get(3), Get(")"), Get("a"), Get("="), Get("b"), Get("+"), Get(1), Get(";") }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(WhileStmt), result.GetType());

                var expr = (WhileStmt)result;

                Assert.AreEqual(typeof(NotEqualsExpr), expr.Condition.GetType());
                Assert.AreEqual(typeof(AssignmentExpr), expr.Block.GetType());
            }
コード例 #14
0
            public void SimplePower()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(3), Get("^"), Get(2) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(PowExpr), result.GetType());

                var expr = (PowExpr)result;

                Assert.AreEqual(typeof(ConstantExpr), expr.Left.GetType());
                Assert.AreEqual(typeof(ConstantExpr), expr.Right.GetType());

                Assert.AreEqual(3, ((ConstantExpr)expr.Left).Value);
                Assert.AreEqual(2, ((ConstantExpr)expr.Right).Value);
            }
コード例 #15
0
            public void LessThanOrEqualToTest()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(2), Get("<="), Get(3) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(LessThanOrEqualsExpr), result.GetType());

                var expr = (LessThanOrEqualsExpr)result;

                Assert.AreEqual(typeof(ConstantExpr), expr.Left.GetType());
                Assert.AreEqual(typeof(ConstantExpr), expr.Right.GetType());

                Assert.AreEqual(2, ((ConstantExpr)expr.Left).Value);
                Assert.AreEqual(3, ((ConstantExpr)expr.Right).Value);
            }
コード例 #16
0
            public void DefaultOperatorPrecedenceAsCounterExampleToAbove()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(2), Get("^"), Get(3), Get("*"), Get(4) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(MultExpr), result.GetType());
            }
コード例 #17
0
            public void NestedParenthesisOperatorPrecedence()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("("), Get(2), Get("^"), Get("("), Get(3), Get("*"), Get(4), Get(")"), Get(")") }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(PowExpr), result.GetType());
            }
コード例 #18
0
            public void SimpleParenthesis()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("("), Get(1), Get(")") }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(ConstantExpr), result.GetType());

                var expr = (ConstantExpr)result;

                Assert.AreEqual(1, expr.Value);
            }
コード例 #19
0
            public void MoreComplexAssignment()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("a"), Get("="), Get("b"), Get("+"), Get(1), Get("*"), Get(5), Get("-"), Get(4) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(AssignmentExpr), result.GetType());

                var expr = (AssignmentExpr)result;

                Assert.AreEqual(typeof(IdentifierExpr), expr.Left.GetType());
                Assert.AreEqual(typeof(MinusExpr), expr.Right.GetType());
            }
コード例 #20
0
            public void LogicalNotPrecedenceTest()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(true), Get("&&"), Get("!"), Get(false) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(AndExpr), result.GetType());

                var expr = (AndExpr)result;

                Assert.AreEqual(typeof(ConstantExpr), expr.Left.GetType());
                Assert.AreEqual(typeof(NotExpr), expr.Right.GetType());
            }
コード例 #21
0
            public void WhileStatementWithBlock()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("while"), Get("("), Get(1), Get("!="), Get(3), Get(")"), Get("{"), Get("a"), Get("="), Get("b"), Get("+"), Get(1), Get(";"), Get("}") }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(WhileStmt), result.GetType());

                var expr = (WhileStmt)result;

                Assert.AreEqual(typeof(NotEqualsExpr), expr.Condition.GetType());
                Assert.AreEqual(typeof(ScopeBlockStmt), expr.Block.GetType());

                var block = (ScopeBlockStmt)expr.Block;
                Assert.AreEqual(1, block.Statements.Count());
                Assert.AreEqual(typeof(AssignmentExpr), block.Statements.ElementAt(0).GetType());
            }
コード例 #22
0
            public void ConditionalExpressionTest()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(true), Get("?"), Get(1234), Get(":"), Get(5678) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(ConditionalExpr), result.GetType());

                var expr = (ConditionalExpr)result;

                Assert.AreEqual(typeof(ConstantExpr), expr.Condition.GetType());
                Assert.AreEqual(typeof(ConstantExpr), expr.ThenExpression.GetType());
                Assert.AreEqual(typeof(ConstantExpr), expr.ElseExpression.GetType());

                Assert.AreEqual(true, ((ConstantExpr)expr.Condition).Value);
                Assert.AreEqual(1234, ((ConstantExpr)expr.ThenExpression).Value);
                Assert.AreEqual(5678, ((ConstantExpr)expr.ElseExpression).Value);
            }
コード例 #23
0
            public void WhileStatementWithExpressionBlock()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("while"), Get("("), Get(1), Get("!="), Get(3), Get(")"), Get("{"), Get("b"), Get("+"), Get(1), Get(";"), Get("}") }));

                parser.ParseAll();
            }
コード例 #24
0
            public void SubExpressionInAllPositionsTest()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(true), Get("&&"), Get(false), Get("?"), Get(1234), Get("+"), Get(2345), Get(":"), Get(3), Get("^"), Get(2) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(ConditionalExpr), result.GetType());

                var expr = (ConditionalExpr)result;

                Assert.AreEqual(typeof(AndExpr), expr.Condition.GetType());
                Assert.AreEqual(typeof(PlusExpr), expr.ThenExpression.GetType());
                Assert.AreEqual(typeof(PowExpr), expr.ElseExpression.GetType());
            }
コード例 #25
0
            public void IfStatementWithBlock()
            {
                var parser = new StatementParser(new FakeScanner(new[]
                {
                    Get("if"), Get("("), Get(1), Get("!="), Get(3), Get(")"),
                    Get("{"),
                        Get("a"), Get("="), Get("b"), Get("+"), Get(1), Get(";"),
                    Get("}")
                }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(IfStmt), result.GetType());

                var stmt = (IfStmt) result;

                Assert.AreEqual(typeof(NotEqualsExpr), stmt.Condition.GetType());
                Assert.AreEqual(typeof(ScopeBlockStmt), stmt.ThenExpression.GetType());
                Assert.AreEqual(typeof(NoOpStatement), stmt.ElseExpression.GetType());
            }
コード例 #26
0
            public void SimpleDivision()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(10), Get("/"), Get(5) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(DivExpr), result.GetType());

                var expr = (DivExpr)result;

                Assert.AreEqual(typeof(ConstantExpr), expr.Left.GetType());
                Assert.AreEqual(typeof(ConstantExpr), expr.Right.GetType());

                Assert.AreEqual(10, ((ConstantExpr)expr.Left).Value);
                Assert.AreEqual(5, ((ConstantExpr)expr.Right).Value);
            }
コード例 #27
0
            public void IfIfElseStatementNoBlocks()
            {
                var parser = new StatementParser(new FakeScanner(new[]
                {
                    Get("if"), Get("("), Get(true), Get(")"),
                        Get("if"), Get("("), Get(false), Get(")"),
                            Get("a"), Get("="), Get("b"), Get("+"), Get(1), Get(";"),
                        Get("else"),
                            Get("c"), Get("="), Get("d"), Get("+"), Get(2), Get(";"),
                }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(IfStmt), result.GetType());

                var stmt = (IfStmt)result;

                Assert.AreEqual(typeof(ConstantExpr), stmt.Condition.GetType());
                Assert.AreEqual(true, ((ConstantExpr)stmt.Condition).Value);

                Assert.AreEqual(typeof(IfStmt), stmt.ThenExpression.GetType());
                Assert.AreEqual(typeof(NoOpStatement), stmt.ElseExpression.GetType());

                var innerIf = (IfStmt)stmt.ThenExpression;
                Assert.AreEqual(typeof(ConstantExpr), innerIf.Condition.GetType());
                Assert.AreEqual(false, ((ConstantExpr)innerIf.Condition).Value);
            }
コード例 #28
0
            public void NegationExpressionTest()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get("-"), Get(5) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(NegationExpr), result.GetType());

                var expr = (NegationExpr)result;

                Assert.AreEqual(typeof(ConstantExpr), expr.Right.GetType());
                Assert.AreEqual(5, ((ConstantExpr)expr.Right).Value);
            }
コード例 #29
0
            public void FunctionCallWithMultipleParametersTest()
            {
                var parser = new StatementParser(new FakeScanner(new[]
                {
                    Get("Test"), Get("("), Get(1), Get(","), Get(2), Get(","), Get(3), Get(")"), Get(";"),
                }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(FunctionCallExpr), result.GetType());

                var stmt = (FunctionCallExpr)result;

                Assert.AreEqual("Test", stmt.FunctionName.Name);
                Assert.AreEqual(3, stmt.Arguments.Count());

                Assert.AreEqual(1, ((ConstantExpr)stmt.Arguments.ElementAt(0)).Value);
                Assert.AreEqual(2, ((ConstantExpr)stmt.Arguments.ElementAt(1)).Value);
                Assert.AreEqual(3, ((ConstantExpr)stmt.Arguments.ElementAt(2)).Value);
            }
コード例 #30
0
            public void AndOrPrecedenceTest6()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(true), Get("&&"), Get("("), Get(true), Get("||"), Get(false), Get(")") }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(AndExpr), result.GetType());
            }
コード例 #31
0
            public void FunctionDefinitionWithParametersTest()
            {
                var parser = new StatementParser(new FakeScanner(new[]
                {
                    Get("function"), Get("Multiply"), Get("("), Get("a"), Get(":"), Get("int"), Get(","), Get("b"), Get(":"), Get("int"), Get(")"),
                    Get("{"),
                        Get("return"), Get("a"), Get("*"), Get("b"), Get(";"),
                    Get("}")
                }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(FunctionDefinitionExpr), result.GetType());

                var stmt = (FunctionDefinitionExpr)result;

                Assert.AreEqual("Multiply", stmt.Name);
                Assert.AreEqual(typeof(ScopeBlockStmt), stmt.Body.GetType());
            }
コード例 #32
0
            public void MultPlusOperatorPrecedence()
            {
                var parser = new StatementParser(new FakeScanner(new[] { Get(3), Get("*"), Get(2), Get("+"), Get(5) }));

                var result = parser.ParseAll();

                Assert.AreEqual(typeof(PlusExpr), result.GetType());
            }