Inheritance: IBlock
示例#1
0
        private ExecutionContext(Block block, object[] arguments)
        {
            this.block = block;
            this.Stack = new ArrayList(5);

            this.arguments = arguments;
            if (this.block.NoLocals > 0)
                this.locals = new object[this.block.NoLocals];
            else
                this.locals = null;

            // TODO refactor to no copy of arguments and locals
            this.arguments = arguments;

            if (block.Closure != null)
            {
                this.self = block.Closure.Self;
                this.nativeSelf = block.Closure.NativeSelf;

                int nlocs = block.NoLocals - block.Closure.NoLocals;

                if (nlocs > 0)
                    this.locals = new object[nlocs];
                else
                    this.locals = null;
            }
            else
            {
                if (this.block.NoLocals > 0)
                    this.locals = new object[this.block.NoLocals];
                else
                    this.locals = null;
            }
        }
示例#2
0
 public ExecutionContext(Machine machine, IObject self, Block block, object[] arguments)
     : this(block, arguments)
 {
     // this.self = receiver; // TODO review
     this.machine = machine;
     this.self = self;
 }
示例#3
0
        public void BeCreated()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("TestClass");
            IBlock block = new Block();

            Assert.IsNotNull(block);
        }
示例#4
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;
 }
示例#5
0
        public void NewProcess()
        {
            Machine machine = new Machine();
            Block block = new Block();

            Process process = new Process(block, null, machine);

            Assert.AreSame(machine, process.Machine);
            Assert.AreSame(block, process.Block);
            Assert.IsNull(process.Arguments);
        }
示例#6
0
        public void DecompileGetIntegerConstantAsString()
        {
            Block block = new Block();
            block.CompileGetConstant(1);
            BlockDecompiler decompiler = new BlockDecompiler(block);

            var result = decompiler.DecompileAsString();

            Assert.IsNotNull(result);
            Assert.AreEqual("{ GetConstant 1 }", result);
        }
示例#7
0
        public void ValidateBlock(Block block)
        {
            BlockDecompiler decompiler = new BlockDecompiler(block);

            var result = decompiler.Decompile();

            Assert.IsNotNull(result);
            Assert.AreEqual(this.compiled.Count, result.Count, this.text);

            for (int k = 0; k < this.compiled.Count; k++)
                Assert.AreEqual(this.compiled[k], result[k], this.text);
        }
示例#8
0
        public void DecompileGetIntegerConstant()
        {
            Block block = new Block();
            block.CompileGetConstant(1);
            BlockDecompiler decompiler = new BlockDecompiler(block);

            var result = decompiler.Decompile();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("GetConstant 1", result[0]);
        }
示例#9
0
        public void DecompileGetGlobalVariable()
        {
            Block block = new Block();
            block.CompileGet("foo");
            BlockDecompiler decompiler = new BlockDecompiler(block);

            var result = decompiler.Decompile();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("GetGlobalVariable foo", result[0]);
        }
        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);
            }
        }
示例#11
0
        public void CompileAndExecuteGetDotNetType()
        {
            Block block;

            block = new Block();
            block.CompileGetDotNetType("System.IO.FileInfo");
            block.CompileByteCode(ByteCode.ReturnPop);

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

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(System.Type));
        }
示例#12
0
        public void DecompileGetConstants()
        {
            Block block = new Block();
            block.CompileGetConstant(1);
            block.CompileGetConstant("foo");
            BlockDecompiler decompiler = new BlockDecompiler(block);

            var result = decompiler.Decompile();

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("GetConstant 1", result[0]);
            Assert.AreEqual("GetConstant \"foo\"", result[1]);
        }
示例#13
0
文件: Parser.cs 项目: ajlopez/AjTalk
        public Block CompileBlock()
        {
            this.block = new Block(this.source, this.outer);
            this.CompileBlockArguments();
            this.CompileLocals();

            // TODO review why to use this.locals instead of this.block.CompileLocal directly
            foreach (string argname in this.arguments)
                this.block.CompileArgument(argname);

            foreach (string locname in this.locals)
                this.block.CompileLocal(locname);

            this.CompileBody();

            return this.block;
        }
示例#14
0
 public void CreateAndInvokeAgentUsingInterpreter()
 {
     Machine machine = new Machine();
     Block block = new Block();
     AjTalk.Language.ExecutionContext context = new AjTalk.Language.ExecutionContext(machine, null, block, null);
     Interpreter interpreter = new Interpreter(context);
     ManualResetEvent handle = new ManualResetEvent(false);
     bool executed = false;
     AgentObject agent = new AgentObject();
     agent.ExecuteMethod(
         interpreter,
         new FunctionalMethod((x, y, args) =>
         {
             executed = true;
             return handle.Set();
         }),
     null);
     handle.WaitOne();
     Assert.IsTrue(executed);
 }
