Наследование: Block, IMethod
Пример #1
0
        public void CompileGlobalName()
        {
            Method mth = new Method("test");
            byte p = mth.CompileGlobal("Class");

            Assert.AreEqual(0, p);
        }
Пример #2
0
        public void Compile()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            cls.DefineInstanceVariable("x");
            cls.DefineClassVariable("count");

            Block block;

            block = new Method(cls, "x:");
            block.CompileArgument("newX");
            block.CompileGet("newX");
            block.CompileGet("count");
            block.CompileSet("x");

            Assert.AreEqual(1, block.Arity);
            Assert.AreEqual(0, block.NoLocals);
            Assert.IsNotNull(block.ByteCodes);
            Assert.IsTrue(block.ByteCodes.Length > 0);

            BlockDecompiler decompiler = new BlockDecompiler(block);
            var result = decompiler.Decompile();

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count);

            Assert.AreEqual("GetArgument newX", result[0]);
            Assert.AreEqual("GetClassVariable count", result[1]);
            Assert.AreEqual("SetInstanceVariable x", result[2]);
        }
Пример #3
0
 public Method CompileInstanceMethod(string text, IBehavior cls)
 {
     ModelParser parser = new ModelParser(text);
     var methodmodel = parser.ParseMethod();
     Method method = new Method(cls, methodmodel.Selector, text);
     BytecodeCompiler compiler = new BytecodeCompiler(method);
     compiler.CompileMethod(methodmodel);
     return method;
 }
Пример #4
0
        public void BeCreated()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            IMethod mth = new Method(cls, "method1");

            Assert.IsNotNull(mth);
            Assert.AreEqual("method1", mth.Name);
            Assert.AreEqual(cls, mth.Behavior);
            Assert.AreEqual("TestClass", ((IClass)mth.Behavior).Name);
        }
Пример #5
0
        public void DefineInstanceMethod()
        {
            IMetaClass meta = BaseMetaClass.CreateMetaClass(null, this.machine);
            BaseBehavior behavior = new BaseBehavior(meta, null, this.machine);
            IMethod method = new Method("method");

            behavior.DefineInstanceMethod(new Method("method"));

            IMethod result = behavior.GetInstanceMethod("method");
            Assert.IsNotNull(result);
            Assert.AreEqual("method", result.Name);
        }
Пример #6
0
        public void Compile()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            cls.DefineClassVariable("count");
            cls.DefineInstanceVariable("x");

            Method mth;

            mth = new Method(cls, "x:");
            mth.CompileArgument("newX");
            mth.CompileGet("newX");
            mth.CompileSet("x");

            cls.DefineInstanceMethod(mth);

            Assert.AreEqual(mth, cls.GetInstanceMethod("x:"));
            Assert.AreEqual("x", mth.GetInstanceVariableName(0));
        }
Пример #7
0
        public void AddBehaviorAsTrait()
        {
            BaseBehavior behavior = new BaseBehavior(null, null, this.machine);
            IMethod method = new Method("method");

            behavior.DefineInstanceMethod(method);

            BaseBehavior trait = new BaseBehavior(null, null, this.machine);
            IMethod method2 = new Method("method2");

            trait.DefineInstanceMethod(method2);

            behavior.AddTrait(trait);

            IMethod result = behavior.GetInstanceMethod("method");
            Assert.IsNotNull(result);
            Assert.AreSame(method, result);
            result = behavior.GetInstanceMethod("method2");
            Assert.IsNotNull(result);
            Assert.AreSame(method2, result);
        }
Пример #8
0
        public void CompileAndRun()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            cls.DefineInstanceVariable("x");

            Method mth;

            mth = new Method(cls, "x:");
            mth.CompileArgument("newX");
            mth.CompileGet("newX");
            mth.CompileSet("x");

            cls.DefineInstanceMethod(mth);

            IObject obj = (IObject)cls.NewObject();

            mth.Execute(machine, obj, new object[] { 10 });

            Assert.AreEqual(10, obj[0]);
        }
Пример #9
0
        public void DefineObjectMethod()
        {
            Machine machine = new Machine();
            BaseClass cls = new BaseClass("MyClass", machine);
            BaseObject bo = (BaseObject)cls.NewObject();

            Assert.AreEqual(cls, bo.Behavior);

            IMethod method = new Method(cls, "mymethod");

            bo.DefineObjectMethod(method);

            Assert.AreNotEqual(cls, bo.Behavior);
            Assert.IsNotNull(bo.Behavior.GetInstanceMethod("mymethod"));
            Assert.AreEqual(method, bo.Behavior.GetInstanceMethod("mymethod"));

            Assert.AreNotEqual(cls, method.Behavior);
            Assert.AreEqual(bo.Behavior, method.Behavior);

            Assert.IsTrue(bo.IsPrototype);
            Assert.AreEqual(true, bo.SendMessage(machine, "isPrototype", null));
        }
Пример #10
0
        public void GetClassMethods()
        {
            IMetaClass meta = BaseMetaClass.CreateMetaClass(null, this.machine);
            BaseBehavior behavior = new BaseBehavior(meta, null, this.machine);
            IMethod method = new Method("method");

            behavior.DefineClassMethod(new Method("method1"));
            behavior.DefineClassMethod(new Method("method2"));

            ICollection<IMethod> methods = behavior.GetClassMethods();

            Assert.IsNotNull(methods);
            Assert.AreEqual(2, methods.Count);
        }
