Esempio n. 1
0
        protected override object DoEvaluate(Irony.Interpreter.ScriptThread thread)
        {
            thread.CurrentNode = this;
            try
            {
                var res = new MtResult();

                var accessor = thread.Bind(_targetName.AsString, BindingRequestFlags.Write | BindingRequestFlags.ExistingOrNew);
                accessor.SetValueRef(thread, res);

                // To allow recursive definitions, we have
                // to evaluate _expression inside the new context.
                var exprResult = _expression.Evaluate(thread) as MtResult;
                exprResult.GetValue((o) =>
                {
                    res.SetValue((state) =>
                    {
                        return o;
                    });
                });

                return res;
            }
            catch (Exception e)
            {
                throw new Exception("Exception on MtBind.DoEvaluate", e);
            }
            finally
            {
                //thread.CurrentNode = Parent;
            }
        }
        protected override object DoEvaluate(ScriptThread thread)
        {
            thread.CurrentNode = this;
            try
            {
                var result = new MtResult();
                var targetThread = _target.NewScriptThread(thread);
                var rTarget = _target.Evaluate(targetThread) as MtResult;

                var sourceThread = _source.NewScriptThread(thread);
                var rSource = _source.Evaluate(sourceThread) as MtResult;

                rTarget.GetValue(target =>
                {
                    rSource.GetValue(source =>
                    {
                        MtStream.ReadFromWriteTo(
                            source.Value as MtStream, target.Value as MtStream, () => {
                                result.SetValue(MtObject.True);
                            });
                    });
                });

                return result;
            }
            catch (Exception e)
            {
                throw new Exception("Exception on MtFlowRightToLeft.DoEvaluate", e);
            }
            finally
            {
                //thread.CurrentNode = Parent;
            }
        }
Esempio n. 3
0
        protected override object DoEvaluate(ScriptThread thread)
        {
            thread.CurrentNode = this;
            try
            {
                var programResult = new MtResult();

                // do NOT use this for anything else other than counting the number of to-finish-threads
                var count = _chains.Count;
                var waitForAllToEnd = new ManualResetEvent(false);

                // TODO THIS NEEDS TO BE FIXED!
                // The rest of the program only cares about the result of the last expression
                // which doesn't depend of the result of the previous expressions;
                // however, the program shouldn't end.
                // SOLUTION: Keep a global counter of all awaiting MtResults?

                for (int i = 0; i < _chains.Count; ++i)
                {
                    // This is here because of issues with closures using the for variable
                    var safe_i = i + 0;
                    var ch = _chains[i];
                    var subthread = ch.NewScriptThread(thread);
                    var chResult = ch.Evaluate(subthread) as MtResult;

                    // Lookout:
                    // do not capture anything loop related inside here
                    chResult.GetValue(delegate(MtObject x)
                    {
                        if (Interlocked.Decrement(ref count) == 0)
                        {
                            waitForAllToEnd.Set();
                        }

                        // Wait for the last chain to end.
                        // Last refers in the order of the chain in the code,
                        // not the order of execution!!!
                        if (safe_i == _chains.Count - 1)
                        {
                            // Wait for the end of *all* the chains before returning ...
                            waitForAllToEnd.WaitOne();
                            programResult.SetValue(x);
                        }
                    });
                }

                // Return value does not matter. This is asynchronous, remember?
                return programResult;
            }
            catch (Exception e)
            {
                throw new Exception("Exception on MtProgram.", e);
            }
            finally
            {
                //thread.CurrentNode = Parent;
            }
        }
Esempio n. 4
0
        protected override object DoEvaluate(Irony.Interpreter.ScriptThread thread)
        {
            // PROLOG
            thread.CurrentNode = this;
            try
            {
                var appResult = new MtResult();

                MtResult[] args = _args.Evaluate(thread) as MtResult[];
                if (args == null)
                {
                    throw new Exception("Args evaluated to null!");
                }

                var subthreadF = _head.NewScriptThread(thread);
                var headResult = _head.Evaluate(subthreadF);
                if (headResult == null)
                {
                    throw new Exception("Head can't evaluate to null!");
                }

                MtFunctionObjectBase.ExtractAsFunction(headResult, (wrkFun) =>
                {

            #if DEBUG && !SILVERLIGHT
                    if (wrkFun is BuiltInCallTarget)
                    {
                        var builtin = wrkFun as BuiltInCallTarget;
                        Debug.Print("Calling builtin: {0}", builtin.Name);
                    }
                    else
                    {
                        Debug.Print("Calling user function");
                    }
            #endif

                    var resultFun = wrkFun.Call(thread, args) as MtResult;
                    resultFun.GetValue((r3) =>
                    {
                        appResult.SetValue(r3);
                    });
                });

                return appResult;
            }
            catch (Exception e)
            {
                throw new Exception("Exception on MtApplication.", e);
            }
            finally
            {
                // EPILOG
                //thread.CurrentNode = Parent;
            }
        }
