Ejemplo n.º 1
0
        public void CreateAndEvaluateInteger()
        {
            ConstantExpression expr = new ConstantExpression(1);

            Assert.AreEqual(1, expr.Evaluate(null));
            Assert.AreEqual(1, expr.Value);
        }
Ejemplo n.º 2
0
 public void ExecuteConstantExpression()
 {
     ConstantExpression expr = new ConstantExpression(1);
     ExpressionCommand command = new ExpressionCommand(expr);
     Assert.AreEqual(1, command.Execute(null));
     Assert.AreEqual(expr, command.Expression);
 }
        public void CreateUnaryExpression()
        {
            IExpression valueExpression = new ConstantExpression(1);
            UnaryExpression expression = new ArithmeticUnaryExpression(ArithmeticOperator.Minus, valueExpression);

            Assert.IsTrue(expression.Expression == valueExpression);
        }
        public void CreateBinaryExpression()
        {
            IExpression leftExpression = new ConstantExpression(1);
            IExpression rightExpression = new ConstantExpression(2);
            BinaryExpression expression = new ArithmeticBinaryExpression(ArithmeticOperator.Add, leftExpression, rightExpression);

            Assert.IsTrue(expression.LeftExpression == leftExpression);
            Assert.IsTrue(expression.RightExpression == rightExpression);
        }
Ejemplo n.º 5
0
 public void ExecuteConstantExpression()
 {
     ConstantExpression expr = new ConstantExpression(1);
     ReturnCommand command = new ReturnCommand(expr);
     Context context = new Context();
     Assert.AreEqual(1, command.Execute(context));
     Assert.IsNotNull(context.ReturnValue);
     Assert.AreEqual(1, context.ReturnValue.Value);
     Assert.AreEqual(expr, command.Expression);
 }
Ejemplo n.º 6
0
        public void SetVariable()
        {
            Context context = new Context();
            ConstantExpression expr = new ConstantExpression(1);
            SetVariableCommand command = new SetVariableCommand("One", expr);

            object result = command.Execute(context);

            Assert.AreEqual(1, result);
            Assert.AreEqual(1, context.GetValue("One"));
            Assert.AreEqual(expr, command.Expression);
            Assert.AreEqual("One", command.Name);
        }
Ejemplo n.º 7
0
        private IExpression ParseStringInterpolation(string text)
        {
            IList<IExpression> expressions = new List<IExpression>();

            while (true)
            {
                int pos = text.IndexOf('$');

                if (pos < 0)
                {
                    if (!string.IsNullOrEmpty(text))
                    {
                        IExpression textexpr = new ConstantExpression(text);
                        expressions.Add(textexpr);
                    }

                    break;
                }

                if (pos == text.Length - 1)
                    throw new ParserException("Unexpected End of String");

                string left = text.Substring(0, pos);

                if (!string.IsNullOrEmpty(left))
                    expressions.Add(new ConstantExpression(left));

                if (text[pos + 1] == '{')
                {
                    int pos2 = text.IndexOf('}', pos + 1);

                    if (pos2 < 0)
                        throw new ParserException("Unexpected End of String");

                    string subtext = text.Substring(pos + 2, pos2 - pos - 2);
                    text = text.Substring(pos2 + 1);

                    Parser parser = new Parser(subtext);

                    IExpression newexpr = parser.ParseExpression();

                    if (parser.ParseExpression() != null)
                        throw new ParserException("Bad String Interpolation");

                    expressions.Add(newexpr);
                }
                else if (char.IsLetter(text[pos + 1]))
                {
                    Parser parser = new Parser(text.Substring(pos + 1));
                    string name = parser.ParseName();
                    IExpression varexpr = new VariableExpression(name);
                    expressions.Add(varexpr);
                    text = text.Substring(pos + name.Length + 1);
                }
                else
                    throw new ParserException("Bad String Interpolation");
            }

            if (expressions.Count == 1)
                return expressions[0];

            IExpression expression = expressions[0];

            foreach (var expr in expressions.Skip(1))
                expression = new ArithmeticBinaryExpression(ArithmeticOperator.Add, expression, expr);

            return expression;
        }