Ejemplo n.º 1
0
        public EnumerableBehavior(IBehavior behavior, IBehavior superclass, Machine machine)
            : base(behavior, superclass, machine, typeof(IEnumerable))
        {
            string dosource = @"
            do: aBlock
            | enumerator |

            enumerator := self !GetEnumerator.

            [enumerator !MoveNext] whileTrue:
            [ aBlock value: enumerator !Current ]
            ";
            string selectsource = @"
            select: aBlock
            | enumerator list |

            enumerator := self !GetEnumerator.
            list := @System.Collections.ArrayList !new.

            [enumerator !MoveNext] whileTrue:
            [ | item |
              item := enumerator !Current.
            (aBlock value: item) ifTrue:  [ list add: item ]
            ].
            ^list
            ";

            Parser parser = new Parser(dosource);
            this.DefineInstanceMethod(parser.CompileInstanceMethod(this));
            parser = new Parser(selectsource);
            this.DefineInstanceMethod(parser.CompileInstanceMethod(this));
            this.DefineInstanceMethod(new FunctionalMethod("includes:", this, this.IncludesMethod));
        }
Ejemplo n.º 2
0
        public void CompileBlock()
        {
            Parser compiler = new Parser("nil ifFalse: [self halt]");
            Block block = compiler.CompileBlock();

            Assert.IsNotNull(block);
            Assert.AreEqual(1, block.NoConstants);
            Assert.AreEqual(0, block.NoLocals);
            Assert.IsNotNull(block.ByteCodes);
            Assert.AreEqual(11, block.ByteCodes.Length);
            Assert.AreEqual(0, block.Arity);

            object constant = block.GetConstant(0);

            Assert.IsNotNull(constant);
            Assert.IsInstanceOfType(constant, typeof(Block));

            var newblock = (Block)constant;
            Assert.AreEqual(0, newblock.Arity);
            Assert.AreEqual(0, newblock.NoLocals);
            Assert.IsNotNull(newblock.ByteCodes);
            Assert.AreEqual(4, newblock.ByteCodes.Length);

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

            Assert.IsNotNull(result);
            Assert.AreEqual("GetNil", result[0]);
            Assert.AreEqual("JumpIfFalse 8", result[1]);
            Assert.AreEqual("GetNil", result[2]);
            Assert.AreEqual("Jump 11", result[3]);
            Assert.AreEqual("GetBlock { GetSelf; Send halt 0 }", result[4]);
        }
Ejemplo n.º 3
0
 protected override object Evaluate(string text, Machine machine)
 {
     Parser parser = new Parser(text);
     Block block = parser.CompileBlock();
     Process process = new Process(block, null, machine);
     return process.Execute();
 }
Ejemplo n.º 4
0
 public void CompileAndExecuteTwoSimpleCommand()
 {
     Parser compiler = new Parser("a := 1. b := 2");
     Block block = compiler.CompileBlock();
     Machine machine = new Machine();
     block.Execute(machine, null);
     Assert.AreEqual(1, machine.GetGlobalObject("a"));
     Assert.AreEqual(2, machine.GetGlobalObject("b"));
 }
Ejemplo n.º 5
0
        public void CompileExpressions()
        {
            IList<ExpressionResult> results = ExpressionResult.LoadExpressionResults("Expressions.txt");

            foreach (var result in results)
            {
                Parser parser = new Parser(result.Text);
                var block = parser.CompileBlock();
                Assert.IsNotNull(block);
                result.ValidateBlock(block);
            }
        }
Ejemplo n.º 6
0
 public BlockBehavior(IBehavior behavior, IBehavior superclass, Machine machine)
     : base(behavior, superclass, machine, typeof(Block))
 {
     string assertcode = @"
     assert
     self value ifFalse: [@AjTalk.Exceptions.AssertError new raise].
     ^true
     ";
     Parser parser = new Parser(assertcode);
     this.DefineInstanceMethod(parser.CompileInstanceMethod(this));
     ////this.DefineInstanceMethod(new FunctionalMethod("assert", this, this.AssertMethod));
 }
Ejemplo n.º 7
0
        public BooleanBehavior(IBehavior behavior, IBehavior superclass, Machine machine)
            : base(behavior, superclass, machine, typeof(bool))
        {
            string iffalseiftruesource = @"
            ifFalse: falseBlock ifTrue: trueBlock
            self ifFalse: [^falseBlock value].
            ^trueBlock value.
            ";
            string iftrueiffalsesource = @"
            ifTrue: trueBlock ifFalse: falseBlock
            self ifFalse: [^falseBlock value].
            ^trueBlock value.
            ";

            Parser parser = new Parser(iffalseiftruesource);
            this.DefineInstanceMethod(parser.CompileInstanceMethod(this));
            parser = new Parser(iftrueiffalsesource);
            this.DefineInstanceMethod(parser.CompileInstanceMethod(this));
        }
