Beispiel #1
0
 public void ParserShouldUnderstandBinaryExpressions() {
     var tree = new Parser("true+true").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.Plus,
             "const", true,
             "const", true,
     });
 }
Beispiel #2
0
 public void ParserShouldUnderstandCallExpressions() {
     var tree = new Parser("print('foo', 'bar')").Parse();
     CheckTree(tree, new object[] {
         "call", TokenKind.Identifier, "print",
             "const", "foo",
             "const", "bar",
     });
 }
Beispiel #3
0
 public void ParserShouldUnderstandCallExpressions2() {
     var tree = new Parser("print 1+2").Parse();
     CheckTree(tree, new object[] {
         "call", TokenKind.Identifier, "print",
             "binop", TokenKind.Plus,
                 "const", 1,
                 "const", 2,
     });
 }
Beispiel #4
0
 public void ParserShouldUnderstandOperatorPrecedence2() {
     var tree = new Parser("1*2+3").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.Plus,
             "binop", TokenKind.Mul,
                 "const", 1,
                 "const", 2,
             "const", 3,
     });
 }
Beispiel #5
0
        protected override EvaluationResult EvaluateSimpleExpression(string expression, Func<string, IList<object>, object> methodInvocationCallback) {
            var ast = new Parser(expression).Parse();
            foreach (var error in ast.GetErrors()) {
                Trace.WriteLine(string.Format("Error during parsing of '{0}': {1}", expression, error.Message));
            }

            if (ast.GetErrors().Any()) {
                return new EvaluationResult(new Error { Message = ast.GetErrors().First().Message });
            }

            var result = new Interpreter().Evalutate(new EvaluationContext {
                Tree = ast,
                MethodInvocationCallback = methodInvocationCallback
            });
            Trace.WriteLine(string.Format("Result of evaluation of '{0}': {1}", expression, result));
            return result;
        }
Beispiel #6
0
 public void ParserShouldContainErrorExpressions2() {
     var tree = new Parser("1 +").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.Plus,
             "const", 1,
             "error",
     });
 }
Beispiel #7
0
 public void ParserShouldContainErrorExpressions() {
     var tree = new Parser("1 + not 3").Parse();
     CheckTree(tree, new object[] {
         "error",
     });
 }
Beispiel #8
0
 public void ParserShouldUnderstandRelationalOperators() {
     var tree = new Parser("true == true").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.EqualEqual,
           "const", true,
           "const", true,
     });
 }
Beispiel #9
0
 public void ParserShouldUnderstandParenthesis() {
     var tree = new Parser("1*(2+3)").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.Mul,
             "const", 1,
             "binop", TokenKind.Plus,
                 "const", 2,
                 "const", 3,
     });
 }
Beispiel #10
0
 public void ParserShouldUnderstandRelationalOperatorPrecedence2() {
     var tree = new Parser("1 < 2 and 2 > 3 or !false").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.Or,
           "binop", TokenKind.And,
             "binop", TokenKind.LessThan,
                 "const", 1,
                 "const", 2,
               "binop", TokenKind.GreaterThan,
                 "const", 2,
                 "const", 3,
             "unop", TokenKind.NotSign,
               "const", false,
     });
 }
Beispiel #11
0
 public void ParserShouldUnderstandRelationalOperators7() {
     var tree = new Parser("null == null").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.EqualEqual,
           "const", null,
           "const", null,
     });
 }
Beispiel #12
0
 public void ParserShouldUnderstandRelationalOperators6() {
     var tree = new Parser("1 >= 2").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.GreaterThanEqual,
           "const", 1,
           "const", 2,
     });
 }
Beispiel #13
0
 public void ParserShouldIgnoreWhitespaces() {
     var tree = new Parser("  true \n  ").Parse();
     CheckTree(tree, new object[] {
         "const", true,
     });
 }
Beispiel #14
0
 public void ParserShouldUnderstandRelationalOperators3() {
     var tree = new Parser("1 < 2").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.LessThan,
           "const", 1,
           "const", 2,
     });
 }
Beispiel #15
0
 public void ParserShouldUnderstandOperatorPrecedence3() {
     var tree = new Parser("not true or true").Parse();
     CheckTree(tree, new object[] {
         "binop", TokenKind.Or,
             "unop", TokenKind.Not,
                 "const", true,
             "const", true,
     });
 }
Beispiel #16
0
 public void ParserShouldUnderstandComplexExpressions() {
     var tree = new Parser("not 1 * (2 / 4 * 6 + (3))").Parse();
     CheckTree(tree, new object[] {
         "unop", TokenKind.Not,
             "binop", TokenKind.Mul,
                 "const", 1,
                 "binop", TokenKind.Plus,
                     "binop", TokenKind.Mul,
                         "binop", TokenKind.Div,
                             "const", 2,
                             "const", 4,
                         "const", 6,
                     "const", 3,
     });
 }
Beispiel #17
0
 public void ParserShouldUnderstandConstantExpressions() {
     var tree = new Parser("true").Parse();
     CheckTree(tree, new object[] {
         "const", true,
     });
 }