public object Evaluate(IBindingEnvironment environment)
        {
            ICallable callable = (ICallable) environment.GetValue(this.name);

            if (callable == null)
                callable = (ICallable)Machine.Current.Environment.GetValue(this.name);

            List<object> parameters = new List<object>();

            foreach (IExpression expression in this.arguments)
            {
                object parameter = expression.Evaluate(environment);

                if (expression is VariableVariableExpression)
                {
                    if (parameter != null)
                        foreach (object obj in (IEnumerable)parameter)
                            parameters.Add(obj);
                }
                else
                    parameters.Add(parameter);
            }

            if (callable is ILocalCallable)
                return callable.Invoke(environment, parameters.ToArray());

            return callable.Invoke(parameters.ToArray());
        }
Beispiel #2
0
        public virtual object Invoke(string name, object[] parameters)
        {
            object value = this.GetValue(name);

            if (value == null && this.IsNativeMethod(name))
            {
                return(ObjectUtilities.GetNativeValue(this, name, parameters));
            }

            if (value == null)
            {
                throw new InvalidOperationException(string.Format("Unknown member '{0}'", name));
            }

            if (!(value is ICallable))
            {
                if (parameters == null)
                {
                    return(value);
                }

                throw new InvalidOperationException(string.Format("'{0}' is not a method", name));
            }

            ICallable method = (ICallable)value;

            IBindingEnvironment objenv = new ObjectEnvironment(this, method.Environment);

            return(method.Invoke(objenv, parameters));
        }
        public virtual object Invoke(string name, object[] parameters)
        {
            object value = this.GetValue(name);

            if ((value == null || value == Undefined.Instance) && this.IsNativeMethod(name))
            {
                return(ObjectUtilities.GetNativeValue(this, name, parameters));
            }

            if (value == null || value == Undefined.Instance)
            {
                throw new InvalidOperationException(string.Format("Unknown member '{0}'", name));
            }

            if (!(value is ICallable))
            {
                if (parameters == null)
                {
                    return(value);
                }

                throw new InvalidOperationException(string.Format("'{0}' is not a method", name));
            }

            ICallable method = (ICallable)value;

            return(method.Invoke(null, this, parameters));

            //TODO set up object environment if needed
            //IContext objenv = new ObjectEnvironment(this, method.Environment);

            //return method.Invoke(objenv, parameters);
        }
Beispiel #4
0
        public object Invoke(ICallable function, params object[] arguments)
        {
            Machine current = Machine.Current;

            try
            {
                this.machine.SetCurrent();
                return(function.Invoke(arguments));
            }
            finally
            {
                Machine.SetCurrent(current);
            }
        }
Beispiel #5
0
        public void EvaluateFactorialFunction()
        {
            Machine   machine   = new Machine();
            ICallable factorial = BuildFactorialFunction();

            machine.Environment.SetValue("Factorial", factorial);

            object result;

            result = factorial.Invoke(new object[] { 3 });

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

            result = factorial.Invoke(new object[] { 4 });

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

            result = factorial.Invoke(new object[] { 5 });

            Assert.IsNotNull(result);
            Assert.AreEqual(120, result);
        }
        public void EvaluateFactorialFunction()
        {
            Context   context   = new Context();;
            ICallable factorial = BuildFactorialFunction(context);

            context.SetValue("Factorial", factorial);

            object result;

            result = factorial.Invoke(context, null, new object[] { 3 });

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

            result = factorial.Invoke(context, null, new object[] { 4 });

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

            result = factorial.Invoke(context, null, new object[] { 5 });

            Assert.IsNotNull(result);
            Assert.AreEqual(120, result);
        }
        public override object Invoke(string name, object[] parameters)
        {
            object value = this.GetValue(name);

            if (value == null || !(value is ICallable))
            {
                value = this.objclass.GetMember(name);
            }

            if (value == null)
            {
                if (this.IsNativeMethod(name))
                {
                    return(ObjectUtilities.GetNativeValue(this, name, parameters));
                }

                if (this.objclass.DefaultMember != null)
                {
                    ICallable           defmethod = (ICallable)this.objclass.DefaultMember;
                    IBindingEnvironment oenv      = new ObjectEnvironment(this, defmethod.Environment);
                    object[]            arguments = new object[] { name, parameters };
                    return(defmethod.Invoke(oenv, arguments));
                }
                else
                {
                    return(base.Invoke(name, parameters));
                }
            }

            if (!(value is ICallable))
            {
                if (parameters == null)
                {
                    return(value);
                }

                throw new InvalidOperationException(string.Format("'{0}' is not a method", name));
            }

            ICallable method = (ICallable)value;

            IBindingEnvironment objenv = new ObjectEnvironment(this, method.Environment);

            return(method.Invoke(objenv, parameters));
        }
Beispiel #8
0
 public virtual object Invoke(ICallable method, object[] parameters)
 {
     return method.Invoke(null, this, parameters);
 }
Beispiel #9
0
        public virtual object Invoke(ICallable method, object[] parameters)
        {
            IBindingEnvironment objenv = new ObjectEnvironment(this, method.Environment);

            return(method.Invoke(objenv, parameters));
        }
Beispiel #10
0
 public object Invoke(ICallable function, params object[] arguments)
 {
     Machine current = Machine.Current;
     try
     {
         this.machine.SetCurrent();
         return function.Invoke(arguments);
     }
     finally
     {
         Machine.SetCurrent(current);
     }
 }
Beispiel #11
0
        public virtual object Invoke(ICallable method, object[] parameters)
        {
            IBindingEnvironment objenv = new ObjectEnvironment(this, method.Environment);

            return method.Invoke(objenv, parameters);
        }
Beispiel #12
0
 public virtual object Invoke(ICallable method, object[] parameters)
 {
     return(method.Invoke(null, this, parameters));
 }