ParseExpression() public method

public ParseExpression ( ) : IExpression
return IExpression
Example #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);
        }
Example #2
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]);
        }
Example #3
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);
                }
            }
        }
        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);
            }
        }
Example #5
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());
        }
Example #6
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());
        }
Example #7
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());
        }
Example #8
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);
 }
Example #9
0
        public void ParseSet()
        {
            ModelParser parser = new ModelParser("a := b + c");
            IExpression expression = parser.ParseExpression();

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

            SetExpression sexpression = (SetExpression)expression;
            Assert.AreEqual("a", sexpression.LeftValue.Name);
        }
Example #10
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);
 }
Example #11
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]);
 }
Example #12
0
        public void ParseTwoFluentExpressions()
        {
            ModelParser parser = new ModelParser("self do: 1 with: 2; do: 2 with: 3; do: 3 with: 4");
            IExpression expression = parser.ParseExpression();

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

            MessageExpression mexpression = (MessageExpression)expression;
            Assert.IsInstanceOfType(mexpression.Target, typeof(FluentMessageExpression));
            Assert.IsNull(parser.ParseExpression());
            Assert.AreEqual("self do: 1 with: 2; do: 2 with: 3; do: 3 with: 4", expression.AsString());
        }
Example #13
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);
 }
Example #14
0
        public void ParseSymbol()
        {
            ModelParser parser = new ModelParser("#foo");
            IExpression expression = parser.ParseExpression();

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

            SymbolExpression sexpression = (SymbolExpression)expression;
            Assert.AreEqual("foo", sexpression.Symbol);

            Assert.AreEqual("#foo", expression.AsString());
        }
Example #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);
        }
Example #16
0
        public void ParseSymbolAndStringArray()
        {
            ModelParser parser = new ModelParser("#('option1' do1:)");
            IExpression expression = parser.ParseExpression();

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

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

            Assert.IsInstanceOfType(cexpression.Expressions.First(), typeof(ConstantExpression));
            Assert.IsInstanceOfType(cexpression.Expressions.Skip(1).First(), typeof(SymbolExpression));

            Assert.AreEqual("#('option1' do1:)", expression.AsString());
        }
Example #17
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"));
        }
Example #18
0
        public void ParseVariable()
        {
            ModelParser parser = new ModelParser("foo");
            IExpression expression = parser.ParseExpression();

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

            VariableExpression vexpression = (VariableExpression)expression;
            Assert.AreEqual("foo", vexpression.Name);

            Assert.AreEqual("foo", expression.AsString());
        }
Example #19
0
        public void ParseUnaryMinusExpression()
        {
            ModelParser parser = new ModelParser("-3");
            IExpression expression = parser.ParseExpression();
            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(MessageExpression));

            MessageExpression mexpression = (MessageExpression)expression;
            Assert.IsTrue(mexpression.IsUnaryMessage);

            Assert.AreEqual("-3", expression.AsString());
        }
Example #20
0
        public void ParseUnaryMessage()
        {
            ModelParser parser = new ModelParser("x length");
            IExpression expression = parser.ParseExpression();

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

            MessageExpression mexpression = (MessageExpression)expression;

            Assert.IsInstanceOfType(mexpression.Target, typeof(VariableExpression));
            Assert.AreEqual("length", mexpression.Selector);
            Assert.AreEqual(0, mexpression.Arguments.Count());

            Assert.AreEqual("x length", expression.AsString());
        }
Example #21
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);
 }
Example #22
0
        public void ParseSimpleBlock()
        {
            ModelParser parser = new ModelParser("[ a := 1. b := 2]");
            IExpression expression = parser.ParseExpression();

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

            BlockExpression bexpression = (BlockExpression)expression;

            Assert.IsNotNull(bexpression.Body);
            Assert.AreEqual(2, bexpression.Body.Count());
            Assert.AreEqual(0, bexpression.ParameterNames.Count);
            Assert.AreEqual(0, bexpression.LocalVariables.Count);

            Assert.AreEqual("[a := 1. b := 2]", expression.AsString());
        }
Example #23
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);
        }
Example #24
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]);
 }
Example #25
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]);
        }
Example #26
0
        public void ParseTrue()
        {
            ModelParser parser = new ModelParser("true");
            IExpression expression = parser.ParseExpression();

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

            ConstantExpression cexpression = (ConstantExpression)expression;
            Assert.AreEqual(true, cexpression.Value);

            Assert.AreEqual("true", expression.AsString());
        }
Example #27
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"));
        }
Example #28
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);
 }
Example #29
0
 private static IExpression ParseExpression(string text)
 {
     ModelParser parser = new ModelParser(text);
     return parser.ParseExpression();
 }
Example #30
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]);
 }