Esempio n. 5
0
        protected override object DoEvaluate(Irony.Interpreter.ScriptThread thread)
        {
            thread.CurrentNode = this;
            try
            {
                MtResult[] result = new MtResult[_args.Count];

                for (var i = 0; i < _args.Count; ++i)
                {
                    try
                    {
                        var subthread = _args[i].NewScriptThread(thread);
                        var evalResult = _args[i].Evaluate(subthread);

                        if (evalResult == null)
                        {
                            throw new Exception(string.Format("_args[{0}].Evaluate evaluated to null!", i));
                        }

                        if (evalResult is MtResult)
                        {
                            result[i] = evalResult as MtResult;
                        }
                        else
                        {
                            // A function, wrap it!
                            result[i] = MtResult.CreateAndWrap(evalResult);
                        }

                        if (result[i] == null)
                        {
                            throw new Exception("Result is a non-MtResult!");
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("There was a problem evaluating arg {0}", i), e);
                    }
                }

                return result;
            }
            catch (Exception e)
            {
                throw new Exception("Error on MtArguments.DoEvaluate", e);
            }
            finally
            {
                //thread.CurrentNode = Parent;
            }
        }
Esempio n. 6
0
        protected override object DoEvaluate(Irony.Interpreter.ScriptThread thread)
        {
            thread.CurrentNode = this;
            try
            {
                var myRes = new MtResult();

                // Eval expression
                var subthread = _expression.NewScriptThread(thread);
                var res = _expression.Evaluate(subthread) as MtResult;
                res.GetValue((resExpr) =>
                {
                    AstNode toEval = null;
                    if (resExpr.Value == MtObject.False.Value)
                    {
                        // Evaluate false branch
                        toEval = _falseBranch;
                    }
                    else
                    {
                        // Evaluate true branch
                        toEval = _trueBranch;
                    }

                    // Evaluate!
                    var subsubthread = toEval.NewScriptThread(thread);
                    var resBranch = toEval.Evaluate(subsubthread) as MtResult;
                    resBranch.GetValue((_resBranch) =>
                    {
                        myRes.SetValue(_resBranch);
                    });
                });

                return myRes;
            }
            catch (Exception e)
            {
                throw new Exception("Exception on If.DoEvaluate.", e);
            }
            finally
            {
                //thread.CurrentNode = Parent;
            }
        }
        protected override object DoEvaluate(Irony.Interpreter.ScriptThread thread)
        {
            thread.CurrentNode = this;
            try
            {
                var ret = new MtResult();

                ret.SetValue(new MtObject(new MtFunctionObject(_body, _arguments, thread)));

                return ret;
            }
            catch (Exception e)
            {
                throw new Exception("Error on MtFunctionLiteral.DoEvaluate", e);
            }
            finally
            {
                //thread.CurrentNode = Parent;
            }
        }
Esempio n. 8
0
        protected override object DoEvaluate(Irony.Interpreter.ScriptThread thread)
        {
            thread.CurrentNode = this;
            try
            {
                // TODO Create a new context with $ as the current array? RECURSIVE ARRAYS? \ :D /

                var ret = new MtResult();

                var expressions = _expressionList.Evaluate(thread) as MtResult[];

                ret.SetValue(new MtObject(expressions));

                return ret;
            }
            catch (Exception e)
            {
                throw new Exception("Exception on MtArray.DoEvaluate.", e);
            }
            finally
            {
                //thread.CurrentNode = Parent;
            }
        }
