Пример #1
0
        public void ParseAddMultiplyWithParens()
        {
            Parser parser = new Parser("(2+3)*4.");

            IExpression expression = parser.ParseExpression();

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

            MultiplyExpression multexpression = (MultiplyExpression)expression;

            Assert.IsInstanceOfType(multexpression.RightExpression, typeof(ConstantExpression));
            Assert.AreEqual(4, ((ConstantExpression)multexpression.RightExpression).Value);

            Assert.IsInstanceOfType(multexpression.LeftExpression, typeof(AddExpression));

            AddExpression addexpression = (AddExpression)multexpression.LeftExpression;

            Assert.IsInstanceOfType(addexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(addexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(2, ((ConstantExpression)addexpression.LeftExpression).Value);
            Assert.AreEqual(3, ((ConstantExpression)addexpression.RightExpression).Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #2
0
        public void ParseAtom()
        {
            Parser parser = new Parser("ok.");

            IExpression expression = parser.ParseExpression();

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

            AtomExpression atomexpression = (AtomExpression)expression;
            Assert.AreEqual("ok", atomexpression.Atom.Name);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #3
0
        public void ParseSimpleQualifiedCallExpression()
        {
            Parser parser = new Parser("mod:add(1, 2).");

            var result = parser.ParseExpression();

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

            var expr = (QualifiedCallExpression)result;

            Assert.IsInstanceOfType(expr.ModuleExpression, typeof(AtomExpression));
            Assert.AreEqual(((AtomExpression)expr.ModuleExpression).Atom.Name, "mod");

            Assert.IsInstanceOfType(expr.NameExpression, typeof(AtomExpression));
            Assert.AreEqual(((AtomExpression)expr.NameExpression).Atom.Name, "add");

            Assert.IsNotNull(expr.ArgumentExpressions);
            Assert.AreEqual(2, expr.ArgumentExpressions.Count);
            Assert.IsInstanceOfType(expr.ArgumentExpressions[0], typeof(ConstantExpression));
            Assert.IsInstanceOfType(expr.ArgumentExpressions[1], typeof(ConstantExpression));

            var cexpr = (ConstantExpression)expr.ArgumentExpressions[0];
            Assert.AreEqual(1, cexpr.Value);

            cexpr = (ConstantExpression)expr.ArgumentExpressions[1];
            Assert.AreEqual(2, cexpr.Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #4
0
 private FunExpression MakeExpression(string text)
 {
     Context context = new Context();
     Parser parser = new Parser(text);
     return (FunExpression)parser.ParseExpression();
 }
Пример #5
0
        public void ParseSimpleMultiply()
        {
            Parser parser = new Parser("10*20.");

            IExpression expression = parser.ParseExpression();

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

            MultiplyExpression multiplyexpression = (MultiplyExpression)expression;

            Assert.IsInstanceOfType(multiplyexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(multiplyexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(10, ((ConstantExpression)multiplyexpression.LeftExpression).Value);
            Assert.AreEqual(20, ((ConstantExpression)multiplyexpression.RightExpression).Value);
        }
Пример #6
0
        public void ParseCompositeExpressionWithTwoConstants()
        {
            Parser parser = new Parser("1,2.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(CompositeExpression));

            var cexpr = (CompositeExpression)expr;

            Assert.IsNotNull(cexpr.Expressions);
            Assert.AreEqual(2, cexpr.Expressions.Count);
            Assert.IsInstanceOfType(cexpr.Expressions[0], typeof(ConstantExpression));
            Assert.IsInstanceOfType(cexpr.Expressions[1], typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)cexpr.Expressions[0]).Value);
            Assert.AreEqual(2, ((ConstantExpression)cexpr.Expressions[1]).Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #7
0
        public void ThrowIfNoPoint()
        {
            Parser parser = new Parser("1");

            try
            {
                parser.ParseExpression();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Expected '.'", ex.Message);
            }
        }
Пример #8
0
        public void ParseStrictEqualExpression()
        {
            Parser parser = new Parser("1 =:= 0.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(StrictEqualExpression));

            var seqexpr = (StrictEqualExpression)expr;

            Assert.IsInstanceOfType(seqexpr.LeftExpression, typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)seqexpr.LeftExpression).Value);
            Assert.IsInstanceOfType(seqexpr.RightExpression, typeof(ConstantExpression));
            Assert.AreEqual(0, ((ConstantExpression)seqexpr.RightExpression).Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #9
0
        public void ParseFunExpression()
        {
            Parser parser = new Parser("fun(X,Y) -> X+Y end.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(FunExpression));

            var fexpr = (FunExpression)expr;

            Assert.AreEqual(2, fexpr.ParameterExpressions.Count);
            Assert.IsInstanceOfType(fexpr.ParameterExpressions[0], typeof(VariableExpression));
            Assert.AreEqual("X", ((VariableExpression)fexpr.ParameterExpressions[0]).Variable.Name);
            Assert.IsInstanceOfType(fexpr.ParameterExpressions[1], typeof(VariableExpression));
            Assert.AreEqual("Y", ((VariableExpression)fexpr.ParameterExpressions[1]).Variable.Name);

            Assert.IsNotNull(fexpr.Body);
            Assert.IsInstanceOfType(fexpr.Body, typeof(AddExpression));

            Assert.IsNull(parser.ParseExpression());
        }
Пример #10
0
        public void ParseReal()
        {
            Parser parser = new Parser("123.45.");

            IExpression expression = parser.ParseExpression();

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

            ConstantExpression consexpression = (ConstantExpression)expression;
            Assert.IsNotNull(consexpression.Value);
            Assert.AreEqual(123.45, consexpression.Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #11
0
        public void ParseReceiveExpression()
        {
            Parser parser = new Parser("receive {Sender, ping} -> io:write(\"ping received\"), Sender ! pong; {Sender, pong} -> io:write(\"pong received\"), Sender ! ping end.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(ReceiveExpression));

            var rexpr = (ReceiveExpression)expr;

            Assert.IsNotNull(rexpr.Matches);
            Assert.AreEqual(2, rexpr.Matches.Count);

            Assert.IsInstanceOfType(rexpr.Matches[0].Head, typeof(Tuple));
            Assert.IsInstanceOfType(rexpr.Matches[1].Head, typeof(Tuple));
            Assert.IsInstanceOfType(rexpr.Matches[0].Body, typeof(CompositeExpression));
            Assert.IsInstanceOfType(rexpr.Matches[1].Body, typeof(CompositeExpression));

            Assert.IsNull(parser.ParseExpression());
        }
Пример #12
0
        public void ParseMatchVariableWithInteger()
        {
            Parser parser = new Parser("X=1.");

            IExpression expression = parser.ParseExpression();

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

            MatchExpression matchexpression = (MatchExpression)expression;
            Assert.IsNotNull(matchexpression.LeftExpression);
            Assert.IsInstanceOfType(matchexpression.LeftExpression, typeof(VariableExpression));
            Assert.IsNotNull(matchexpression.RightExpression);
            Assert.IsInstanceOfType(matchexpression.RightExpression, typeof(ConstantExpression));

            Assert.IsNull(parser.ParseExpression());
        }
Пример #13
0
        public void ParseMultiFunExpression()
        {
            Parser parser = new Parser("fun({c,C}) -> {f, 32 + C*9/5}; ({f,F}) -> {c, (F-32)*5/9} end.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(MultiFunExpression));

            var mfexpr = (MultiFunExpression)expr;

            Assert.AreEqual(2, mfexpr.Expressions.Count);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #14
0
        public void ParseList()
        {
            Parser parser = new Parser("[1,2,3].");

            IExpression expression = parser.ParseExpression();

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

            ListExpression listexpression = (ListExpression)expression;
            Assert.IsNotNull(listexpression.Expressions);
            Assert.AreEqual(3, listexpression.Expressions.Count);

            foreach (var expr in listexpression.Expressions)
                Assert.IsInstanceOfType(expr, typeof(ConstantExpression));

            Assert.IsNull(parser.ParseExpression());
        }
Пример #15
0
        public void ParseFunWithDelayedCall()
        {
            Parser parser = new Parser("fun(X,Y) -> f(X-1, Y+1) end.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(FunExpression));

            var fexpr = (FunExpression)expr;

            Assert.IsInstanceOfType(fexpr.Body, typeof(DelayedCallExpression));
        }
Пример #16
0
        public void ParseFunWithCompositeBodyAndDelayedCall()
        {
            Parser parser = new Parser("fun(X,Y) -> Z=X-1, W=Y+1, f(Z, W) end.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(FunExpression));

            var fexpr = (FunExpression)expr;

            Assert.IsInstanceOfType(fexpr.Body, typeof(CompositeExpression));

            var cexpr = (CompositeExpression)fexpr.Body;

            Assert.AreEqual(3, cexpr.Expressions.Count);
            Assert.IsInstanceOfType(cexpr.Expressions[2], typeof(DelayedCallExpression));
        }
Пример #17
0
        public void ParseSimpleRem()
        {
            Parser parser = new Parser("10 rem 20.");

            IExpression expression = parser.ParseExpression();

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

            RemExpression remexpression = (RemExpression)expression;

            Assert.IsInstanceOfType(remexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(remexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(10, ((ConstantExpression)remexpression.LeftExpression).Value);
            Assert.AreEqual(20, ((ConstantExpression)remexpression.RightExpression).Value);
        }
Пример #18
0
        public void ParseString()
        {
            Parser parser = new Parser("\"foo\".");

            IExpression expression = parser.ParseExpression();

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

            ConstantExpression consexpression = (ConstantExpression)expression;
            Assert.IsNotNull(consexpression.Value);
            Assert.AreEqual("foo", consexpression.Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #19
0
        public void ParseSimpleSubtract()
        {
            Parser parser = new Parser("10-20.");

            IExpression expression = parser.ParseExpression();

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

            SubtractExpression subtractexpression = (SubtractExpression)expression;

            Assert.IsInstanceOfType(subtractexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(subtractexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(10, ((ConstantExpression)subtractexpression.LeftExpression).Value);
            Assert.AreEqual(20, ((ConstantExpression)subtractexpression.RightExpression).Value);
        }
Пример #20
0
        public void ParseUnaryTuple()
        {
            Parser parser = new Parser("{1}.");

            IExpression expression = parser.ParseExpression();

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

            TupleExpression tupleexpression = (TupleExpression)expression;
            Assert.IsNotNull(tupleexpression.Expressions);
            Assert.AreEqual(1, tupleexpression.Expressions.Count);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #21
0
        public void ParseSendExpression()
        {
            Parser parser = new Parser("X ! 1.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(SendExpression));

            var sexpr = (SendExpression)expr;

            Assert.IsInstanceOfType(sexpr.ProcessExpression, typeof(VariableExpression));
            Assert.AreEqual("X", ((VariableExpression)sexpr.ProcessExpression).Variable.Name);
            Assert.IsInstanceOfType(sexpr.MessageExpression, typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)sexpr.MessageExpression).Value);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #22
0
        public void ParseCompositeExpressionWithTwoVariables()
        {
            Parser parser = new Parser("X,Y.");

            var expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(CompositeExpression));

            var cexpr = (CompositeExpression)expr;

            Assert.IsNotNull(cexpr.Expressions);
            Assert.AreEqual(2, cexpr.Expressions.Count);
            Assert.IsInstanceOfType(cexpr.Expressions[0], typeof(VariableExpression));
            Assert.IsInstanceOfType(cexpr.Expressions[1], typeof(VariableExpression));
            Assert.AreEqual("X", ((VariableExpression)cexpr.Expressions[0]).Variable.Name);
            Assert.AreEqual("Y", ((VariableExpression)cexpr.Expressions[1]).Variable.Name);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #23
0
        public void ParseTuple()
        {
            Parser parser = new Parser("{1,2,3}.");

            IExpression expression = parser.ParseExpression();

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

            TupleExpression tupleexpression = (TupleExpression)expression;
            Assert.IsNotNull(tupleexpression.Expressions);
            Assert.AreEqual(3, tupleexpression.Expressions.Count);

            foreach (var expr in tupleexpression.Expressions)
                Assert.IsInstanceOfType(expr, typeof(ConstantExpression));

            Assert.IsNull(parser.ParseExpression());
        }
Пример #24
0
        public void ParseSimpleAdd()
        {
            Parser parser = new Parser("10+20.");

            IExpression expression = parser.ParseExpression();

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

            AddExpression addexpression = (AddExpression)expression;

            Assert.IsInstanceOfType(addexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(addexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(10, ((ConstantExpression)addexpression.LeftExpression).Value);
            Assert.AreEqual(20, ((ConstantExpression)addexpression.RightExpression).Value);
        }
Пример #25
0
        public void ParseVariable()
        {
            Parser parser = new Parser("X.");

            IExpression expression = parser.ParseExpression();

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

            VariableExpression varexpression = (VariableExpression)expression;
            Assert.AreEqual("X", varexpression.Variable.Name);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #26
0
        public void ParseSimpleDivide()
        {
            Parser parser = new Parser("10/20.");

            IExpression expression = parser.ParseExpression();

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

            DivideExpression divideexpression = (DivideExpression)expression;

            Assert.IsInstanceOfType(divideexpression.LeftExpression, typeof(ConstantExpression));
            Assert.IsInstanceOfType(divideexpression.RightExpression, typeof(ConstantExpression));

            Assert.AreEqual(10, ((ConstantExpression)divideexpression.LeftExpression).Value);
            Assert.AreEqual(20, ((ConstantExpression)divideexpression.RightExpression).Value);
        }
Пример #27
0
        public void ThrowIfListIsNotClosed()
        {
            Parser parser = new Parser("[1,2");

            try
            {
                parser.ParseExpression();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Expected ']'", ex.Message);
            }
        }
Пример #28
0
        public void ParseSimpleMatch()
        {
            Parser parser = new Parser("X=ok.");

            IExpression expression = parser.ParseExpression();

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

            MatchExpression matchexpression = (MatchExpression)expression;
            Assert.IsNotNull(matchexpression.LeftExpression);
            Assert.IsNotNull(matchexpression.RightExpression);

            Assert.IsNull(parser.ParseExpression());
        }
Пример #29
0
        public void ThrowIfTupleHasUnexpectedOperator()
        {
            Parser parser = new Parser("{1,2,=");

            try
            {
                parser.ParseExpression();
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Unexpected '='", ex.Message);
            }
        }
Пример #30
0
 private object EvaluateExpression(string text)
 {
     Parser parser = new Parser(text);
     IExpression expression = parser.ParseExpression();
     return expression.Evaluate(this.context);
 }