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()); }
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()); }
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) { } }
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()); }
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); }
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()); }
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); }
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()); }
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()); }
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()); }
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); }
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); }
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()); }
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); }
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); }
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()); }
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()); }
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); }
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()); }
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()); }
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()); }
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); }
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(); }
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()); }
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()); }
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); }
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); }
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); }
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); }
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()); }
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()); }
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()); }