Beispiel #1
0
        public void EvaluateMultiFunctionWithTwoFunctions()
        {
            Function func1 = this.MakeFunction("f(0) -> 1.");
            Function func2 = this.MakeFunction("f(1) -> 2.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func1, func2 });

            Assert.AreEqual(2, mfunc.Apply(null, new object[] { 1 }));
        }
Beispiel #2
0
        public void EvaluateMultiFunctionWithOneFunction()
        {
            Function func = this.MakeFunction("f(0) -> 1.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func });

            Assert.IsNotNull(mfunc.Functions);
            Assert.AreEqual(1, mfunc.Functions.Count);

            Assert.AreEqual(1, mfunc.Apply(null, new object[] { 0 }));
        }
Beispiel #3
0
        public void RaiseIfNoClauseToMatch()
        {
            Function func1 = this.MakeFunction("f(0) -> 1.");
            Function func2 = this.MakeFunction("f(1) -> 2.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func1, func2 });

            try
            {
                mfunc.Apply(null, new object[] { 2 });
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("no function clause to match", ex.Message);
            }
        }
Beispiel #4
0
        public object Evaluate(Context context, bool withvars = false)
        {
            int arity = this.expressions[0].ParameterExpressions.Count;

            if (!this.expressions.Skip(1).All(f => f.ParameterExpressions.Count == arity))
                throw new Exception("head mismatch");

            IList<Function> functions = new List<Function>();

            foreach (var form in this.Expressions)
                functions.Add((Function)form.Evaluate(context, true));

            var func = new MultiFunction(functions);

            return func;
        }
Beispiel #5
0
        public object Evaluate(Context context)
        {
            string name = this.forms[0].Name;
            int arity = this.forms[0].ParameterExpressions.Count;

            if (!this.forms.Skip(1).All(f => f.Name == name && f.ParameterExpressions.Count == arity))
                throw new Exception("head mismatch");

            IList<Function> functions = new List<Function>();

            foreach (var form in this.Forms)
                functions.Add((Function)form.Evaluate(context));

            var func = new MultiFunction(functions);

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

            return func;
        }