Beispiel #1
0
        public void CompileAndEvaluateComplexExpressionWithParenthesis()
        {
            Parser parser = new Parser("(6+3)/(1+2)");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BinaryOperatorExpression));
            Assert.AreEqual(3, expression.Evaluate(new BindingEnvironment()));

            Assert.IsNull(parser.CompileExpression());
        }
Beispiel #2
0
        public void CompileAndEvaluateComplexListExpression()
        {
            Parser parser = new Parser("[1, 2, [a, b], 'spam']");
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("a", 1);
            environment.SetValue("b", 2);

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ListExpression));
            Assert.IsFalse(((ListExpression)expression).IsReadOnly);

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IList));

            IList list = (IList)result;

            Assert.AreEqual(4, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
            Assert.IsNotNull(list[2]);
            Assert.IsInstanceOfType(list[2], typeof(IList));
            Assert.AreEqual("spam", list[3]);

            IList list2 = (IList)list[2];

            Assert.IsNotNull(list2);
            Assert.AreEqual(2, list2.Count);
            Assert.AreEqual(1, list2[0]);
            Assert.AreEqual(2, list2[1]);
        }
Beispiel #3
0
        public void CompileAndEvaluateAddExpression()
        {
            Parser parser = new Parser("1+2");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BinaryOperatorExpression));
            Assert.AreEqual(3, expression.Evaluate(new BindingEnvironment()));
        }
Beispiel #4
0
        public object Apply(IContext context, IList<object> arguments, IDictionary<string, object> namedArguments)
        {
            int nargs = arguments == null ? 0 : arguments.Count;

            if (nargs == 0)
                throw new TypeError("eval expected at least 1 arguments, got 0");

            // TODO implement bytes or code object
            if (!(arguments[0] is string))
                throw new TypeError("eval() arg 1 must be a string, bytes or code object");

            Parser parser = new Parser((string)arguments[0]);

            IExpression expression = parser.CompileExpression();

            if (expression == null)
                return null;

            return expression.Evaluate(context);
        }
Beispiel #5
0
        public void CompileAndEvaluateArrayAsListExpression()
        {
            Parser parser = new Parser("[1,2,'spam']");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ListExpression));
            Assert.IsFalse(((ListExpression)expression).IsReadOnly);

            object result = expression.Evaluate(new BindingEnvironment());

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IList));

            IList list = (IList)result;

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
            Assert.AreEqual("spam", list[2]);
        }
Beispiel #6
0
        public void CompileAttributeExpression()
        {
            Parser parser = new Parser("module.spam");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(AttributeExpression));

            AttributeExpression attrexpr = (AttributeExpression)expression;

            Assert.IsNotNull(attrexpr.Expression);
            Assert.IsInstanceOfType(attrexpr.Expression, typeof(NameExpression));
            Assert.AreEqual("spam", attrexpr.Name);
        }
Beispiel #7
0
        public void CompileAndEvaluateNullDictionaryExpression()
        {
            Parser parser = new Parser("{ }");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(DictionaryExpression));

            var dictexpr = (DictionaryExpression)expression;

            Assert.AreEqual(0, dictexpr.KeyExpressions.Count);
            Assert.AreEqual(0, dictexpr.ValueExpressions.Count);

            object result = expression.Evaluate(new BindingEnvironment());

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IDictionary));

            IDictionary dictionary = (IDictionary)result;

            Assert.AreEqual(0, dictionary.Keys.Count);
            Assert.AreEqual(0, dictionary.Values.Count);
        }
Beispiel #8
0
        public void CompileNoneAsNullConstantExpression()
        {
            Parser parser = new Parser("None");
            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ConstantExpression));

            ConstantExpression cexpr = (ConstantExpression)expression;

            Assert.IsNull(cexpr.Value);
        }
Beispiel #9
0
        public void CompileMethodCallExpression()
        {
            Parser parser = new Parser("foo.find('spam')");
            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(CallExpression));

            CallExpression mcexpression = (CallExpression)expression;
            Assert.IsNotNull(mcexpression.TargetExpression);
            Assert.IsInstanceOfType(mcexpression.TargetExpression, typeof(AttributeExpression));
            Assert.AreEqual("find", ((AttributeExpression)mcexpression.TargetExpression).Name);
            Assert.IsNotNull(mcexpression.ArgumentExpressions);
            Assert.AreEqual(1, mcexpression.ArgumentExpressions.Count);
            Assert.IsInstanceOfType(mcexpression.ArgumentExpressions[0], typeof(ConstantExpression));
        }
Beispiel #10
0
        public void CompileCallExpression()
        {
            Parser parser = new Parser("len('spam')");
            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(CallExpression));
        }
Beispiel #11
0
        public void CompileCompareExpression()
        {
            Parser parser = new Parser("a < b");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(CompareExpression));

            Assert.IsNull(parser.CompileExpression());
        }
Beispiel #12
0
        public void CompileString()
        {
            Parser parser = new Parser("\"spam\"");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ConstantExpression));

            Assert.AreEqual("spam", ((ConstantExpression)expression).Value);
        }
Beispiel #13
0
 public void RaiseIfCompileListWithMissingComma()
 {
     Parser parser = new Parser("[1 2]");
     parser.CompileExpression();
 }
Beispiel #14
0
        public void CompileSimpleExpressionInParenthesis()
        {
            Parser parser = new Parser("(1)");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ConstantExpression));
        }
Beispiel #15
0
        public void CompileSlicedExpressionWithNullEnd()
        {
            Parser parser = new Parser("'spam'[1:]");
            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(SlicedExpression));

            SlicedExpression sexpr = (SlicedExpression)expression;

            Assert.AreEqual("spam", sexpr.TargetExpression.Evaluate(null));
            Slice slice = (Slice)sexpr.SliceExpression.Evaluate(null);
            Assert.IsTrue(slice.Begin.HasValue);
            Assert.IsFalse(slice.End.HasValue);
            Assert.AreEqual(1, slice.Begin.Value);
        }
