public void FunctionAsString()
        {
            DefinedFunction function = new DefinedFunction("foo", null, null, null);

            Assert.AreEqual("<function foo>", ValueUtilities.AsString(function));
            Assert.AreEqual("<function foo>", ValueUtilities.AsPrintString(function));
        }
Example #2
0
        public void CreateAndExecuteDefineFunctionCommand()
        {
            IEnumerable <ICommand> commands = new ICommand[] {
                new SetVariableCommand("a", new ConstantExpression(1)),
                new SetVariableCommand("b", new ConstantExpression(2))
            };

            CompositeCommand      body    = new CompositeCommand(commands);
            DefineFunctionCommand command = new DefineFunctionCommand("foo", null, body);

            Context context = new Context();

            Assert.IsNull(command.Execute(context));
            Assert.AreEqual("foo", command.Name);
            Assert.IsNull(command.ArgumentNames);
            Assert.AreEqual(body, command.Command);

            var result = context.GetValue("foo");

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

            DefinedFunction dfunc = (DefinedFunction)result;

            Assert.AreEqual(2, dfunc.Call(context, null));
        }
        public void GetAndUseDelegate()
        {
            ICommand body = new PassCommand();

            DefinedFunction func = new DefinedFunction("foo", null, body, null);
            var             type = typeof(ThreadStart);
            //Activator.CreateInstance(typeof(ThreadStart), func.DoFunction);
        }
        public void GetMethodFromSingletonClass()
        {
            DynamicObject obj    = new DynamicObject(this.@class);
            var           newfoo = new DefinedFunction(null, null, null);

            obj.SingletonClass.SetInstanceMethod("foo", newfoo);
            Assert.AreSame(newfoo, obj.GetMethod("foo"));
        }
        public void CreateSimpleDefinedFunction()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            ICommand          body       = new SetCommand("c", new NameExpression("a"));
            DefinedFunction   func       = new DefinedFunction("foo", parameters, body, null);

            Assert.AreEqual(parameters, func.Parameters);
            Assert.AreEqual(body, func.Body);
        }
        public void EvaluateUsingDefaultValuesForArguments()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
            ICommand          body       = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            Assert.AreEqual(3, func.Apply(new BindingEnvironment(), null, null));
        }
        public void RetrieveDefinedInstanceMethodIsNull()
        {
            DynamicClass dclass = new DynamicClass("Dog");
            IFunction    foo    = new DefinedFunction(null, null, null);

            dclass.SetInstanceMethod("foo", foo);

            Assert.AreSame(foo, dclass.GetInstanceMethod("foo"));
        }
Example #8
0
 public override void Execute(Context context)
 {
     if (IsFunction)
     {
         context[Name] = new DefinedFunction(Name, Expression, Arguments);
     }
     else
     {
         context[Name] = Expression;
     }
 }
        public void DefineAndExecuteFunctionWithParameters()
        {
            Context context = new Context();

            DefinedFunction function = new DefinedFunction(new AddExpression(new NameExpression("a"), new NameExpression("b")), new string[] { "a", "b" }, context);

            var result = function.Apply(null, null, new object[] { 1, 2 });

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result);
        }
Example #10
0
        public void EvaluateMethodCall()
        {
            DefinedClass    klass    = new DefinedClass("Spam");
            DefinedFunction function = new DefinedFunction("get", new Parameter[] { new Parameter("self", null, false), new Parameter("a", null, false) }, new ReturnCommand(new NameExpression("a")), null);

            klass.SetMethod(function.Name, function);
            DynamicObject foo = (DynamicObject)klass.Apply(this.machine.Environment, null, null);

            this.machine.Environment.SetValue("foo", foo);
            Assert.AreEqual(2, this.Evaluate("foo.get(2)"));
        }
        public void ValuesInFunction()
        {
            ICommand body = new PassCommand();

            DefinedFunction func = new DefinedFunction("foo", null, body, null);

            func.SetValue("__doc__", "foo function");

            Assert.IsFalse(func.HasValue("bar"));
            Assert.IsTrue(func.HasValue("__doc__"));
            Assert.AreEqual("foo function", func.GetValue("__doc__"));
        }
        public void DefineAndExecuteSimplePuts()
        {
            Machine      machine = new Machine();
            StringWriter writer  = new StringWriter();
            PutsFunction puts    = new PutsFunction(writer);

            machine.RootContext.Self.Class.SetInstanceMethod("puts", puts);

            DefinedFunction function = new DefinedFunction(new CallExpression("puts", new IExpression[] { new ConstantExpression(123) }), new string[] { }, machine.RootContext);

            Assert.IsNull(function.Apply(machine.RootContext.Self, machine.RootContext, new object[] { }));
            Assert.AreEqual("123\r\n", writer.ToString());
        }
        public void GetOwnInstanceMethodNames()
        {
            DynamicClass dclass = new DynamicClass("Dog");
            IFunction    foo    = new DefinedFunction(null, null, null);

            dclass.SetInstanceMethod("foo", foo);

            var result = dclass.GetOwnInstanceMethodNames();

            Assert.IsNotNull(result);
            Assert.AreNotEqual(0, result.Count);
            Assert.IsTrue(result.Contains("foo"));
        }
        public void CreateInstance()
        {
            DynamicClass dclass = new DynamicClass("Dog");
            IFunction    foo    = new DefinedFunction(null, null, null);

            dclass.SetInstanceMethod("foo", foo);

            var result = dclass.CreateInstance();

            Assert.IsNotNull(result);
            Assert.AreSame(dclass, result.Class);
            Assert.AreSame(foo, result.GetMethod("foo"));
        }
        public void EvaluateUsingTwoNamedArguments()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
            ICommand          body       = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            var result = func.Apply(new BindingEnvironment(), null, new Dictionary <string, object> {
                { "a", 2 }, { "b", 3 }
            });

            Assert.IsNotNull(result);
            Assert.AreEqual(5, result);
        }
        public void EvaluateUsingEmptyListArgument()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, true) };
            ICommand          body       = new ReturnCommand(new NameExpression("b"));

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            var result = func.Apply(new BindingEnvironment(), new object[] { 1 }, null);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IList <object>));

            var list = (IList <object>)result;

            Assert.AreEqual(0, list.Count);
        }