Ejemplo n.º 8
0
        private void DoIt()
        {
            if (String.IsNullOrEmpty(txtInput.SelectedText))
                return;

            string text = txtInput.SelectedText;
            Parser parser = new Parser(text);

            try
            {
                Block block = parser.CompileBlock();
                object result = block.Execute(this.machine, null);
                if (result != null)
                    MessageBox.Show(result.ToString(), "Result");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 9
0
        private static void ProcessLine(string [] words, StreamReader reader)
        {
            if (words[0].Length == 0)
                return;

            Console.WriteLine(words[0]);

            if (words[0] == "class")
            {
                if (words.Length > 2)
                    cls = new BaseClass(words[1], (IClass) objects[words[2]], machine);
                else
                    cls = new BaseClass(words[1], machine);

                objects[words[1]]=cls;
            }
            else if (words[0] == "variables")
            {
                for (int k = 1; k < words.Length; k++)
                    cls.DefineInstanceVariable(words[k]);
            }
            else if (words[0] == "method")
            {
                string line;
                string body = "";

                line = reader.ReadLine();

                while (line != null && line != "")
                {
                    body += line;
                    body += " ";
                    line = reader.ReadLine();
                }

                Parser compiler = new Parser(body);
                compiler.CompileInstanceMethod(cls);
            }
            else
                Console.WriteLine("Unknown {0}", words[0]);
        }
Ejemplo n.º 10
0
        public object Evaluate(string command)
        {
            Machine current = Machine.Current;
            object result = null;

            try
            {
                this.machine.SetCurrent();
                Parser parser = new Parser(command);

                Block block = parser.CompileBlock();

                if (block != null)
                    result = block.Execute(this.machine, null);

                return result;
            }
            finally
            {
                Machine.SetCurrent(current);
            }
        }
Ejemplo n.º 11
0
 public void CompileTwoSimpleCommand()
 {
     Parser compiler = new Parser("a := 1. b := 2");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(2, block.NoConstants);
     Assert.AreEqual(0, block.NoLocals);
     Assert.AreEqual(2, block.NoGlobalNames);
     Assert.IsNotNull(block.ByteCodes);
     Assert.AreEqual(8, block.ByteCodes.Length);
     Assert.AreEqual(0, block.Arity);
 }
Ejemplo n.º 12
0
 public void CompileTwoCommandsUsingSemicolon()
 {
     Parser compiler = new Parser("nil invokeWith: 10; invokeWith: 20");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(3, block.NoConstants);
     Assert.AreEqual(0, block.NoLocals);
     Assert.IsNotNull(block.ByteCodes);
     Assert.AreEqual(12, block.ByteCodes.Length);
     Assert.AreEqual(0, block.Arity);
 }
Ejemplo n.º 13
0
 public void CompileTwoCommands()
 {
     Parser compiler = new Parser("nil invokeWith: 10. Global := 20");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(3, block.NoConstants);
     Assert.AreEqual(1, block.NoGlobalNames);
     Assert.AreEqual(0, block.NoLocals);
     Assert.IsNotNull(block.ByteCodes);
     Assert.AreEqual(10, block.ByteCodes.Length);
     Assert.AreEqual(0, block.Arity);
 }
Ejemplo n.º 14
0
 public void CompileSymbolArray()
 {
     Parser parser = new Parser("#(a b c)");
     var result = parser.CompileBlock();
     Assert.IsNotNull(result);
     Assert.IsNotNull(result.ByteCodes);
     BlockDecompiler decompiler = new BlockDecompiler(result);
     var ops = decompiler.Decompile();
     Assert.IsNotNull(ops);
     Assert.AreEqual(1, ops.Count);
     Assert.IsTrue(ops[0].StartsWith("GetConstant "));
 }
Ejemplo n.º 15
0
 public void CompileSimpleExpressionInParenthesisUsingYourself()
 {
     Parser compiler = new Parser("(1+2;yourself)");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(0, block.NoLocals);
     Assert.AreEqual(4, block.NoConstants);
     Assert.AreEqual(0, block.NoGlobalNames);
     BlockDecompiler decompiler = new BlockDecompiler(block);
     var result = decompiler.Decompile();
     Assert.IsNotNull(result);
     Assert.AreEqual(5, result.Count);
     Assert.AreEqual("GetConstant 1", result[0]);
     Assert.AreEqual("GetConstant 2", result[1]);
     Assert.AreEqual("Send + 1", result[2]);
     Assert.AreEqual("ChainedSend", result[3]);
     Assert.AreEqual("Send yourself 0", result[4]);
 }
Ejemplo n.º 16
0
        public void ExecuteBasicInstVarAtPut()
        {
            Machine machine = new Machine();
            IClass cls = CompileClass(
                "Rectangle",
                new string[] { "x", "y" },
                new string[] { "x ^x", "x: newX x := newX", "y ^y", "y: newY y := newY" });

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

            machine.SetGlobalObject("aRectangle", iobj);

            Parser compiler = new Parser("aRectangle basicInstVarAt: 1 put: 200");
            Block block = compiler.CompileBlock();

            Assert.IsNotNull(block);

            block.Execute(machine, null);

            Assert.AreEqual(200, iobj[0]);
            Assert.IsNull(iobj[1]);
        }
Ejemplo n.º 17
0
        public void ExecuteBasicInstSize()
        {
            Machine machine = new Machine();

            object nil = machine.UndefinedObjectClass;

            Assert.IsNotNull(nil);
            Assert.IsInstanceOfType(nil, typeof(IClass));

            Parser compiler = new Parser("^UndefinedObject new basicInstSize");
            Block block = compiler.CompileBlock();

            Assert.IsNotNull(block);

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

            Assert.AreEqual(0, result);
        }
Ejemplo n.º 18
0
 public void CompileSubClassDefinition()
 {
     Parser compiler = new Parser("nil subclass: #Object");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(2, block.NoConstants);
     Assert.AreEqual(0, block.NoGlobalNames);
     Assert.AreEqual(0, block.NoLocals);
     Assert.IsNotNull(block.ByteCodes);
     Assert.AreEqual(6, block.ByteCodes.Length);
     Assert.AreEqual(0, block.Arity);
 }
Ejemplo n.º 19
0
 public void CompileSimpleSetExpression()
 {
     Parser parser = new Parser("a := 1");
     var result = parser.CompileBlock();
     Assert.IsNotNull(result);
     Assert.IsNotNull(result.ByteCodes);
     Assert.AreEqual(ByteCode.SetGlobalVariable, (ByteCode)result.ByteCodes[2]);
     BlockDecompiler decompiler = new BlockDecompiler(result);
     var ops = decompiler.Decompile();
     Assert.IsNotNull(ops);
     Assert.AreEqual("SetGlobalVariable a", ops[1]);
 }
Ejemplo n.º 20
0
 public void CompileSimpleSendToSelf()
 {
     Parser compiler = new Parser("self invokeWith: 10");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(2, block.NoConstants);
     Assert.AreEqual(0, block.NoLocals);
     Assert.IsNotNull(block.ByteCodes);
     Assert.AreEqual(6, block.ByteCodes.Length);
     Assert.AreEqual(ByteCode.GetSelf, (ByteCode)block.ByteCodes[0]);
     Assert.AreEqual(0, block.Arity);
 }
Ejemplo n.º 21
0
 public void CompileSimpleRealSum()
 {
     Parser compiler = new Parser("1.2 + 3.4");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(3, block.NoConstants);
     Assert.AreEqual(0, block.NoLocals);
     Assert.IsNotNull(block.ByteCodes);
     Assert.AreEqual(7, block.ByteCodes.Length);
     Assert.AreEqual(0, block.Arity);
 }
Ejemplo n.º 22
0
 public void CompileSimpleOr()
 {
     Parser parser = new Parser("1 | 0");
     var result = parser.CompileBlock();
     Assert.IsNotNull(result);
     Assert.IsNotNull(result.ByteCodes);
     BlockDecompiler decompiler = new BlockDecompiler(result);
     var ops = decompiler.Decompile();
     Assert.IsNotNull(ops);
     Assert.AreEqual(3, ops.Count);
     Assert.AreEqual("GetConstant 1", ops[0]);
     Assert.AreEqual("GetConstant 0", ops[1]);
     Assert.AreEqual("Send | 1", ops[2]);
 }
Ejemplo n.º 23
0
 public void CompileWhileTrue()
 {
     Parser compiler = new Parser(":arg | [arg < 3] whileTrue: [arg := arg + 1]");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(13, block.Bytecodes.Length);
     var decompiler = new BlockDecompiler(block);
     var steps = decompiler.Decompile();
     Assert.IsNotNull(steps);
     Assert.AreEqual(7, steps.Count);
     Assert.AreEqual("GetBlock { GetArgument arg; GetConstant 3; Send < 1 }", steps[0]);
     Assert.AreEqual("Value", steps[1]);
     Assert.AreEqual("JumpIfFalse 13", steps[2]);
     Assert.AreEqual("GetBlock { GetArgument arg; GetConstant 1; Send + 1; SetArgument arg }", steps[3]);
     Assert.AreEqual("Value", steps[4]);
     Assert.AreEqual("Pop", steps[5]);
     Assert.AreEqual("Jump 0", steps[6]);
 }
Ejemplo n.º 24
0
 public void CompileSubClassDefinitionWithInstances()
 {
     Parser compiler = new Parser("nil subclass: #Object instanceVariables: 'a b c'");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(3, block.NoConstants);
     Assert.AreEqual(0, block.NoLocals);
     Assert.IsNotNull(block.ByteCodes);
     Assert.AreEqual(8, block.ByteCodes.Length);
     Assert.AreEqual(0, block.Arity);
 }
Ejemplo n.º 25
0
        public void Create()
        {
            Parser compiler = new Parser("x ^x");

            Assert.IsNotNull(compiler);
        }
Ejemplo n.º 26
0
 public void CompileSumWithNegativeNumber()
 {
     Parser parser = new Parser("-1 + 0");
     var result = parser.CompileBlock();
     Assert.IsNotNull(result);
     Assert.IsNotNull(result.ByteCodes);
     BlockDecompiler decompiler = new BlockDecompiler(result);
     var ops = decompiler.Decompile();
     Assert.IsNotNull(ops);
     Assert.AreEqual(4, ops.Count);
     Assert.AreEqual("GetConstant 1", ops[0]);
     Assert.AreEqual("Send minus 0", ops[1]);
     Assert.AreEqual("GetConstant 0", ops[2]);
     Assert.AreEqual("Send + 1", ops[3]);
 }
Ejemplo n.º 27
0
        public void ExecuteBasicInstSizeInRectangle()
        {
            Machine machine = new Machine();
            IClass cls = CompileClass(
                "Rectangle",
                new string[] { "x", "y" },
                new string[] { "x ^x", "x: newX x := newX", "y ^y", "y: newY y := newY" });

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

            Parser compiler = new Parser("^aRectangle basicInstSize");
            Block block = compiler.CompileBlock();

            Assert.IsNotNull(block);

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

            Assert.AreEqual(2, result);
        }
Ejemplo n.º 28
0
 public void CompileSuperNew()
 {
     Parser compiler = new Parser("new super new");
     Block block = compiler.CompileInstanceMethod(null);
     Assert.IsNotNull(block);
     var decompiler = new BlockDecompiler(block);
     var steps = decompiler.Decompile();
     Assert.IsNotNull(steps);
     Assert.AreEqual(2, steps.Count);
     Assert.AreEqual("GetSuper", steps[0]);
     Assert.AreEqual("Send new 0", steps[1]);
 }
Ejemplo n.º 29
0
 public void CompileSimpleSum()
 {
     Parser compiler = new Parser("1 + 2");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(0, block.NoLocals);
     Assert.IsNotNull(block.ByteCodes);
     Assert.AreEqual(7, block.ByteCodes.Length);
     Assert.AreEqual(0, block.Arity);
     BlockDecompiler decompiler = new BlockDecompiler(block);
     var result = decompiler.Decompile();
     Assert.IsNotNull(result);
     Assert.AreEqual(3, result.Count);
     Assert.AreEqual("GetConstant 1", result[0]);
     Assert.AreEqual("GetConstant 2", result[1]);
     Assert.AreEqual("Send + 1", result[2]);
 }
Ejemplo n.º 30
0
 public void CompileSimpleExpressionInParenthesis()
 {
     Parser compiler = new Parser("(1+2)");
     Block block = compiler.CompileBlock();
     Assert.IsNotNull(block);
     Assert.AreEqual(0, block.NoLocals);
     Assert.AreEqual(3, block.NoConstants);
     Assert.AreEqual(0, block.NoGlobalNames);
     BlockDecompiler decompiler = new BlockDecompiler(block);
     var result = decompiler.Decompile();
     Assert.IsNotNull(result);
     Assert.AreEqual(3, result.Count);
     Assert.AreEqual("GetConstant 1", result[0]);
     Assert.AreEqual("GetConstant 2", result[1]);
     Assert.AreEqual("Send + 1", result[2]);
 }