CreateClass() public method

public CreateClass ( string clsname ) : IClass
clsname string
return IClass
Exemplo n.º 1
0
 public void AvoidDuplicatedInstanceVariable()
 {
     Machine machine = new Machine();
     IClass cls = machine.CreateClass("TestClass");
     cls.DefineInstanceVariable("x");
     cls.DefineInstanceVariable("x");
 }
Exemplo n.º 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]);
        }
Exemplo n.º 3
0
        public void BeCreated()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            IBlock block = new Block();

            Assert.IsNotNull(block);
        }
Exemplo n.º 4
0
        public void AddInstanceVariable()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            cls.DefineInstanceVariable("x");
            cls.DefineInstanceVariable("y");

            Assert.AreEqual(0, cls.GetInstanceVariableOffset("x"));
            Assert.AreEqual(1, cls.GetInstanceVariableOffset("y"));
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public void CreateClass()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");

            Assert.IsNotNull(cls);
            Assert.AreEqual("TestClass", cls.Name);
            Assert.AreEqual(-1, cls.GetInstanceVariableOffset("x"));
            Assert.IsNull(cls.GetInstanceMethod("x"));
            Assert.IsNotNull(cls.SuperClass);
            Assert.IsInstanceOfType(cls.SuperClass, typeof(IClass));
            Assert.AreEqual("UndefinedObject", ((IClass)cls.SuperClass).Name);
        }
Exemplo n.º 7
0
        public void BeCreated()
        {
            Machine machine = new Machine();

            IClass cls = machine.CreateClass("Rectangle");

            Assert.IsNotNull(cls);
            Assert.AreEqual("Rectangle", cls.Name);
            Assert.IsNull(cls.GetInstanceMethod("x"));
            Assert.IsNull(cls.GetClassMethod("x"));
            Assert.IsNotNull(cls.SuperClass);
            Assert.IsInstanceOfType(cls.SuperClass, typeof(IClass));
            Assert.AreEqual("UndefinedObject", ((IClass)cls.SuperClass).Name);
        }
Exemplo n.º 8
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));
        }
Exemplo n.º 9
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]);
        }
Exemplo n.º 10
0
        public void CreateClassWithInstanceAndClassVariables()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass", null, "x", "Count");

            Assert.IsNotNull(cls);
            Assert.AreEqual("TestClass", cls.Name);
            Assert.AreEqual(0, cls.GetInstanceVariableOffset("x"));
            Assert.AreEqual(-1, cls.GetInstanceVariableOffset("y"));
            Assert.AreEqual(0, cls.GetClassVariableOffset("Count"));
            Assert.AreEqual(-1, cls.GetClassVariableOffset("y"));
            Assert.AreEqual("x", cls.GetInstanceVariableNamesAsString());
            Assert.AreEqual("Count", cls.GetClassVariableNamesAsString());
            Assert.AreEqual(0, cls.NoVariables);
            Assert.AreEqual(1, cls.NoClassVariables);

            Assert.IsNull(cls.GetInstanceMethod("x"));
            Assert.IsNotNull(cls.SuperClass);
            Assert.IsInstanceOfType(cls.SuperClass, typeof(IClass));
            Assert.AreEqual("UndefinedObject", ((IClass)cls.SuperClass).Name);
        }
Exemplo n.º 11
0
        public void CompileMethodWithLocals()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("Rectangle");
            cls.DefineInstanceVariable("x");
            Parser compiler = new Parser("x | temp | temp := x. ^temp");
            var method = compiler.CompileInstanceMethod(cls);

            Assert.IsNotNull(method);
            Assert.AreEqual("x", method.Name);
            Assert.IsNotNull(method.ByteCodes);
        }
Exemplo n.º 12
0
        public void CompileMethodWithHorizontalBarAsName()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("Rectangle");
            Parser compiler = new Parser("| aBoolean ^aBoolean");
            var method = compiler.CompileInstanceMethod(cls);

            Assert.IsNotNull(method);
            Assert.AreEqual("|", method.Name);
            Assert.IsNotNull(method.ByteCodes);
        }