Example #17
0
        public void EvaluateFunctionExpression()
        {
            IList <string> arguments = new string[] { "a", "b" };
            IExpression    body      = new CompositeExpression(new IExpression[] { });

            FunctionExpression expression = new FunctionExpression(arguments, body);

            var result = expression.Evaluate(null);

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

            DefinedFunction dfunc = (DefinedFunction)result;

            Assert.AreSame(arguments, dfunc.Arguments);
            Assert.AreSame(body, dfunc.Expression);
        }
Example #18
0
        public void EvaluateUsingArgumentAndOriginalContext()
        {
            Context context = new Context();

            context.SetValue("return", new Return());
            IList <string> arguments = new string[] { "a" };
            IExpression    body      = new CallExpression(new NameExpression("return"), new IExpression[] { new NameExpression("a") }, null);

            DefinedFunction dfunc = new DefinedFunction(context, arguments, body);

            var result = dfunc.Apply(null, new object[] { 42 }, null);

            Assert.IsNotNull(result);
            Assert.AreEqual(42, result);

            Assert.AreSame(context, dfunc.Context);
        }
        public void ExecuteFunctionWithPrint()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            CompositeCommand  body       = new CompositeCommand();

            body.AddCommand(new ExpressionCommand(new CallExpression(new NameExpression("print"), new IExpression[] { new NameExpression("a") })));
            body.AddCommand(new ExpressionCommand(new CallExpression(new NameExpression("print"), new IExpression[] { new NameExpression("b") })));

            Machine      machine = new Machine();
            StringWriter writer  = new StringWriter();

            machine.Output = writer;

            DefinedFunction func = new DefinedFunction("foo", parameters, body, machine.Environment);

            func.Apply(machine.Environment, new object[] { 1, 2 }, null);
            Assert.AreEqual("1\r\n2\r\n", writer.ToString());
        }
        public void ApplyNewMethodCallingInitialize()
        {
            Machine      machine    = new Machine();
            DynamicClass @class     = new DynamicClass((DynamicClass)machine.RootContext.GetLocalValue("Class"), "Dog", (DynamicClass)machine.RootContext.GetLocalValue("Object"));
            IFunction    initialize = new DefinedFunction(new AssignInstanceVarExpression("age", new ConstantExpression(10)), new string[0], null);

            @class.SetInstanceMethod("initialize", initialize);

            var result = @class.GetMethod("new").Apply(@class, @class.Constants, new object[] { });

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

            var obj = (DynamicObject)result;

            Assert.AreSame(@class, obj.Class);
            Assert.AreEqual(10, obj.GetValue("age"));
        }
Example #21
0
        public void Execute(IContext context)
        {
            IList <Parameter> parameters = null;

            if (this.parameterExpressions != null && this.parameterExpressions.Count > 0)
            {
                parameters = new List <Parameter>();
                foreach (var parexpr in this.parameterExpressions)
                {
                    parameters.Add((Parameter)parexpr.Evaluate(context));
                }
            }

            DefinedFunction function = new DefinedFunction(this.name, parameters, this.body, context);

            function.SetValue("__doc__", this.doc);

            context.SetValue(this.name, function);
        }
        public void ExecuteFunctionWithReturn()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            CompositeCommand  body       = new CompositeCommand();

            body.AddCommand(new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add)));

            Machine      machine = new Machine();
            StringWriter writer  = new StringWriter();

            machine.Output = writer;

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            var result = func.Apply(machine.Environment, new object[] { 1, 2 }, null);

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result);
        }
        public void RaiseWhenOneParameterExpectedAndNoneIsProvided()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", null, false) };
            CompositeCommand  body       = new CompositeCommand();

            Machine machine = new Machine();

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            try
            {
                func.Apply(machine.Environment, null, null);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("foo() takes exactly 1 positional argument (0 given)", ex.Message);
            }
        }