Пример #11
0
        public void GetInstanceMethods()
        {
            BaseBehavior behavior = new BaseBehavior(null, null, this.machine);
            IMethod method = new Method("method");

            behavior.DefineInstanceMethod(new Method("method1"));
            behavior.DefineInstanceMethod(new Method("method2"));

            ICollection<IMethod> methods = behavior.GetInstanceMethods();

            Assert.IsNotNull(methods);
            Assert.AreEqual(2, methods.Count);
        }
Пример #12
0
        public void GetInstanceVariableOffsetWithOuter()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            cls.DefineInstanceVariable("x");
            Method method = new Method(cls, "mymethod");

            Block block = new Block("x", method);
            Assert.AreEqual(0, block.GetInstanceVariableOffset("x"));
        }
Пример #13
0
        public void NullSourceCode()
        {
            Machine machine = new Machine();

            Method method;

            method = new Method("anyMethod");

            Assert.IsNull(method.SourceCode);
        }
Пример #14
0
        public void DecompileGetSetInstanceVariable()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            cls.DefineInstanceVariable("x");
            cls.DefineInstanceVariable("y");
            Block block = new Method(cls,"process");
            block.CompileByteCode(ByteCode.GetInstanceVariable, 0);
            block.CompileByteCode(ByteCode.SetInstanceVariable, 1);
            BlockDecompiler decompiler = new BlockDecompiler(block);

            var result = decompiler.Decompile();

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("GetInstanceVariable x", result[0]);
            Assert.AreEqual("SetInstanceVariable y", result[1]);
        }
Пример #15
0
        static void Main(string[] args)
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("Point");
            cls.DefineInstanceVariable("x");
            cls.DefineInstanceVariable("y");
            IObject obj = (IObject) cls.NewObject();
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
            obj[0]=0;
            obj[1]=1;
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
            Console.ReadLine();
            Method mth = new Method(cls,"set:");
            mth.CompileArgument("newValue");
            mth.CompileGet("newValue");
            mth.CompileSet("x");
            mth.CompileGet("newValue");
            mth.CompileSet("y");
            mth.Execute(obj.Behavior,obj,new object[] {10});
            cls.DefineInstanceMethod(mth);
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
            Console.ReadLine();
            mth = new Method(cls,"x:y:");
            mth.CompileArgument("newX");
            mth.CompileArgument("newY");
            mth.CompileGet("newX");
            mth.CompileSet("x");
            mth.CompileGet("newY");
            mth.CompileSet("y");
            mth.Execute(obj.Behavior,obj,new object[] {10,20});
            cls.DefineInstanceMethod(mth);
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
            Console.ReadLine();

            mth = new Method(cls,"x:");
            mth.CompileArgument("newX");
            mth.CompileGet("newX");
            mth.CompileSet("x");
            mth.Execute(obj.Behavior,obj,new object[] {10,20});
            cls.DefineInstanceMethod(mth);

            mth = new Method(cls,"y:");
            mth.CompileArgument("newY");
            mth.CompileGet("newY");
            mth.CompileSet("y");
            mth.Execute(obj.Behavior,obj,new object[] {10,20});
            cls.DefineInstanceMethod(mth);

            Parser compiler = new Parser("set2: newValue self x: newValue self y: newValue.");
            compiler.CompileInstanceMethod(cls);

            compiler = new Parser("x ^x.");
            compiler.CompileInstanceMethod(cls);

            compiler = new Parser("y ^y.");
            compiler.CompileInstanceMethod(cls);

            compiler = new Parser("x1 ^x+1.");
            compiler.CompileInstanceMethod(cls);

            compiler = new Parser("y1 ^y+1.");
            compiler.CompileInstanceMethod(cls);

            obj.SendMessage("set2:", new object[] { 10 });
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
            Console.ReadLine();

            obj.SendMessage("x:", new object[] { 30 });
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
            Console.ReadLine();

            obj.SendMessage("y:", new object[] { 20 });
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
            Console.ReadLine();

            Console.WriteLine(obj.SendMessage("x", null));
            Console.WriteLine(obj.SendMessage("y", null));
            Console.ReadLine();
        }
Пример #16
0
        internal static IClass CompileClass(string clsname, string[] varnames, string[] methods, string[] clsmethods)
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass(clsname);

            if (varnames != null)
            {
                foreach (string varname in varnames)
                {
                    cls.DefineInstanceVariable(varname);
                }
            }

            if (methods != null)
            {
                foreach (string method in methods)
                {
                    ModelParser parser = new ModelParser(method);
                    MethodModel model = parser.ParseMethod();
                    Method newmethod = new Method(cls, model.Selector, method);
                    BytecodeCompiler compiler = new BytecodeCompiler(newmethod);
                    compiler.CompileMethod(model);
                    cls.DefineInstanceMethod(newmethod);
                }
            }

            if (clsmethods != null)
            {
                foreach (string method in clsmethods)
                {
                    ModelParser parser = new ModelParser(method);
                    MethodModel model = parser.ParseMethod();
                    Method newmethod = new Method(cls, model.Selector, method);
                    BytecodeCompiler compiler = new BytecodeCompiler(newmethod);
                    compiler.CompileMethod(model);
                    cls.DefineClassMethod(newmethod);
                }
            }

            return cls;
        }
Пример #17
0
        public void NotRunWithMachine()
        {
            Machine machine = new Machine();

            Method method;

            method = new Method("invalidMethod");
            method.CompileArgument("newX");
            method.CompileGet("newX");
            method.CompileSet("GlobalX");

            method.Execute(machine, new object[] { 10 });
        }