private AphidObject CallFunctionCore(AphidFunction function, IEnumerable <AphidObject> parms)
        {
            var functionScope = new AphidScope(new AphidObject(), function.ParentScope);

            var i = 0;

            foreach (var arg in parms)
            {
                if (function.Args.Length == i)
                {
                    break;
                }

                functionScope.Variables.Add(function.Args[i++], arg);
            }

            var lastScope = _currentScope;

            _currentScope = functionScope;

            Interpret(function.Body);

            var retVal = GetReturnValue();

            _currentScope = lastScope;

            return(retVal);
        }
 public static void Lock(AphidInterpreter interpreter, AphidObject obj, AphidFunction body)
 {
     lock (obj)
     {
         interpreter.CallFunction(body);
     }
 }
        private static ManualResetEvent ThreadCore(AphidInterpreter interpreter, Action<Action> start, AphidFunction function, params object[] parms)
        {
            var reset = new ManualResetEvent(false);
            var interpreter2 = new AphidInterpreter(interpreter.CurrentScope);

            Action call = () =>
            {
                interpreter2.CallFunction(function, parms);
                reset.Set();
            };

            start(call);

            return reset;
        }
        public AphidObject InterpretPartialFunctionExpression(PartialFunctionExpression expression)
        {
            var obj             = (AphidObject)InterpretExpression(expression.Call.FunctionExpression);
            var func            = (AphidFunction)obj.Value;
            var partialArgCount = func.Args.Length - expression.Call.Args.Count();
            var partialArgs     = func.Args.Skip(partialArgCount).ToArray();
            var partialFunc     = new AphidFunction()
            {
                Args = partialArgs,
                Body = new List <Expression>
                {
                    new UnaryOperatorExpression(AphidTokenType.retKeyword,
                                                new CallExpression(
                                                    expression.Call.FunctionExpression,
                                                    expression.Call.Args.Concat(partialArgs.Select(x => new IdentifierExpression(x))).ToArray())),
                },
                ParentScope = _currentScope,
            };

            return(new AphidObject(partialFunc));
        }
 public static ManualResetEvent ThreadPoolQueue(AphidInterpreter interpreter, AphidFunction function, params object[] parms)
 {
     return ThreadCore(interpreter, x => ThreadPool.QueueUserWorkItem(y => x()), function, parms);
 }
 public static ManualResetEvent StartThread(AphidInterpreter interpreter, AphidFunction function, params object[] parms)
 {
     return ThreadCore(interpreter, x => new Thread(y => x()).Start(), function, parms);
 }
 public AphidObject CallFunction(AphidFunction function, params object[] parms)
 {
     return(CallFunctionCore(function, parms.Select(ValueHelper.Wrap)));
 }
 public AphidObject CallFunction(AphidFunction function, params object[] parms)
 {
     return CallFunctionCore(function, parms.Select(ValueHelper.Wrap));
 }
        private AphidObject CallFunctionCore(AphidFunction function, IEnumerable<AphidObject> parms)
        {
            var functionScope = new AphidScope(new AphidObject(), function.ParentScope);

            var i = 0;
            foreach (var arg in parms)
            {
                if (function.Args.Length == i)
                {
                    break;
                }

                functionScope.Variables.Add(function.Args[i++], arg);
            }

            var lastScope = _currentScope;
            _currentScope = functionScope;

            Interpret(function.Body);

            var retVal = GetReturnValue();

            _currentScope = lastScope;

            return retVal;
        }
        public AphidObject InterpretPartialFunctionExpression(PartialFunctionExpression expression)
        {
            var obj = (AphidObject)InterpretExpression(expression.Call.FunctionExpression);
            var func = (AphidFunction)obj.Value;
            var partialArgCount = func.Args.Length - expression.Call.Args.Count();
            var partialArgs = func.Args.Skip(partialArgCount).ToArray();
            var partialFunc = new AphidFunction()
            {
                Args = partialArgs,
                Body = new List<Expression>
                {
                    new UnaryOperatorExpression(AphidTokenType.retKeyword,
                        new CallExpression(
                            expression.Call.FunctionExpression,
                            expression.Call.Args.Concat(partialArgs.Select(x => new IdentifierExpression(x))).ToArray())),
                },
                ParentScope = _currentScope,
            };

            return new AphidObject(partialFunc);
        }