Exemplo n.º 13
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)
                {
                    Parser compiler = new Parser(method);
                    cls.DefineInstanceMethod(compiler.CompileInstanceMethod(cls));
                }
            }

            if (clsmethods != null)
            {
                foreach (string method in clsmethods)
                {
                    Parser compiler = new Parser(method);
                    cls.DefineClassMethod(compiler.CompileClassMethod(cls));
                }
            }

            return cls;
        }
Exemplo n.º 14
0
        public void DefineClassVariables()
        {
            Machine machine = new Machine();
            IClass bclass = machine.CreateClass("MyClass");

            bclass.DefineClassVariable("Count");
            bclass.DefineClassVariable("Items");

            Assert.AreEqual(0, bclass.Behavior.NoInstanceVariables);
            Assert.AreEqual(0, bclass.NoVariables);
            Assert.AreEqual(2, bclass.NoClassVariables);

            Assert.AreEqual(0, bclass.GetClassVariableOffset("Count"));
            Assert.AreEqual(1, bclass.GetClassVariableOffset("Items"));
            Assert.AreEqual(-1, bclass.GetClassVariableOffset("Z"));

            Assert.AreEqual(0, ((IClassDescription)bclass.Behavior).GetClassVariableOffset("Count"));
            Assert.AreEqual(1, ((IClassDescription)bclass.Behavior).GetClassVariableOffset("Items"));
            Assert.AreEqual(-1, ((IClassDescription)bclass.Behavior).GetClassVariableOffset("Z"));

            Assert.AreEqual("Count Items", bclass.GetClassVariableNamesAsString());
            Assert.AreEqual("Count Items", ((IClassDescription)bclass.Behavior).GetClassVariableNamesAsString());
            Assert.AreEqual(string.Empty, bclass.GetInstanceVariableNamesAsString());
            Assert.AreEqual(null, bclass.GetInstanceVariableNames());

            var result = bclass.GetClassVariableNames();

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("Count", result.ElementAt(0));
            Assert.AreEqual("Items", result.ElementAt(1));

            result = ((IClassDescription)bclass.Behavior).GetClassVariableNames();

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("Count", result.ElementAt(0));
            Assert.AreEqual("Items", result.ElementAt(1));
        }
Exemplo n.º 15
0
 public void CreateObject()
 {
     Machine machine = new Machine();
     IClass cls = machine.CreateClass("TestClass");
     cls.DefineInstanceVariable("x");
     cls.DefineInstanceVariable("y");
     IObject obj = (IObject)cls.NewObject();
     Assert.IsNotNull(obj);
     Assert.IsNull(obj[0]);
     Assert.IsNull(obj[1]);
 }
Exemplo n.º 16
0
 public void CompileClassMethod()
 {
     Machine machine = new Machine();
     IClass cls = machine.CreateClass("Rectangle");
     cls.DefineClassVariable("x");
     var method = this.compiler.CompileClassMethod("x ^x", cls);
     Assert.IsNotNull(method);
     Assert.IsNotNull(method.ByteCodes);
     Assert.AreEqual("x ^x", method.SourceCode);
 }
Exemplo n.º 17
0
 public void CompileInstanceVariableInBlockInsideAMethod()
 {
     Machine machine = new Machine();
     IClass cls = machine.CreateClass("Rectangle");
     cls.DefineInstanceVariable("x");
     var method = this.compiler.CompileInstanceMethod("x ^[x] value", cls);
     Assert.IsNotNull(method);
     Assert.IsNotNull(method.ByteCodes);
     Assert.AreEqual("x ^[x] value", method.SourceCode);
     Assert.IsTrue(method.NoConstants > 0);
     Assert.IsInstanceOfType(method.GetConstant(0), typeof(Block));
     var block = (Block)method.GetConstant(0);
     var result = (new BlockDecompiler(block)).Decompile();
     Assert.IsNotNull(result);
     Assert.AreEqual(1, result.Count);
     Assert.AreEqual("GetInstanceVariable x", result[0]);
 }
Exemplo n.º 18
0
        public void AvoidNullName()
        {
            Machine machine = new Machine();

            IClass cls = machine.CreateClass(null);
        }