Beispiel #16
0
        public void CompileOrExpression()
        {
            Parser parser = new Parser("2 or 3");

            var expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BooleanExpression));

            var booleanexpression = (BooleanExpression)expression;
            Assert.AreEqual(BooleanOperator.Or, booleanexpression.Operation);
            Assert.IsInstanceOfType(booleanexpression.Left, typeof(ConstantExpression));
            Assert.IsInstanceOfType(booleanexpression.Right, typeof(ConstantExpression));
        }
Beispiel #17
0
        public void CompileNotExpression()
        {
            Parser parser = new Parser("not 3");

            var expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(NotExpression));

            var notexpression = (NotExpression)expression;
            Assert.IsInstanceOfType(notexpression.Expression, typeof(ConstantExpression));
        }
Beispiel #18
0
        public void CompileAndEvaluateDictionaryExpression()
        {
            Parser parser = new Parser("{ 'firstname': 'spam', 'lastname': 'bar' }");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(DictionaryExpression));

            object result = expression.Evaluate(new BindingEnvironment());

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IDictionary));

            IDictionary dictionary = (IDictionary)result;

            Assert.AreEqual(2, dictionary.Keys.Count);
            Assert.AreEqual("spam", dictionary["firstname"]);
            Assert.AreEqual("bar", dictionary["lastname"]);
        }
Beispiel #19
0
        public void CompileBoolean()
        {
            Parser parser = new Parser("true");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ConstantExpression));

            Assert.AreEqual(true, ((ConstantExpression)expression).Value);

            Assert.IsNull(parser.CompileExpression());
        }
Beispiel #20
0
        public void CompileEmptyTupleExpression()
        {
            Parser parser = new Parser("()");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ListExpression));

            var lexpr = (ListExpression)expression;

            Assert.IsTrue(lexpr.IsReadOnly);
            Assert.AreEqual(0, lexpr.Expressions.Count);
        }
Beispiel #21
0
        public void CompileBooleanExpression()
        {
            Parser parser = new Parser("2 and 4 or not 3");

            var expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BooleanExpression));

            var booleanexpression = (BooleanExpression)expression;
            Assert.AreEqual(BooleanOperator.Or, booleanexpression.Operation);
            Assert.IsInstanceOfType(booleanexpression.Left, typeof(BooleanExpression));
            Assert.IsInstanceOfType(booleanexpression.Right, typeof(NotExpression));

            var leftexpression = (BooleanExpression)booleanexpression.Left;

            Assert.AreEqual(BooleanOperator.And, leftexpression.Operation);
        }
Beispiel #22
0
        public void CompileName()
        {
            Parser parser = new Parser("name");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(NameExpression));

            Assert.AreEqual("name", ((NameExpression)expression).Name);
        }
Beispiel #23
0
        public void CompileChainedNamedCalls()
        {
            Parser parser = new Parser("f.foo().bar()");
            var expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(CallExpression));

            var call = (CallExpression)expression;

            Assert.IsNotNull(call.TargetExpression);
            Assert.IsInstanceOfType(call.TargetExpression, typeof(AttributeExpression));

            var attr = (AttributeExpression)call.TargetExpression;

            Assert.IsNotNull(attr.Expression);
            Assert.IsInstanceOfType(attr.Expression, typeof(CallExpression));
        }
Beispiel #24
0
        public void CompileInteger()
        {
            Parser parser = new Parser("123");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ConstantExpression));

            Assert.AreEqual(123, ((ConstantExpression)expression).Value);

            Assert.IsNull(parser.CompileExpression());
        }
Beispiel #25
0
 public void RaiseIfDictionaryExpressionWithMissingPeriod()
 {
     Parser parser = new Parser("{ 'firstname' 'spam', 'lastname': 'bar' }");
     parser.CompileExpression();
 }
Beispiel #26
0
        public void CompileNegateExpression()
        {
            Parser parser = new Parser("-3");

            var expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(NegateExpression));
        }
Beispiel #27
0
        public void CompileFalseAsBooleanConstantExpression()
        {
            Parser parser = new Parser("False");
            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ConstantExpression));

            ConstantExpression cexpr = (ConstantExpression)expression;

            Assert.AreEqual(false, cexpr.Value);
        }
Beispiel #28
0
        public void CompileAndEvaluateListWithExpressionsExpression()
        {
            Parser parser = new Parser("[a+1, b+2, c]");
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("a", 1);
            environment.SetValue("b", 2);
            environment.SetValue("c", "spam");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ListExpression));
            Assert.IsFalse(((ListExpression)expression).IsReadOnly);

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IList));

            IList list = (IList)result;

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(2, list[0]);
            Assert.AreEqual(4, list[1]);
            Assert.AreEqual("spam", list[2]);
        }
Beispiel #29
0
        public void CompileIndexedExpression()
        {
            Parser parser = new Parser("'spam'[1]");
            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(IndexedExpression));

            IndexedExpression iexpr = (IndexedExpression)expression;

            Assert.AreEqual("spam", iexpr.TargetExpression.Evaluate(null));
            Assert.AreEqual(1, iexpr.IndexExpression.Evaluate(null));
        }
Beispiel #30
0
        private static object CompileAndEvaluateExpression(string text)
        {
            Machine machine = new Machine();
            Parser parser = new Parser(text);

            IExpression expression = parser.CompileExpression();

            Assert.IsNull(parser.CompileExpression());

            return expression.Evaluate(machine.Environment);
        }