Ejemplo n.º 1
0
        public void CompileBlockWithParameter()
        {
            ModelParser parser = new ModelParser("[ :a | a doSomething ]");
            this.compiler.CompileExpression(parser.ParseExpression());

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

            object constant = this.block.GetConstant(0);

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

            var newblock = (Block)constant;
            Assert.AreEqual(0, newblock.NoGlobalNames);
            Assert.AreEqual(0, newblock.NoLocals);
            Assert.AreEqual(1, newblock.NoConstants);
            Assert.IsNotNull(newblock.ByteCodes);
            Assert.AreEqual(5, newblock.ByteCodes.Length);
            Assert.AreEqual(1, newblock.Arity);
        }
Ejemplo n.º 2
0
        public void Process(CodeModel model)
        {
            for (string chunk = this.reader.GetChunk(); chunk != null; chunk = this.reader.GetChunk())
            {
                bool isreader = false;

                if (chunk.StartsWith("!"))
                {
                    chunk = chunk.Substring(1);
                    isreader = true;
                }

                ModelParser parser = new ModelParser(chunk);
                IExpression expression = parser.ParseExpression();

                if (isreader)
                {
                    if (expression is MessageExpression && ((MessageExpression)expression).Selector.Contains("methodsFor:"))
                        this.ProcessMethods(model, (MessageExpression)expression);
                    else if (expression is MessageExpression && ((MessageExpression)expression).Selector.Contains("commentStamp:"))
                        this.ProcessComment(model, (MessageExpression)expression);
                    else
                        this.ProcessReader();
                }
                else
                {
                    if (expression is MessageExpression)
                        this.ProcessMessageExpression(model, (MessageExpression)expression);
                }
            }
        }
Ejemplo n.º 3
0
        public void CompileBlock()
        {
            ModelParser parser = new ModelParser("nil ifFalse: [self halt]");
            this.compiler.CompileExpression(parser.ParseExpression());

            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.º 4
0
 public void CompileBlockWithDot()
 {
     Lexer lexer = new Lexer("[. 1. 2]");
     ModelParser parser = new ModelParser(lexer);
     var result = parser.ParseBlock();
     Assert.IsNotNull(result);
 }
Ejemplo n.º 5
0
 public void CompileAndExecuteTwoSimpleCommand()
 {
     ModelParser parser = new ModelParser("a := 1. b := 2");
     this.compiler.CompileExpressions(parser.ParseExpressions());
     Machine machine = new Machine();
     this.block.Execute(machine, null);
     Assert.AreEqual(1, machine.GetGlobalObject("a"));
     Assert.AreEqual(2, machine.GetGlobalObject("b"));
 }
Ejemplo n.º 6
0
 public Block CompileBlock(string text)
 {
     ModelParser parser = new ModelParser(text);
     var expr = parser.ParseBlock();
     Block block = new Block(text);
     BytecodeCompiler compiler = new BytecodeCompiler(block);
     compiler.CompileExpression(expr);
     return block;
 }
Ejemplo n.º 7
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;
 }
        public void CompileExpressions()
        {
            IList<ExpressionResult> results = ExpressionResult.LoadExpressionResults("Expressions.txt");

            foreach (var result in results)
            {
                ModelParser parser = new ModelParser(result.Text);
                Block block = new Block();
                BytecodeCompiler compiler = new BytecodeCompiler(block);
                compiler.CompileExpression(parser.ParseExpression());
                result.ValidateBlock(block);
            }
        }
Ejemplo n.º 9
0
        public void ParseArrayOfArrays()
        {
            ModelParser parser = new ModelParser("#((1 2 3) (1 2 3) (1 2 3))");
            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ArrayExpression));

            ArrayExpression cexpression = (ArrayExpression)expression;
            Assert.AreEqual(3, cexpression.Expressions.Count());

            foreach (IExpression item in cexpression.Expressions)
                Assert.IsInstanceOfType(item, typeof(ArrayExpression));

            Assert.AreEqual("#((1 2 3) (1 2 3) (1 2 3))", expression.AsString());
        }
Ejemplo n.º 10
0
        public void ParseBlockWithLocalVariables()
        {
            ModelParser parser = new ModelParser("[ | x y | ^a + b]");
            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BlockExpression));

            BlockExpression bexpression = (BlockExpression)expression;

            Assert.IsNotNull(bexpression.Body);
            Assert.IsInstanceOfType(bexpression.Body.First(), typeof(ReturnExpression));
            Assert.AreEqual(0, bexpression.ParameterNames.Count);
            Assert.AreEqual(2, bexpression.LocalVariables.Count);
            Assert.AreEqual("x", bexpression.LocalVariables[0]);
            Assert.AreEqual("y", bexpression.LocalVariables[1]);

            Assert.AreEqual("[ | x y | ^a + b]", expression.AsString());
        }
