AjErl.Forms.FunctionForm.Evaluate C# (CSharp) Method

Evaluate() public method

public Evaluate ( Context context ) : object
context Context
return object
        public object Evaluate(Context context)
        {
            Context newcontext = new Context();
            IList<object> parameters = new List<object>();

            foreach (var pexpr in this.parameterexpressions)
                parameters.Add(pexpr.Evaluate(newcontext, true));

            var func = new Function(context, parameters, this.body);

            context.SetValue(string.Format("{0}/{1}", this.name, parameters.Count), func);

            return func;
        }

Usage Example

Ejemplo n.º 1
0
        public void DefineAndEvaluateFunction()
        {
            FunctionForm form = new FunctionForm("add", new IExpression[] { new VariableExpression(new Variable("X")), new VariableExpression(new Variable("Y")) }, new AddExpression(new VariableExpression(new Variable("X")), new VariableExpression(new Variable("Y"))));
            Context context = new Context();

            var result = form.Evaluate(context);

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

            var func = (Function)result;

            Assert.AreSame(func, context.GetValue("add/2"));

            var newcontext = func.MakeContext(new object[] { 1, 2 });

            Assert.IsNotNull(newcontext);
            Assert.AreEqual(1, newcontext.GetValue("X"));
            Assert.AreEqual(2, newcontext.GetValue("Y"));

            Assert.AreSame(func, newcontext.GetValue("add/2"));

            Assert.AreEqual(3, func.Evaluate(newcontext));
        }