public static void New() { CompilationScope mainScope = new CompilationScope { instructions = new Instructions { }, lastInstruction = new EmittedInstruction { }, previousInstruction = new EmittedInstruction { }, }; symbol_table.SymbolTable symbolTable = symbol_table.NewSymbolTable(); for (int i = 0; i < Object.builtins.Builtins.Length; i++) { Object._BuiltinDefinition v = Object.builtins.Builtins[i]; symbol_table.DefineBuiltin(ref symbolTable, i, v.Name); } compiler = new Compiler_t { constants = new List <Object.Object>(), symbolTable = symbolTable, scopes = new List <CompilationScope> { mainScope }, scopeIndex = 0, }; }
static void replaceLastPopWithReturn() { int lastPos = compiler.scopes[compiler.scopeIndex].lastInstruction.Position; replaceInstruction(lastPos, code.Make(Opcode.OpReturnValue)); CompilationScope _scope = compiler.scopes[compiler.scopeIndex]; _scope.lastInstruction.Opcode = Opcode.OpReturnValue; compiler.scopes[compiler.scopeIndex] = _scope; }
static void setLastInstruction(Opcode op, int pos) { EmittedInstruction previous = compiler.scopes[compiler.scopeIndex].lastInstruction; EmittedInstruction last = new EmittedInstruction { Opcode = op, Position = pos }; CompilationScope _scope = compiler.scopes[compiler.scopeIndex]; _scope.previousInstruction = previous; _scope.lastInstruction = last; compiler.scopes[compiler.scopeIndex] = _scope; }
static void replaceInstruction(int pos, List <byte> newInstruction) { List <byte> ins = currentInstructions(); for (int i = 0; i < newInstruction.Count; i++) { ins[pos + i] = newInstruction[i]; } CompilationScope _scope = compiler.scopes[compiler.scopeIndex]; _scope.instructions = ins; compiler.scopes[compiler.scopeIndex] = _scope; }
static void enterScope() { CompilationScope scope = new CompilationScope { instructions = new Instructions { }, lastInstruction = new EmittedInstruction { }, previousInstruction = new EmittedInstruction { }, }; compiler.scopes.Add(scope); compiler.scopeIndex++; compiler.symbolTable = symbol_table.NewEnclosedSymbolTable(compiler.symbolTable); }
static void removeLastPop() { EmittedInstruction last = compiler.scopes[compiler.scopeIndex].lastInstruction; EmittedInstruction previous = compiler.scopes[compiler.scopeIndex].previousInstruction; List <byte> old = currentInstructions(); List <byte> new_ = new List <byte>(last.Position + 1); for (int i = 0; i < last.Position; i++) { new_.Add(old[i]); } CompilationScope _scope = compiler.scopes[compiler.scopeIndex]; _scope.instructions = new_; _scope.lastInstruction = previous; compiler.scopes[compiler.scopeIndex] = _scope; }