Ejemplo n.º 11
0
        public void ParseAddMessage()
        {
            ModelParser parser = new ModelParser("10 + 20");
            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(MessageExpression));

            MessageExpression mexpression = (MessageExpression)expression;

            Assert.IsInstanceOfType(mexpression.Target, typeof(ConstantExpression));

            ConstantExpression cleftexpression = (ConstantExpression)mexpression.Target;
            Assert.AreEqual(10, cleftexpression.Value);

            Assert.AreEqual("+", mexpression.Selector);
            Assert.AreEqual(1, mexpression.Arguments.Count());
            Assert.IsInstanceOfType(mexpression.Arguments.First(), typeof(ConstantExpression));

            ConstantExpression crightexpression = (ConstantExpression)mexpression.Arguments.First();
            Assert.AreEqual(20, crightexpression.Value);

            Assert.AreEqual("10 + 20", expression.AsString());
        }
Ejemplo n.º 12
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);

            ModelParser parser = new ModelParser("aRectangle basicInstVarAt: 1 put: 200");
            this.compiler.CompileExpression(parser.ParseExpression());

            this.block.Execute(machine, null);

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

            object nil = machine.UndefinedObjectClass;

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

            ModelParser parser = new ModelParser("nil ifNil: [GlobalName := 'foo']");
            this.compiler.CompileExpression(parser.ParseExpression());

            this.block.Execute(machine, null);

            Assert.IsNotNull(machine.GetGlobalObject("GlobalName"));
        }
Ejemplo n.º 14
0
        public void ExecuteBasicInstSize()
        {
            Machine machine = new Machine();

            object nil = machine.UndefinedObjectClass;

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

            ModelParser parser = new ModelParser("^UndefinedObject new basicInstSize");
            this.compiler.CompileExpression(parser.ParseExpression());

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

            Assert.AreEqual(0, result);
        }
Ejemplo n.º 15
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());

            ModelParser parser = new ModelParser("^aRectangle basicInstSize");
            this.compiler.CompileExpression(parser.ParseExpression());

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

            Assert.AreEqual(2, result);
        }