Exemplo n.º 19
0
        public void InheritsVariables()
        {
            Machine machine = new Machine();

            IClass supercls = machine.CreateClass("Figure");
            supercls.DefineInstanceVariable("x");
            supercls.DefineInstanceVariable("y");

            IClass cls = machine.CreateClass("Rectangle", supercls);
            Assert.AreEqual(supercls, cls.SuperClass);
            cls.DefineInstanceVariable("width");
            cls.DefineInstanceVariable("height");

            Assert.IsNotNull(cls);
            Assert.AreEqual("Rectangle", cls.Name);

            Assert.AreEqual(0, supercls.GetInstanceVariableOffset("x"));
            Assert.AreEqual(1, supercls.GetInstanceVariableOffset("y"));

            Assert.AreEqual(0, cls.GetInstanceVariableOffset("x"));
            Assert.AreEqual(1, cls.GetInstanceVariableOffset("y"));
            Assert.AreEqual(2, cls.GetInstanceVariableOffset("width"));
            Assert.AreEqual(3, cls.GetInstanceVariableOffset("height"));
        }
Exemplo n.º 20
0
        public void CompileSetMethod()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("Rectangle");
            cls.DefineInstanceVariable("x");
            Parser compiler = new Parser("x: newX x := newX");
            var method = compiler.CompileInstanceMethod(cls);

            Assert.IsNotNull(method);
            Assert.AreEqual("x:", method.Name);
            Assert.IsNotNull(method.ByteCodes);
        }
Exemplo n.º 21
0
        internal 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)
                    cls.DefineInstanceMethod(this.compiler.CompileInstanceMethod(method, cls));

            if (clsmethods != null)
                foreach (string method in clsmethods)
                    cls.DefineClassMethod(this.compiler.CompileClassMethod(method, cls));

            return cls;
        }
Exemplo n.º 22
0
 public void CompileSetMethod()
 {
     Machine machine = new Machine();
     IClass cls = machine.CreateClass("Rectangle");
     cls.DefineInstanceVariable("x");
     var method = this.compiler.CompileInstanceMethod("x: newX x := newX", cls);
     Assert.IsNotNull(method);
     Assert.IsNotNull(method.ByteCodes);
 }
Exemplo n.º 23
0
 public void CompileInstanceMethod()
 {
     Machine machine = new Machine();
     IClass cls = machine.CreateClass("Rectangle");
     cls.DefineInstanceVariable("x");
     var method = this.compiler.CompileInstanceMethod("x ^x", cls);
     Assert.IsNotNull(method);
     Assert.IsNotNull(method.ByteCodes);
     Assert.AreEqual("x ^x", method.SourceCode);
     var result = (new BlockDecompiler(method)).Decompile();
     Assert.IsNotNull(result);
     Assert.AreEqual(2, result.Count);
     Assert.AreEqual("GetInstanceVariable x", result[0]);
     Assert.AreEqual("ReturnPop", result[1]);
 }
Exemplo n.º 24
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();
        }
Exemplo n.º 25
0
 public void CompileMethodWithLocals()
 {
     Machine machine = new Machine();
     IClass cls = machine.CreateClass("Rectangle");
     cls.DefineInstanceVariable("x");
     var method = this.compiler.CompileInstanceMethod("x | temp | temp := x. ^temp", cls);
     Assert.IsNotNull(method);
     Assert.IsNotNull(method.ByteCodes);
 }
