Ejemplo n.º 1
0
        public void ShouldExecuteInstAtPut()
        {
            PepsiMachine machine = new PepsiMachine();

            string[] methods =
            {
                "x [^x]",
                "x: newX [x := newX]",
                "y [^y]",
                "y: newY [y := newY]"
            };

            IClass cls = CompileClass(
                "Rectangle",
                new string[] { "x", "y" },
                methods);

            IObject iobj = cls.CreateInstance();

            machine.SetGlobalObject("aRectangle", iobj);

            Compiler compiler = new Compiler("aRectangle instAt: 0 put: 200");
            Block    block    = compiler.CompileBlock();

            Assert.IsNotNull(block);

            block.Execute(machine, null);

            Assert.AreEqual(200, iobj.GetValueAt(0));
            Assert.IsNull(iobj.GetValueAt(1));
        }
Ejemplo n.º 2
0
        public void ShouldRunMethods()
        {
            string[] methods =
            {
                "x [^x]",
                "x: newX [x := newX]",
                "y [^y]",
                "y: newY [y := newY]"
            };

            IClass cls = CompileClass(
                "Rectangle",
                new string[] { "x", "y" },
                methods);

            Assert.IsNotNull(cls);

            IObject obj = cls.CreateInstance();

            obj.Send("x:", 10);

            Assert.AreEqual(10, obj.GetValueAt(0));

            obj.Send("y:", 20);

            Assert.AreEqual(20, obj.GetValueAt(1));

            Assert.AreEqual(10, cls.Lookup("x").Execute(obj));
            Assert.AreEqual(20, cls.Lookup("y").Execute(obj));
        }
Ejemplo n.º 3
0
        public void ShouldExecuteInstSizeInRectangle()
        {
            PepsiMachine machine = new PepsiMachine();

            string[] methods =
            {
                "x [^x]",
                "x: newX [x := newX]",
                "y [^y]",
                "y: newY [y := newY]"
            };

            IClass cls = CompileClass(
                "Rectangle",
                new string[] { "x", "y" },
                methods);

            machine.SetGlobalObject("aRectangle", cls.CreateInstance());

            Compiler compiler = new Compiler("^aRectangle instSize");
            Block    block    = compiler.CompileBlock();

            Assert.IsNotNull(block);

            object result = block.Execute(machine, null);

            Assert.AreEqual(2, result);
        }
Ejemplo n.º 4
0
        public void ShouldCreateSubclassInstanceWithVariables()
        {
            IClass baseClass = new BaseClass();

            baseClass.AddVariable("a");

            IClass subClass = (IClass)baseClass.CreateDelegated();

            subClass.AddVariable("b");

            IObject instance = subClass.CreateInstance();

            Assert.IsNotNull(instance);
            Assert.AreEqual(2, instance.Size);
            Assert.IsNull(instance.GetValueAt(0));
            Assert.IsNull(instance.GetValueAt(1));
        }
Ejemplo n.º 5
0
        public IObject CreatePrototype(string prototypeName, string superName, List <string> variableNames)
        {
            IObject   super      = (IObject)this.globals[superName];
            IBehavior superclass = (IBehavior)super.Behavior;
            IClass    cls        = (IClass)superclass.CreateDelegated();

            if (variableNames != null)
            {
                foreach (string name in variableNames)
                {
                    cls.AddVariable(name);
                }
            }

            IObject obj = cls.CreateInstance();

            this.globals[prototypeName] = obj;
            return(obj);
        }
Ejemplo n.º 6
0
        public void ShouldRunMultiCommandMethodWithLocal()
        {
            string[] methods =
            {
                "side: newSide [| temp | temp := newSide. x := temp. y := temp ]"
            };

            IClass cls = CompileClass(
                "Rectangle",
                new string[] { "x", "y" },
                methods);

            Assert.IsNotNull(cls);

            IObject obj = cls.CreateInstance();

            obj.Send("side:", 10);

            Assert.AreEqual(10, obj.GetValueAt(0));
            Assert.AreEqual(10, obj.GetValueAt(1));
        }
Ejemplo n.º 7
0
        public void ShouldExecuteBasicNew()
        {
            PepsiMachine machine = new PepsiMachine();
            IClass       cls     = CompileClass(
                "Rectangle",
                new string[] { "x", "y" },
                null);

            machine.SetGlobalObject("Rectangle", cls.CreateInstance());

            Compiler compiler = new Compiler("^Rectangle class basicNew");
            Block    block    = compiler.CompileBlock();

            Assert.IsNotNull(block);

            object obj = block.Execute(machine, null);

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(IObject));
            Assert.AreEqual(cls, ((IObject)obj).Behavior);
        }
Ejemplo n.º 8
0
        public object Execute(object receiver, params object[] arguments)
        {
            IClass self = (IClass)receiver;

            return(self.CreateInstance());
        }