public void IndexExpressionParsingTest() { var input = "someList[1 + 1]"; var lexer = new Lexer(input); var parser = new Parser(lexer); var program = parser.ParseCode(); StaticTests.CheckParserErrors(parser); Assert.AreEqual(nameof(ExpressionStatement), program.Statements.First().GetType().Name); var stmt = (ExpressionStatement)program.Statements.First(); Assert.AreEqual(nameof(IndexExpression), stmt.Expression.GetType().Name); var indexExp = (IndexExpression)stmt.Expression; StaticTests.TestIdentifier(indexExp.Left, "someList"); var int1 = new IntegerLiteral { Token = new Token { Type = Token.Int, Literal = "1" }, Value = 1 }; StaticTests.TestInfixExpression(indexExp.Index, int1, "+", int1); }
public void BoolInfixExpressionsParseTest() { var infixTests = new BoolInfixTest[] { new() { Input = "true == true", LeftValue = true, RightValue = true, Operator = "==" }, new() { Input = "true != false", LeftValue = true, RightValue = false, Operator = "!=" }, new() { Input = "false == false", LeftValue = false, RightValue = false, Operator = "==" } }; foreach (var infixTest in infixTests) { var lexer = new Lexer(infixTest.Input); var parser = new Parser(lexer); var program = parser.ParseCode(); StaticTests.CheckParserErrors(parser); Assert.AreEqual(1, program.Statements.Count, $"program should have 1 statement, got={program.Statements.Count}"); var stmt = program.Statements.First() as ExpressionStatement; Assert.AreEqual("ExpressionStatement", stmt.GetType().Name, $"first statement not of type 'ExpressionStatement', got={stmt.GetType().Name}"); var exp = stmt.Expression as InfixExpression; StaticTests.TestInfixExpression(stmt.Expression, exp.Left, exp.Operator, exp.Right); } } }
public void ArrayLiteralParsingTest() { const string input = "[1, 2 * 2, 3 + 3]"; var lexer = new Lexer(input); var parser = new Parser(lexer); var program = parser.ParseCode(); StaticTests.CheckParserErrors(parser); Assert.AreEqual(nameof(ExpressionStatement), program.Statements.First().GetType().Name); var stmt = (ExpressionStatement)program.Statements.First(); Assert.AreEqual(nameof(ArrayLiteral), stmt.Expression.GetType().Name); var array = (ArrayLiteral)stmt.Expression; Assert.AreEqual(3, array.Elements.Count); StaticTests.TestIntegerLiteral(array.Elements.First(), 1); IntegerLiteral two = 2; IntegerLiteral three = 3; StaticTests.TestInfixExpression(array.Elements[1], two, "*", two); StaticTests.TestInfixExpression(array.Elements[2], three, "+", three); }
public void InfixExpressionsParseTest() { var infixTests = new InfixTest[] { new() { Input = "5 + 5", LeftValue = 5, RightValue = 5, Operator = "+" }, new() { Input = "5 - 5", LeftValue = 5, RightValue = 5, Operator = "-" }, new() { Input = "5 * 5", LeftValue = 5, RightValue = 5, Operator = "*" }, new() { Input = "5 / 5", LeftValue = 5, RightValue = 5, Operator = "/" }, new() { Input = "5 > 5", LeftValue = 5, RightValue = 5, Operator = ">" }, new() { Input = "5 < 5", LeftValue = 5, RightValue = 5, Operator = "<" }, new() { Input = "5 == 5", LeftValue = 5, RightValue = 5, Operator = "==" }, new() { Input = "5 != 5", LeftValue = 5, RightValue = 5, Operator = "!=" } }; foreach (var infixTest in infixTests) { var lexer = new Lexer(infixTest.Input); var parser = new Parser(lexer); var program = parser.ParseCode(); StaticTests.CheckParserErrors(parser); Assert.AreEqual(1, program.Statements.Count, $"program should have 1 statement, got={program.Statements.Count}"); var stmt = program.Statements.First() as ExpressionStatement; Assert.AreEqual("ExpressionStatement", stmt.GetType().Name, $"first statement not of type 'ExpressionStatement', got={stmt.GetType().Name}"); var exp = stmt.Expression as InfixExpression; StaticTests.TestInfixExpression(stmt.Expression, exp.Left, exp.Operator, exp.Right); } }
public void HashLiteralWithExpressionParsingTest() { const string input = "{'one': 0 + 1, 'two': 228 - 226, 'three': 45 / 15}"; var lexer = new Lexer(input); var parser = new Parser(lexer); var program = parser.ParseCode(); StaticTests.CheckParserErrors(parser); Assert.AreEqual(nameof(ExpressionStatement), program.Statements.First().GetType().Name); var stmt = (ExpressionStatement)program.Statements.First(); Assert.AreEqual(nameof(HashLiteral), stmt.Expression.GetType().Name); var hash = (HashLiteral)stmt.Expression; Assert.AreEqual(3, hash.Pairs.Count); var tests = new Dictionary <string, ExpressionTestDelegate> { { "one", expression => StaticTests.TestInfixExpression(expression, (IntegerLiteral)0, "+", (IntegerLiteral)1) }, { "two", expression => StaticTests.TestInfixExpression(expression, (IntegerLiteral)228, "-", (IntegerLiteral)226) }, { "three", expression => StaticTests.TestInfixExpression(expression, (IntegerLiteral)45, "/", (IntegerLiteral)15) } }; foreach (var pairsKey in hash.Pairs.Keys) { var value = hash.Pairs[pairsKey]; Assert.AreEqual(nameof(StringLiteral), pairsKey.GetType().Name); var literal = (StringLiteral)pairsKey; var keyExists = tests.TryGetValue(literal.Str(), out var testFunc); Assert.True(keyExists); testFunc(value); } }