Example #1
0
        public void EvaluateCallObject()
        {
            Machine machine = new Machine();
            ICommand body = new ReturnCommand(new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("x"), new DotExpression(new VariableExpression("this"), "Age")));
            Function function = new Function(new string[] { "x" }, body);
            DynamicObject person = new DynamicObject();
            person.SetValue("Age", 30);

            Assert.AreEqual(40, function.Call(person, new object[] { 10 }));
        }
Example #2
0
        public void InvokeSimpleFunction()
        {
            ICommand body = new ReturnCommand(new VariableExpression("x"));
            Function function = new Function(new string[] { "x" }, body);

            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("x", 2);

            object result = function.Invoke(environment, new object[] { 1 });

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
        }
Example #3
0
        private static ICallable BuildFactorialFunction()
        {
            IExpression condition = new CompareExpression(ComparisonOperator.LessEqual, new VariableExpression("n"), new ConstantExpression(1));

            ICommand return1 = new ReturnCommand(new ConstantExpression(1));
            ICommand return2 = new ReturnCommand(new ArithmeticBinaryExpression(
                ArithmeticOperator.Multiply,
                new VariableExpression("n"),
                new InvokeExpression("Factorial", new IExpression[] { new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("n"), new ConstantExpression(1)) })));

            ICommand ifcmd = new IfCommand(condition, return1, return2);
            ICallable factorial = new Function(new string[] { "n" }, ifcmd);

            return factorial;
        }
Example #4
0
        public void DefineMethod()
        {
            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(null, body);

            Assert.AreEqual(0, function.Arity);

            this.dynobj.SetValue("GetName", function);

            object result = this.dynobj.GetValue("GetName");

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ICallable));
            Assert.IsTrue(result == function);
        }
Example #5
0
        public void InvokeMethod()
        {
            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(null, body);

            Assert.AreEqual(0, function.Arity);

            this.dynobj.SetValue("Name", "Adam");
            this.dynobj.SetValue("GetName", function);

            object result = this.dynobj.Invoke("GetName", new object[] { });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(string));
            Assert.AreEqual("Adam", result);
        }
Example #6
0
        public void InvokeDefaultMethod()
        {
            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(new string[] { "name", "parameters" }, body, null, true, false);

            this.dynclass.SetMember("Name", "Adam");
            this.dynclass.SetMember("Age", 800);
            this.dynclass.SetMember("DefaultMethod", function);

            Assert.AreEqual(2, function.Arity);
            Assert.IsTrue(function.IsDefault);

            Assert.IsNotNull(this.dynclass.DefaultMember);
            Assert.AreEqual(this.dynclass.DefaultMember, function);

            object instance = this.dynclass.NewInstance(null);

            Assert.IsNotNull(instance);

            Assert.IsInstanceOfType(instance, typeof(IObject));

            IObject dynobj = (IObject)instance;

            object result = dynobj.Invoke("AnyName", new object[] { });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(string));
            Assert.AreEqual("Adam", result);
        }
Example #7
0
        public void InvokeMethodRedefinedInObject()
        {
            ICommand body = new ReturnCommand(new VariableExpression("Age"));
            Function function = new Function(null, body);

            ICommand body2 = new ReturnCommand(new ConstantExpression(0));
            Function function2 = new Function(null, body2);

            this.dynclass.SetMember("Name", "Adam");
            this.dynclass.SetMember("Age", 800);
            this.dynclass.SetMember("GetAge", function);

            Assert.AreEqual(0, function.Arity);

            object instance = this.dynclass.NewInstance(null);

            Assert.IsNotNull(instance);

            Assert.IsInstanceOfType(instance, typeof(IObject));

            IObject dynobj = (IObject)instance;

            dynobj.SetValue("GetAge", function2);

            object result = dynobj.Invoke("GetAge", new object[] { });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(int));
            Assert.AreEqual(0, result);
        }
Example #8
0
        public void EvaluateInvokeExpression()
        {
            ICommand body = new ReturnCommand(new VariableExpression("x"));
            Function function = new Function(new string[] { "x" }, body);

            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("foo", function);

            IExpression expression = new InvokeExpression("foo", new IExpression[] { new ConstantExpression(1) });

            object result = expression.Evaluate(environment);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result);
        }