Example #24
0
        public void ExecuteAssignDotCommand()
        {
            Machine machine = new Machine();
            var     @class  = new DynamicClass("Dog");
            var     method  = new DefinedFunction((new Parser("@name = name")).ParseCommand(), new string[] { "name" }, machine.RootContext);

            @class.SetInstanceMethod("name=", method);
            var nero = @class.CreateInstance();

            machine.RootContext.SetLocalValue("nero", nero);
            var leftvalue            = (DotExpression)(new Parser("nero.name")).ParseExpression();
            var value                = new ConstantExpression("Nero");
            AssignDotExpressions cmd = new AssignDotExpressions(leftvalue, value);

            var result = cmd.Evaluate(machine.RootContext);

            Assert.IsNotNull(result);
            Assert.AreEqual("Nero", result);
            Assert.AreEqual("Nero", nero.GetValue("name"));
        }
        public void RaiseWhenMultipleValuesForKeywordArgument()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
            ICommand          body       = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            try
            {
                func.Apply(new BindingEnvironment(), new object[] { 1 }, new Dictionary <string, object> {
                    { "a", 2 }
                });
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("foo() got multiple values for keyword argument 'a'", ex.Message);
            }
        }
        public void CreateInstanceWithConstructor()
        {
            DefinedClass    klass       = new DefinedClass("Spam");
            ICommand        body        = new SetAttributeCommand(new NameExpression("self"), "name", new NameExpression("name"));
            DefinedFunction constructor = new DefinedFunction("__init__", new Parameter[] { new Parameter("self", null, false), new Parameter("name", null, false) }, body, null);

            klass.SetMethod(constructor.Name, constructor);
            var result = klass.Apply(null, new object[] { "Adam" }, null);

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

            var dynobj = (DynamicObject)result;

            Assert.AreEqual(klass, dynobj.Class);
            Assert.IsTrue(dynobj.HasValue("name"));
            var name = dynobj.GetValue("name");

            Assert.IsNotNull(name);
            Assert.AreEqual("Adam", name);
        }
        public void RaiseWhenFewerThanExpectedParametersProvided()
        {
            IList <Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", new ConstantExpression(1), false) };
            CompositeCommand  body       = new CompositeCommand();

            Machine      machine = new Machine();
            StringWriter writer  = new StringWriter();

            machine.Output = writer;

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            try
            {
                func.Apply(machine.Environment, new object[] { }, null);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("foo() takes at least 1 positional argument (0 given)", ex.Message);
            }
        }
Example #28
0
        public override object Evaluate(Context context)
        {
            var result = new DefinedFunction(this.expression, this.parameters, context);

            if (this.namedexpression.TargetExpression == null)
            {
                if (context.Module != null)
                {
                    context.Module.SetInstanceMethod(this.namedexpression.Name, result);
                }
                else
                {
                    context.Self.Class.SetInstanceMethod(this.namedexpression.Name, result);
                }
            }
            else
            {
                var target = (DynamicObject)this.namedexpression.TargetExpression.Evaluate(context);
                target.SingletonClass.SetInstanceMethod(this.namedexpression.Name, result);
            }

            return(null);
        }
Example #29
0
 public LivelinessTable(DefinedFunction function)
 {
     this.function = function;
     FindSymbols();
     CreateBlocks();
 }
Example #30
0
 public void Convert(DefinedFunction x)
 {
     // TODO:
 }
Example #31
0
        public void SetMacroOnDefinedFunction()
        {
            Machine machine = new Machine();
            machine.CreateNamespace("ns");

            Variable variable = Variable.Intern(machine, "ns/func");

            Parser parser = new Parser("[x y] (list x y) 1 2");
            object argumentNames = parser.ParseForm();
            object body = parser.ParseForm();

            DefinedFunction func = new DefinedFunction("simple-list", (ICollection)argumentNames, Utilities.ToExpression(body));

            machine.SetVariableValue(variable, func);

            variable.SetMacro(machine);

            object result = machine.GetVariableValue(variable);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DefinedMacro));
        }
Example #32
0
 static void ExpandInline(CatExpr fxns, DefinedFunction d, int nMaxDepth)
 {
     foreach (Function f in d.GetSubFxns())
         ExpandInline(fxns, f, nMaxDepth - 1);
 }