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; } }
public ExecutionContext(Machine machine, IObject self, Block block, object[] arguments) : this(block, arguments) { // this.self = receiver; // TODO review this.machine = machine; this.self = self; }
public void BeCreated() { Machine machine = new Machine(); IClass cls = machine.CreateClass("TestClass"); IBlock block = new Block(); Assert.IsNotNull(block); }
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; }
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); }
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); }
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); }
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]); }
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); } }
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)); }
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]); }
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; }
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); }
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); } }
public Block(string sourcecode, Block outer) { this.sourcecode = sourcecode; this.outer = outer; }
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(); }
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]); }
public ExecutionContext(Machine machine, object nativeself, Block block, object[] arguments) : this(block, arguments) { this.machine = machine; this.nativeSelf = nativeself; }
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); }
public BytecodeCompiler(Block block) { this.block = block; }
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); }
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]); }
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")); }
public void ExecuteEmptyBlock() { Block block; block = new Block(); object obj = block.Execute((Machine)null, null); Assert.IsNull(obj); }
public void Setup() { this.block = new Block(); this.compiler = new BytecodeCompiler(this.block); }
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")); }
public void GetInstanceVariableOffset() { Block block = new Block(); Assert.AreEqual(-1, block.GetInstanceVariableOffset("x")); }
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); } }
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)); }