示例#15
0
        public void CompilePharoCoreRectangle()
        {
            ChunkReader chunkCoreReader = new ChunkReader(@"PharoCoreKernelObjects.st");
            CodeReader coreReader = new CodeReader(chunkCoreReader);
            ChunkReader chunkReader = new ChunkReader(@"PharoCoreRectangle.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            coreReader.Process(model);
            reader.Process(model);

            foreach (var element in model.Elements)
            {
                Block block = new Block();
                BytecodeCompiler compiler = new BytecodeCompiler(block);
                element.Visit(compiler);

                if (element is MethodModel && ((MethodModel)element).Body == null)
                    continue;

                Assert.IsNotNull(block.ByteCodes);
            }
        }
示例#16
0
文件: Block.cs 项目: ajlopez/AjTalk
 public Block(string sourcecode, Block outer)
 {
     this.sourcecode = sourcecode;
     this.outer = outer;
 }
示例#17
0
文件: Parser.cs 项目: ajlopez/AjTalk
        private void CompileMethod(IBehavior cls)
        {
            this.CompileArguments();
            this.CompileLocals();

            this.block = new Method(cls, this.methodname, this.source);

            foreach (string argname in this.arguments)
            {
                this.block.CompileArgument(argname);
            }

            foreach (string locname in this.locals)
            {
                this.block.CompileLocal(locname);
            }

            this.CompileBody();
        }
示例#18
0
        public void CompileJumpAt()
        {
            Block block = new Block();

            block.CompileByteCode((ByteCode)1);
            block.CompileByteCode((ByteCode)2);
            block.CompileByteCode((ByteCode)3);

            block.CompileInsert(1, 3);
            block.CompileJumpByteCodeAt(ByteCode.Jump, 10, 1);

            Assert.AreEqual(6, block.Bytecodes.Length);
            Assert.AreEqual(1, block.Bytecodes[0]);
            Assert.AreEqual((byte)ByteCode.Jump, block.Bytecodes[1]);
            Assert.AreEqual(0, block.Bytecodes[2]);
            Assert.AreEqual(10, block.Bytecodes[3]);
            Assert.AreEqual(2, block.Bytecodes[4]);
            Assert.AreEqual(3, block.Bytecodes[5]);
        }
示例#19
0
 public ExecutionContext(Machine machine, object nativeself, Block block, object[] arguments)
     : this(block, arguments)
 {
     this.machine = machine;
     this.nativeSelf = nativeself;
 }
示例#20
0
        public override void Visit(BlockExpression expression)
        {
            Block newblock = new Block(null, this.block);

            // TODO Review is the copy of argument and local names is needed
            foreach (var parname in this.block.ParameterNames)
                newblock.CompileArgument(parname);

            foreach (var locname in this.block.LocalNames)
                newblock.CompileLocal(locname);

            if (expression.ParameterNames != null)
                foreach (var parname in expression.ParameterNames)
                    newblock.CompileArgument(parname);

            if (expression.LocalVariables != null)
                foreach (var locname in expression.LocalVariables)
                    newblock.CompileLocal(locname);

            var compiler = new BytecodeCompiler(newblock);
            compiler.Visit(expression.Body);
            this.block.CompileGetBlock(newblock);
        }
示例#21
0
 public BytecodeCompiler(Block block)
 {
     this.block = block;
 }
示例#22
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);
        }
示例#23
0
        public void CompileJumps()
        {
            Block block = new Block();

            block.CompileJumpByteCode(ByteCode.Jump, 10);
            block.CompileJumpByteCode(ByteCode.JumpIfFalse, 256);
            block.CompileJumpByteCode(ByteCode.JumpIfTrue, 1024);

            Assert.AreEqual(9, block.Bytecodes.Length);
            Assert.AreEqual((byte)ByteCode.Jump, block.Bytecodes[0]);
            Assert.AreEqual(0, block.Bytecodes[1]);
            Assert.AreEqual(10, block.Bytecodes[2]);
            Assert.AreEqual((byte)ByteCode.JumpIfFalse, block.Bytecodes[3]);
            Assert.AreEqual(1, block.Bytecodes[4]);
            Assert.AreEqual(0, block.Bytecodes[5]);
            Assert.AreEqual((byte)ByteCode.JumpIfTrue, block.Bytecodes[6]);
            Assert.AreEqual(4, block.Bytecodes[7]);
            Assert.AreEqual(0, block.Bytecodes[8]);
        }
示例#24
0
        public void CompileAndRun()
        {
            Machine machine = new Machine();

            Block block;

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

            block.Execute(machine, new object[] { 10 });

            Assert.AreEqual(10, machine.GetGlobalObject("GlobalX"));
        }
示例#25
0
        public void ExecuteEmptyBlock()
        {
            Block block;

            block = new Block();

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

            Assert.IsNull(obj);
        }
示例#26
0
 public void Setup()
 {
     this.block = new Block();
     this.compiler = new BytecodeCompiler(this.block);
 }
示例#27
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"));
        }
示例#28
0
 public void GetInstanceVariableOffset()
 {
     Block block = new Block();
     Assert.AreEqual(-1, block.GetInstanceVariableOffset("x"));
 }
示例#29
0
        internal static void CompileFile(string filename)
        {
            ChunkReader chunkReader = new ChunkReader(filename);
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            foreach (var element in model.Elements)
            {
                Block block = new Block();
                BytecodeCompiler compiler = new BytecodeCompiler(block);
                element.Visit(compiler);
                if (element is MethodModel && ((MethodModel)element).Body == null)
                    continue;
                Assert.IsNotNull(block.ByteCodes);
            }
        }
示例#30
0
        public void CompileAndExecuteNewDotNetObject()
        {
            Block block;

            block = new Block();
            block.CompileGetDotNetType("System.IO.FileInfo");
            block.CompileGetConstant("FooBar.txt");
            block.CompileSend("!new:");
            block.CompileByteCode(ByteCode.ReturnPop);

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

            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(obj, typeof(System.IO.FileInfo));
        }