Esempio n. 9
0
        protected override object DoEvaluate(ScriptThread thread)
        {
            // PROLOG
            thread.CurrentNode = this;

            try
            {

            #if SILVERLIGHT

                var result = new MtResult();

                // This is the best we can do in silverlight. There is no BeginInvoke!
                ThreadPool.QueueUserWorkItem(new WaitCallback(_ =>
                {
                    // synchronous eval :(
                    var headResult = _head.Evaluate(thread);

                    // Wrap it if necessary.
                    if (headResult as ICallTarget != null)
                    {
                        headResult = MtResult.CreateAndWrap(headResult);
                    }

                    if (headResult as MtResult == null)
                    {
                        throw new Exception("Head of chain evaluated to null!");
                    }

                    var subthread = _tail.NewScriptThread(thread);
                    var accessor = subthread.Bind("_", BindingRequestFlags.Write | BindingRequestFlags.ExistingOrNew);
                    accessor.SetValueRef(subthread, headResult);

                    var _tailResult = _tail.Evaluate(subthread) as MtResult;

                    if (_tailResult == null)
                    {
                        throw new Exception("tail of chain evaluated to null!");
                    }

                    _tailResult.GetValue(o => { result.SetValue(o); });
                }));

                return result;

            #else // SILVERLIGHT

            #if ALL_SYNC
                // synchronous eval :(
                var headResult = _head.Evaluate(thread);

                // Wrap it if necessary.
                if (headResult as ICallTarget != null)
                {
                    headResult = MtResult.CreateAndWrap(headResult);
                }

                if (headResult as MtResult == null)
                {
                    throw new Exception("Head of chain evaluated to null!");
                }

                var subthread = _tail.NewScriptThread(thread);
                var accessor = subthread.Bind("_", BindingRequestFlags.Write | BindingRequestFlags.ExistingOrNew);
                accessor.SetValueRef(subthread, headResult);

                var _tailResult = _tail.Evaluate(subthread) as MtResult;

                if (_tailResult == null)
                {
                    throw new Exception("tail of chain evaluated to null!");
                }

                return _tailResult;

            #else // ALL_SYNC

                MtResult chainResult = new MtResult();

                _head.Evaluate.BeginInvoke(thread, iar =>
                {
                    var headResult = _head.Evaluate.EndInvoke(iar);

                    // Wrap if necessary.
                    if (headResult as ICallTarget != null)
                    {
                        headResult = MtResult.CreateAndWrap(headResult);
                    }

                    if (headResult as MtResult == null)
                    {
                        throw new Exception("Head of chain evaluated to null!");
                    }

                    var subthread = _tail.NewScriptThread(thread);
                    var accessor = subthread.Bind("_", BindingRequestFlags.Write | BindingRequestFlags.ExistingOrNew);
                    accessor.SetValueRef(subthread, headResult);

                    var tailResult = _tail.Evaluate(subthread) as MtResult;

                    if (tailResult == null)
                    {
                        throw new Exception("tail of chain evaluated to null!");
                    }

                    tailResult.GetValue(o =>
                    {
                        chainResult.SetValue(o);
                    });

                }, null);

                return chainResult;
            #endif // ALL_SYNC

            #endif // SILVERLIGHT

            }
            catch (Exception e)
            {
                throw new Exception("Exception on MtChain.DoEvaluate.", e);
            }
            finally
            {
                // EPILOG
                //thread.CurrentNode = Parent;
            }
        }
        protected override object DoEvaluate(ScriptThread thread)
        {
            thread.CurrentNode = this;
            try
            {
                var ret = new MtResult();

                var emitterThread = _eventEmitter.NewScriptThread(thread);
                var emitter = _eventEmitter.Evaluate(emitterThread) as MtResult;

                var listThread = _listener.NewScriptThread(thread);
                var list = _listener.Evaluate(listThread) as MtResult;

                emitter.GetValue(o =>
                {
                    list.GetValue(l =>
                    {
                        // set listener
                        var wrkEmitter = o.Value as IEventEmitter;

                        if (wrkEmitter == null)
                        {
            #if DEBUG && !SILVERLIGHT
                            Debug.Print("I dont throw events");
            #endif
                        }
                        else
                        {
                            ICallTarget callable = null;
                            var haveCallable = new ManualResetEvent(false);

                            // Extract Callable (Compute it once)
                            MtFunctionObjectBase.ExtractAsFunction(
                                    l, fun => {
                                        callable = fun;
                                        haveCallable.Set();
                                    });

                            wrkEmitter.On(_eventName, args => {
            #if DEBUG && !SILVERLIGHT
                                Debug.Print("EventEmitter Called {0}", _eventName);
            #endif
                                var resArgs = new MtResult[args.Length];
                                for (var i = 0; i < args.Length; ++i)
                                {
                                    resArgs[i] = MtResult.CreateAndWrap(args[i]);
                                }

                                haveCallable.WaitOne();

                                // Call it
                                var result = callable.Call(thread, resArgs) as MtResult;

                            });
                        }

                        ret.SetValue(o);
                    });
                });

                return ret.WaitForValue();
                //return ret;
            }
            catch (Exception e)
            {
                throw new Exception("Exception on listener statement evaluate", e);
            }
            finally
            {
                //thread.CurrentNode = Parent;
            }
        }