public void Test_Correct_TokenType() { string input = "everything (over 1.11 + 1)&& ;<"; var tokens = TextTokenizer.TextTokenize(input); Assert.Equal(TextTokenType.Word, tokens[0].Type); Assert.Equal(TextTokenType.ParentheseStart, tokens[1].Type); Assert.Equal(TextTokenType.Word, tokens[2].Type); Assert.Equal(TextTokenType.Number, tokens[3].Type); Assert.Equal(TextTokenType.Operator, tokens[4].Type); Assert.Equal(TextTokenType.Number, tokens[5].Type); Assert.Equal(TextTokenType.ParentheseStop, tokens[6].Type); Assert.Equal(TextTokenType.Operator, tokens[7].Type); Assert.Equal(TextTokenType.EndOfSentence, tokens[8].Type); Assert.Equal(TextTokenType.Operator, tokens[9].Type); }
public void Test_Correct_TokenContent() { string input = "everything is over 1.11 + 1 && 1 < 1"; var tokens = TextTokenizer.TextTokenize(input); Assert.Equal("everything", new String(tokens[0].RawData.ToArray())); Assert.Equal("is", new String(tokens[1].RawData.ToArray())); Assert.Equal("over", new String(tokens[2].RawData.ToArray())); Assert.Equal("1.11", new String(tokens[3].RawData.ToArray())); Assert.Equal("+", new String(tokens[4].RawData.ToArray())); Assert.Equal("1", new String(tokens[5].RawData.ToArray())); Assert.Equal("&&", new String(tokens[6].RawData.ToArray())); Assert.Equal("1", new String(tokens[7].RawData.ToArray())); Assert.Equal("<", new String(tokens[8].RawData.ToArray())); Assert.Equal("1", new String(tokens[9].RawData.ToArray())); }
public void Order_Of_Adding_And_Multiplyging_Is_Correct2() { string input = "1+2*3;"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); Assert.Equal(ExpressionType.BinaryOperation, AST.Type); Assert.Equal(BinaryExpressionType.Add, ((BinaryExpressionNode)AST).BinaryType); }
public void One_Plus_One_Is_Two() { string input = "1 + 1"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); global::BRE.ExecutionContext context = new global::BRE.ExecutionContext(); ExecutionEngine executionEngine = new ExecutionEngine(context); executionEngine.ExecuteExpression(AST); Assert.Equal(2.0, (double)AST.EvaluatedValue.Value); }
public void Basic_Boolean_Logic_Got_Correct_AST() { string input = "1 + 3 > 1 && 1 + 1 > 0"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); var BinaryAst = (BinaryExpressionNode)AST; var left = (BinaryExpressionNode)BinaryAst.Left; var right = (BinaryExpressionNode)BinaryAst.Right; Assert.Equal(BinaryExpressionType.GreaterThan, left.BinaryType); Assert.Equal(BinaryExpressionType.GreaterThan, right.BinaryType); }
public void One_Plus_Two_Is_Greater_Than_Two() { string input = "1 + 2 > 2"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); global::BRE.ExecutionContext context = new global::BRE.ExecutionContext(); ExecutionEngine executionEngine = new ExecutionEngine(context); executionEngine.ExecuteExpression(AST); Assert.Equal(ValueType.Logical, AST.EvaluatedValue.Type); Assert.Equal(true, (bool)AST.EvaluatedValue.Value); }
public void Variables_Can_Be_Found_By_Name() { string input = "1+Apple"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); var BinaryAst = (BinaryExpressionNode)AST; var left = (ConstantExpression)BinaryAst.Left; var right = (VariableExpression)BinaryAst.Right; Assert.Equal(BinaryExpressionType.Add, BinaryAst.BinaryType); Assert.Equal(1.0, (double)left.Value.Value); Assert.Equal("Apple", right.VariableValue.VariableName); }
public void Add_Operator_Has_Higher_Precedence_Than_Adding() { string input = "1 && 1 + 1"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); var BinaryAst = (BinaryExpressionNode)AST; var left = (ConstantExpression)BinaryAst.Left; var right = (BinaryExpressionNode)BinaryAst.Right; Assert.Equal(BinaryExpressionType.And, BinaryAst.BinaryType); Assert.Equal(1.0, (double)left.Value.Value); Assert.Equal(BinaryExpressionType.Add, right.BinaryType); }
public void Variable_Can_Be_Found_From_Context() { string input = "Word"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); global::BRE.ExecutionContext context = new global::BRE.ExecutionContext(); context.AddVariableValue("word", 2.313); ExecutionEngine executionEngine = new ExecutionEngine(context); executionEngine.ExecuteExpression(AST); Assert.Equal(ValueType.Number, AST.EvaluatedValue.Type); Assert.Equal(2.313, (double)AST.EvaluatedValue.Value); }
public void Parentheses_Affect_The_Order_Of_AST() { string input = "1*(2+2.2)+3;"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); var BinaryAst = (BinaryExpressionNode)AST; var left = (BinaryExpressionNode)BinaryAst.Left; var leftRight = (ExpressionNode)left.Right; var targetExpression = (BinaryExpressionNode)leftRight.Expression; Assert.Equal(BinaryExpressionType.Add, targetExpression.BinaryType); Assert.Equal(2.0, (double)targetExpression.Left.Value.Value); Assert.Equal(2.2, (double)targetExpression.Right.Value.Value); }
public void Ternary_Expression_Parsed_Correctly() { string input = " 1 == 2 ? 2 : 3"; var TextTokens = TextTokenizer.TextTokenize(input); Parser parsers = new Parser(TextTokens); var AST = parsers.ParseExpression(); var ternary = (TernaryExpressionNode)AST; var first = (BinaryExpressionNode)ternary.First; var second = (ConstantExpression)ternary.Second; var third = (ConstantExpression)ternary.Third; Assert.Equal(BinaryExpressionType.Equal, first.BinaryType); Assert.NotNull(second); Assert.NotNull(third); }