Ejemplo n.º 1
0
        public object Execute()
        {
            ExecutionContext context = this.block.CreateContext(this.machine, this.arguments);
            this.interpreter = new Interpreter(context);

            return this.interpreter.Execute();
        }
Ejemplo n.º 2
0
 public void CreateAndInvokeAgentUsingInterpreter()
 {
     Machine machine = new Machine();
     Block block = new Block();
     AjTalk.Language.ExecutionContext context = new AjTalk.Language.ExecutionContext(machine, null, block, null);
     Interpreter interpreter = new Interpreter(context);
     ManualResetEvent handle = new ManualResetEvent(false);
     bool executed = false;
     AgentObject agent = new AgentObject();
     agent.ExecuteMethod(
         interpreter,
         new FunctionalMethod((x, y, args) =>
         {
             executed = true;
             return handle.Set();
         }),
     null);
     handle.WaitOne();
     Assert.IsTrue(executed);
 }
Ejemplo n.º 3
0
 public object ExecuteInInterpreter(Interpreter interpreter, IObject self, object[] args)
 {
     interpreter.PushContext(new ExecutionContext(interpreter.Machine, self, this, args));
     return interpreter;
 }
 protected override object DoesNotUnderstand(Interpreter interpreter, IObject self, string msgname, object[] args)
 {
     return this.DoesNotUnderstand(interpreter.Machine, self, msgname, args);
 }
Ejemplo n.º 5
0
 public virtual object ExecuteInInterpreter(Interpreter interpreter, object[] args)
 {
     interpreter.PushContext(this.CreateContext(interpreter.Machine, args));
     return interpreter;
 }
Ejemplo n.º 6
0
 public virtual object ExecuteMethod(Interpreter interpreter, IMethod method, object[] arguments)
 {
     return method.ExecuteInInterpreter(interpreter, this, arguments);
 }
Ejemplo n.º 7
0
 public override object ExecuteMethod(Interpreter interpreter, IMethod method, object[] arguments)
 {
     Message message = new Message(interpreter.Machine, method, arguments);
     this.queue.PostMessage(message);
     return null;    // TODO what to return?
 }
Ejemplo n.º 8
0
 public object ExecuteMethod(Interpreter interpreter, IMethod method, object[] arguments)
 {
     return this.obj.ExecuteMethod(interpreter, method, arguments);
 }
Ejemplo n.º 9
0
        public static object SendMessage(Interpreter interpreter, object obj, string msgname, object[] args)
        {
            if (obj == null)
                return SendNativeMessage(interpreter.Machine, obj, msgname, args);

            NativeBehavior behavior = interpreter.Machine.GetNativeBehavior(obj.GetType());

            if (behavior != null)
            {
                IMethod mth = behavior.GetInstanceMethod(msgname);

                if (mth != null)
                    return mth.ExecuteNativeInInterpreter(interpreter, obj, args);
            }

            string mthname = msgname;
            int p = msgname.IndexOf(":");

            if (p > 0)
                mthname = msgname.Substring(0, p);
            else
                mthname = msgname;

            if (obj is Type)
            {
                if (msgname == "new" || msgname.StartsWith("new:"))
                    return NewObject((Type)obj, args);
            }

            if (obj is IList)
            {
                behavior = interpreter.Machine.GetNativeBehavior(typeof(IList));
                IMethod mth = behavior.GetInstanceMethod(msgname);

                if (mth != null)
                    return mth.ExecuteNativeInInterpreter(interpreter, obj, args);
            }

            if (obj is IEnumerable)
            {
                behavior = interpreter.Machine.GetNativeBehavior(typeof(IEnumerable));
                IMethod mth = behavior.GetInstanceMethod(msgname);

                if (mth != null)
                    return mth.ExecuteNativeInInterpreter(interpreter, obj, args);
            }

            // TODO how to use doesNotUnderstand in native behavior
            // mth = behavior.GetInstanceMethod("doesNotUnderstand:with:");
            // if (mth != null)
            //    return mth.ExecuteNative(obj, new object[] { msgname, args });
            return SendNativeMessage(interpreter.Machine, obj, mthname, args);
        }
Ejemplo n.º 10
0
        public object SendMessageToObject(IObject self, Interpreter interpreter, string msgname, object[] args)
        {
            IMethod mth = this.GetInstanceMethod(msgname);

            // TODO refactor self == null, it could be all in SendMessageToNilObject
            if (mth != null)
                return self.ExecuteMethod(interpreter, mth, args);

            mth = this.GetInstanceMethod("doesNotUnderstand:with:");

            if (mth != null)
                return self.ExecuteMethod(interpreter, mth, new object[] { msgname, args });

            return DotNetObject.SendMessage(interpreter.Machine, self, msgname, args);
        }
Ejemplo n.º 11
0
 public object ExecuteNativeInInterpreter(Interpreter interpreter, object self, object[] args)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 12
0
 public object ExecuteInInterpreter(Interpreter interpreter, IObject self, object[] args)
 {
     return this.DoesNotUnderstand(interpreter, self, (string)args[0], (object[])args[1]);
 }
Ejemplo n.º 13
0
        protected virtual object DoesNotUnderstand(Interpreter interpreter, IObject self, string msgname, object[] args)
        {
            if (interpreter.Machine.HostMachine != null)
            {
                IBehavior behavior = interpreter.Machine.HostMachine.GetAssociatedBehavior(self.Behavior);

                if (behavior != null)
                {
                    IMethod method = behavior.GetInstanceMethod(msgname);

                    if (method != null)
                        return method.ExecuteInInterpreter(interpreter, self, args);

                    method = behavior.GetInstanceMethod(this.Name);

                    if (method != null)
                        return method.ExecuteInInterpreter(interpreter, self, new object[] { msgname, args });
                }
            }

            return DotNetObject.SendMessage(interpreter.Machine, self, msgname, args);
        }
Ejemplo n.º 14
0
        public object SendMessage(object obj, string msgname, object[] args, Interpreter interpreter)
        {
            if (this.debug)
            {
                if (obj == null)
                    Trace.Write("UndefinedObject");
                else if ((obj as IObject) == null)
                    Trace.Write(obj.GetType().FullName);
                else
                {
                    IObject io = (IObject)obj;

                    if (io.Behavior is IMetaClass)
                    {
                        IClass cls = ((IMetaClass)io.Behavior).ClassInstance;

                        if (cls == null)
                            Trace.Write("meta");
                        else
                            Trace.Write(cls.Name);
                        Trace.Write(" class");
                    }
                    else
                        Trace.Write(((IClass)io.Behavior).Name);
                }

                Trace.Write(">>");
                Trace.WriteLine(msgname);
            }

            if (obj == null)
                return this.nilclass.SendMessageToNilObject(this, msgname, args);

            IObject iobj = obj as IObject;

            if (iobj != null)
                if (interpreter == null)
                    return iobj.Behavior.SendMessageToObject(iobj, this, msgname, args);
                else
                    return iobj.Behavior.SendMessageToObject(iobj, interpreter, msgname, args);

            if (interpreter != null)
                return DotNetObject.SendMessage(interpreter, obj, msgname, args);

            return DotNetObject.SendMessage(this, obj, msgname, args);
        }
Ejemplo n.º 15
0
 public object ExecuteInInterpreter(Interpreter interpreter, IObject self, object[] args)
 {
     return this.Execute(interpreter.Machine, self, args);
 }
Ejemplo n.º 16
0
 public object ExecuteNativeInInterpreter(Interpreter interpreter, object self, object[] args)
 {
     return this.nativeFunction(interpreter.Machine, self, args);
 }