Example #1
0
 public void OpenScopeFor(Action body,
                          bool when = true,
                          Action <ScopeFrame> withInit = null,
                          ScopeFrame parentScope       = null,
                          BikeFunction func            = null)
 {
     if (when)
     {
         var scopeFrame = new ScopeFrame(func, GlobalFrame, parentScope ?? CurrentFrame, CurrentFrame);
         if (withInit != null)
         {
             withInit(scopeFrame);
         }
         Push(scopeFrame);
     }
     try
     {
         body();
     }
     finally
     {
         if (when)
         {
             Pop();
         }
     }
 }
Example #2
0
        public override object Walk(FunctionDeclaration node)
        {
            var funcDec = new BikeFunction(node, node.Identifier, node.FormalParameters, node.Body,
                                           Context.CurrentFrame);

            Context.CurrentFrame.Define(node.Identifier.Value, funcDec);
            return(funcDec);
        }
 public BikeCallback(object target, Type returnType, BikeFunction function)
 {
     callingThread = Thread.CurrentThread;
     ContextLocal[callingThread] = InterpretationContext.Instance;
     Target     = target;
     ReturnType = returnType;
     Function   = function;
 }
Example #4
0
 public T OpenScopeFor <T>(Func <T> body,
                           bool when = true,
                           Action <ScopeFrame> withInit = null,
                           ScopeFrame parentScope       = null,
                           BikeFunction func            = null)
 {
     return(scopeStack.OpenScopeFor(body, when, withInit, parentScope, func));
 }
Example #5
0
 public void OpenScopeFor(Action body,
                          bool when = true,
                          Action <ScopeFrame> withInit = null,
                          ScopeFrame parentScope       = null,
                          BikeFunction func            = null)
 {
     scopeStack.OpenScopeFor(body, when, withInit, parentScope, func);
 }
Example #6
0
 private ScopeFrame(SourceLocation location, BikeFunction func, 
     ScopeFrame global, ScopeFrame parent, ScopeFrame caller)
 {
     this.Location = location;
     this.Func = func;
     this.global = global;
     this.parent = parent;
     this.Caller = caller;
 }
Example #7
0
 private ScopeFrame(SourceLocation location, BikeFunction func,
                    ScopeFrame global, ScopeFrame parent, ScopeFrame caller)
 {
     this.Location = location;
     this.Func     = func;
     this.global   = global;
     this.parent   = parent;
     this.Caller   = caller;
 }
        public void AddHandler(object target, string name, BikeFunction bikeFunc, Interpreter interpreter, bool isStatic)
        {
            var invoker      = new BikeCallback(target, null, bikeFunc);
            var delegateType = isStatic
                ? ((Type)target).AddHandler(name, invoker.Callback)
                : target.AddHandler(name, invoker.Callback);

            invoker.ReturnType = delegateType.GetMethod("Invoke").ReturnType;
        }
        public object CallBikeFunction(BikeFunction function, object self, object[] argValues)
        {
            var parameters = function.Parameters;
            Action <ScopeFrame> bindArgs = scopeFrame =>
            {
                scopeFrame.Define("this", self);
                for (var i = 0; i < parameters.Count; i++)
                {
                    var argName = parameters[i].Identifier.Value;
                    if (i < argValues.Length)                     // normal parameter
                    {
                        if (parameters[i].IsParams)               // collapse
                        {
                            var bk = new BikeArray();
                            for (var j = i; j < argValues.Length; j++)
                            {
                                bk[j - i] = argValues[j];
                            }
                            scopeFrame.Define(argName, bk);
                        }
                        else
                        {
                            scopeFrame.Define(argName, argValues[i]);
                        }
                    }
                    else                     // optional parameter
                    {
                        scopeFrame.Define(argName, null);
                    }
                }
            };
            Func <object> execute = () =>
            {
                try
                {
                    return(function.Body.Accept(this));
                }
                catch (Return r)
                {
                    return(r.Value);
                }
            };

            return(Context.OpenScopeFor(execute,
                                        withInit: bindArgs,
                                        parentScope: function.BoundScope,
                                        func: function));
        }
Example #10
0
 public object CallBikeFunction(BikeFunction function, object self, object[] argValues)
 {
     var parameters = function.Parameters;
     Action<ScopeFrame> bindArgs = scopeFrame =>
     {
         scopeFrame.Define("this", self);
         for (var i = 0; i < parameters.Count; i++)
         {
             var argName = parameters[i].Identifier.Value;
             if (i < argValues.Length) // normal parameter
             {
                 if (parameters[i].IsParams) // collapse
                 {
                     var bk = new BikeArray();
                     for (var j = i; j < argValues.Length; j++)
                     {
                         bk[j - i] = argValues[j];
                     }
                     scopeFrame.Define(argName, bk);
                 }
                 else
                 {
                     scopeFrame.Define(argName, argValues[i]);
                 }
             }
             else // optional parameter
             {
                 scopeFrame.Define(argName, null);
             }
         }
     };
     Func<object> execute = () =>
     {
         try
         {
             return function.Body.Accept(this);
         }
         catch (Return r)
         {
             return r.Value;
         }
     };
     return Context.OpenScopeFor(execute,
                                 withInit: bindArgs,
                                 parentScope: function.BoundScope,
                                 func: function);
 }
Example #11
0
 public ScopeFrame(BikeFunction func, ScopeFrame global, 
                   ScopeFrame parent, ScopeFrame caller)
     : this(InterpretationContext.Instance.CurrentLocation,
            func, global, parent, caller)
 {
 }
Example #12
0
 public override object Walk(FunctionExpression node)
 {
     var funcDec = new BikeFunction(node, node.Identifier, node.FormalParameters, node.Body,
         Context.CurrentFrame);
     if (node.Identifier != null)
         Context.CurrentFrame.Define(node.Identifier.Value, funcDec);
     return funcDec;
 }
Example #13
0
        }                                                            // global frame

        public ScopeFrame(BikeFunction func, ScopeFrame global,
                          ScopeFrame parent, ScopeFrame caller)
            : this(InterpretationContext.Instance.CurrentLocation,
                   func, global, parent, caller)
        {
        }
Example #14
0
 public object CallBikeFunction(BikeFunction function, object self, Arguments arguments)
 {
     var argValues = GetArgumentValues(arguments);
     return CallBikeFunction(function, self, argValues);
 }
        public object CallBikeFunction(BikeFunction function, object self, Arguments arguments)
        {
            var argValues = GetArgumentValues(arguments);

            return(CallBikeFunction(function, self, argValues));
        }