Exemplo n.º 26
0
        public void CompileWithLocals()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            cls.DefineInstanceVariable("x");

            Block block;

            block = new Block();
            block.CompileArgument("newX");
            block.CompileLocal("l");
            block.CompileGet("newX");
            block.CompileSet("l");

            Assert.AreEqual(1, block.NoLocals);
            Assert.IsNotNull(block.ByteCodes);
            Assert.IsTrue(block.ByteCodes.Length > 0);
        }
        protected override object DoesNotUnderstand(Machine machine, IObject self, string msgname, object[] args)
        {
            if (self != null && !(self is IBehavior))
                return base.DoesNotUnderstand(machine, self, msgname, args);

            if (msgname.Equals("new"))
                return ((IBehavior)self).NewObject();

            if (msgname.StartsWith("commentStamp:"))
                return new ChunkReaderProcessor((Machine mach, ICompiler compiler, string text) => { }, 1);

            if (msgname == "methods" || msgname.StartsWith("methodsFor:"))
            {
                IBehavior behavior = (IBehavior)self;
                return new ChunkReaderProcessor((Machine mach, ICompiler compiler, string text) =>
                {
                    var method = compiler.CompileInstanceMethod(text, behavior);
                    if (reservedMethods.Contains(method.Name))
                        return;
                    if (reservedRedefinedMethods.Contains(method.Name) && behavior.GetInstanceMethod(method.Name) != null)
                        return;
                    behavior.DefineInstanceMethod(compiler.CompileInstanceMethod(text, behavior));
                });
            }

            if (msgname.StartsWith("new:") && self is NativeBehavior)
                return ((NativeBehavior)self).CreateObject(args);

            if (msgname.Equals("subclass:") || msgname.Equals("agent:"))
            {
                IClass newclass = machine.CreateClass((string)args[0], (IClass)self);
                machine.SetCurrentEnvironmentObject(newclass.Name, newclass);

                if (msgname.Equals("agent:"))
                    ((BaseClass)newclass).IsAgentClass = true;

                return newclass;
            }

            if (msgname.Equals("subclass:instanceVariableNames:") ||
                msgname.Equals("subclass:instanceVariableNames:classVariableNames:poolDictionaries:category:") ||
                msgname.Equals("variableSubclass:instanceVariableNames:classVariableNames:poolDictionaries:category:") ||

                // TODO weakSubclass because Pharo Kernel Objects defines one, WeakMessageSend
                msgname.Equals("weakSubclass:instanceVariableNames:classVariableNames:poolDictionaries:category:") ||

                // TODO variableWordSubclass because Pharo Kernel Number use it
                msgname.Equals("variableWordSubclass:instanceVariableNames:classVariableNames:poolDictionaries:category:") ||

                // TODO variableByteSubclass because Pharo Kernel Number use it
                msgname.Equals("variableByteSubclass:instanceVariableNames:classVariableNames:poolDictionaries:category:") ||
                msgname.Equals("agent:instanceVariableNames:") ||
                msgname.Equals("agent:instanceVariableNames:classVariableNames:poolDictionaries:category:"))
            {
                string clsname = (string)args[0];
                string instancevarnames = (string)args[1];
                string classvarnames = args.Length > 2 ? (string)args[2] : string.Empty;

                IClass newclass = machine.CreateClass((string)args[0], (IClass)self, instancevarnames, classvarnames);

                if (msgname.StartsWith("agent:"))
                    ((BaseClass)newclass).IsAgentClass = true;
                if (msgname.StartsWith("variableSubclass:") || msgname.StartsWith("variableWordSubclass:"))
                    ((BaseClass)newclass).IsIndexed = true;
                if (args.Length >= 5)
                    ((BaseClass)newclass).Category = (string)args[4];

                machine.SetCurrentEnvironmentObject(newclass.Name, newclass);

                return newclass;
            }

            if (msgname.Equals("subclass:nativeType:"))
            {
                Type type = (Type)args[1];
                NativeBehavior newbehavior = machine.GetNativeBehavior(type);

                if (newbehavior == null)
                {
                    newbehavior = (NativeBehavior)machine.CreateNativeBehavior((IClassDescription)self, type);
                    machine.RegisterNativeBehavior(newbehavior.NativeType, newbehavior);
                }
                else if (newbehavior.SuperClass != self)
                    newbehavior.SetSuperClass((IBehavior)self);

                machine.SetCurrentEnvironmentObject((string)args[0], newbehavior);
                return newbehavior;
            }

            if (msgname.Equals("instanceVariableNames:"))
            {
                string names = (string)args[0];

                if (!string.IsNullOrEmpty(names))
                    throw new NotSupportedException();

                return self;
            }

            return base.DoesNotUnderstand(machine, self, msgname, args);
        }
Exemplo n.º 28
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"));
        }