Ejemplo n.º 16
0
 public void CompileTwoCommandsUsingSemicolon()
 {
     ModelParser parser = new ModelParser("nil invokeWith: 10; invokeWith: 20");
     this.compiler.CompileExpressions(parser.ParseExpressions());
     Assert.IsNotNull(this.block);
     Assert.AreEqual(3, this.block.NoConstants);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(12, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
 }
Ejemplo n.º 17
0
        public void CompileTwoSimpleCommand()
        {
            ModelParser parser = new ModelParser("a := 1. b := 2");
            this.compiler.CompileExpressions(parser.ParseExpressions());

            Assert.IsNotNull(this.block);
            Assert.AreEqual(2, this.block.NoConstants);
            Assert.AreEqual(0, this.block.NoLocals);
            Assert.AreEqual(2, this.block.NoGlobalNames);
            Assert.IsNotNull(this.block.ByteCodes);
            Assert.AreEqual(8, this.block.ByteCodes.Length);
            Assert.AreEqual(0, this.block.Arity);
            BlockDecompiler decompiler = new BlockDecompiler(this.block);
            var program = decompiler.Decompile();
            Assert.IsNotNull(program);
            Assert.AreEqual(4, program.Count);
            Assert.AreEqual("GetConstant 1", program[0]);
            Assert.AreEqual("SetGlobalVariable a", program[1]);
            Assert.AreEqual("GetConstant 2", program[2]);
            Assert.AreEqual("SetGlobalVariable b", program[3]);
        }
Ejemplo n.º 18
0
 public void CompileSubClassDefinitionWithInstances()
 {
     ModelParser parser = new ModelParser("nil subclass: #Object instanceVariables: 'a b c'");
     IExpression expr = parser.ParseExpression();
     this.compiler.CompileExpression(expr);
     Assert.IsNotNull(this.block);
     Assert.AreEqual(3, this.block.NoConstants);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(8, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
 }
Ejemplo n.º 19
0
 public void CompileTwoCommands()
 {
     ModelParser parser = new ModelParser("nil invokeWith: 10. Global := 20");
     this.compiler.CompileExpressions(parser.ParseExpressions());
     Assert.IsNotNull(this.block);
     Assert.AreEqual(3, this.block.NoConstants);
     Assert.AreEqual(1, this.block.NoGlobalNames);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(10, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
 }
Ejemplo n.º 20
0
 public void CompileSubClassDefinition()
 {
     ModelParser parser = new ModelParser("nil subclass: #Object");
     IExpression expr = parser.ParseExpression();
     this.compiler.CompileExpression(expr);
     Assert.IsNotNull(this.block);
     Assert.AreEqual(2, this.block.NoConstants);
     Assert.AreEqual(0, this.block.NoGlobalNames);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(6, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
 }
Ejemplo n.º 21
0
 private static MethodModel ParseMethod(string text)
 {
     ModelParser parser = new ModelParser(text);
     ClassModel classModel = new ClassModel("MyClass", (ClassModel)null, null, null, false, null, null);
     return parser.ParseMethod(classModel, false);
 }
Ejemplo n.º 22
0
 public void CompileGlobalVariable()
 {
     ModelParser parser = new ModelParser("AClass");
     IExpression expr = parser.ParseExpression();
     this.compiler.CompileExpression(expr);
     Assert.IsNotNull(this.block);
     Assert.AreEqual(1, this.block.NoGlobalNames);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(2, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
 }
Ejemplo n.º 23
0
 public void CompileIntegerArray()
 {
     ModelParser parser = new ModelParser("#(1 2 3)");
     this.compiler.CompileExpression(parser.ParseExpression());
     Assert.IsNotNull(this.block.ByteCodes);
     BlockDecompiler decompiler = new BlockDecompiler(this.block);
     var ops = decompiler.Decompile();
     Assert.IsNotNull(ops);
     Assert.AreEqual(1, ops.Count);
     Assert.AreEqual("GetConstant System.Object[]", ops[0]);
 }
Ejemplo n.º 24
0
        public void ExecuteTrueIfTrueIfFalse()
        {
            Machine machine = new Machine();

            ModelParser parser = new ModelParser("true ifTrue: [GlobalName := 'bar'] ifFalse: [GlobalName := 'foo']");
            this.compiler.CompileExpression(parser.ParseExpression());

            this.block.Execute(machine, null);

            Assert.AreEqual("bar", machine.GetGlobalObject("GlobalName"));
        }
Ejemplo n.º 25
0
 public void CompileSimpleCommand()
 {
     ModelParser parser = new ModelParser("nil invokeWith: 10");
     IExpression expr = parser.ParseExpression();
     this.compiler.CompileExpression(expr);
     Assert.IsNotNull(this.block);
     Assert.AreEqual(2, this.block.NoConstants);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(6, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
 }
Ejemplo n.º 26
0
 public void CompileSimpleArithmeticWithParenthesis()
 {
     ModelParser parser = new ModelParser("1 * (2 + 3)");
     IExpression expr = parser.ParseExpression();
     this.compiler.CompileExpression(expr);
     Assert.IsNotNull(this.block);
     Assert.AreEqual(5, this.block.NoConstants);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(12, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
     BlockDecompiler decompiler = new BlockDecompiler(this.block);
     var program = decompiler.Decompile();
     Assert.IsNotNull(program);
     Assert.AreEqual(5, program.Count);
     Assert.AreEqual("GetConstant 1", program[0]);
     Assert.AreEqual("GetConstant 2", program[1]);
     Assert.AreEqual("GetConstant 3", program[2]);
     Assert.AreEqual("Send + 1", program[3]);
     Assert.AreEqual("Send * 1", program[4]);
 }
Ejemplo n.º 27
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;
        }
Ejemplo n.º 28
0
 public void CompileSimpleCommandWithParenthesisAndBangKeyword()
 {
     ModelParser parser = new ModelParser("a := b with: (anObject !nativeMethod: 1)");
     IExpression expr = parser.ParseExpression();
     this.compiler.CompileExpression(expr);
     Assert.IsNotNull(this.block);
     Assert.AreEqual(3, this.block.NoGlobalNames);
     Assert.AreEqual(3, this.block.NoConstants);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(14, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
 }
Ejemplo n.º 29
0
 private static IExpression ParseExpression(string text)
 {
     ModelParser parser = new ModelParser(text);
     return parser.ParseExpression();
 }
Ejemplo n.º 30
0
 public void CompileSimpleSum()
 {
     ModelParser parser = new ModelParser("1 + 2");
     IExpression expr = parser.ParseExpression();
     this.compiler.CompileExpression(expr);
     Assert.IsNotNull(this.block);
     Assert.AreEqual(3, this.block.NoConstants);
     Assert.AreEqual(0, this.block.NoLocals);
     Assert.IsNotNull(this.block.ByteCodes);
     Assert.AreEqual(7, this.block.ByteCodes.Length);
     Assert.AreEqual(0, this.block.Arity);
     BlockDecompiler decompiler = new BlockDecompiler(this.block);
     var program = decompiler.Decompile();
     Assert.IsNotNull(program);
     Assert.AreEqual(3, program.Count);
     Assert.AreEqual("GetConstant 1", program[0]);
     Assert.AreEqual("GetConstant 2", program[1]);
     Assert.AreEqual("Send + 1", program[2]);
 }