refactor how repl works
This commit is contained in:
@@ -25,8 +25,11 @@ type CompilationScope struct {
|
||||
type Compiler struct {
|
||||
Debug bool
|
||||
|
||||
fn string
|
||||
input string
|
||||
|
||||
l int
|
||||
constants []object.Object
|
||||
constants *[]object.Object
|
||||
|
||||
symbolTable *SymbolTable
|
||||
|
||||
@@ -48,7 +51,7 @@ func New() *Compiler {
|
||||
}
|
||||
|
||||
return &Compiler{
|
||||
constants: []object.Object{},
|
||||
constants: &[]object.Object{},
|
||||
symbolTable: symbolTable,
|
||||
scopes: []CompilationScope{mainScope},
|
||||
scopeIndex: 0,
|
||||
@@ -59,7 +62,7 @@ func (c *Compiler) currentInstructions() code.Instructions {
|
||||
return c.scopes[c.scopeIndex].instructions
|
||||
}
|
||||
|
||||
func NewWithState(s *SymbolTable, constants []object.Object) *Compiler {
|
||||
func NewWithState(s *SymbolTable, constants *[]object.Object) *Compiler {
|
||||
compiler := New()
|
||||
compiler.symbolTable = s
|
||||
compiler.constants = constants
|
||||
@@ -442,7 +445,7 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
|
||||
freeSymbols := c.symbolTable.FreeSymbols
|
||||
numLocals := c.symbolTable.numDefinitions
|
||||
numLocals := c.symbolTable.NumDefinitions
|
||||
instructions := c.leaveScope()
|
||||
|
||||
for _, s := range freeSymbols {
|
||||
@@ -529,8 +532,8 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
|
||||
func (c *Compiler) addConstant(obj object.Object) int {
|
||||
c.constants = append(c.constants, obj)
|
||||
return len(c.constants) - 1
|
||||
*c.constants = append(*c.constants, obj)
|
||||
return len(*c.constants) - 1
|
||||
}
|
||||
|
||||
func (c *Compiler) emit(op code.Opcode, operands ...int) int {
|
||||
@@ -542,6 +545,11 @@ func (c *Compiler) emit(op code.Opcode, operands ...int) int {
|
||||
return pos
|
||||
}
|
||||
|
||||
func (c *Compiler) SetFileInfo(fn, input string) {
|
||||
c.fn = fn
|
||||
c.input = input
|
||||
}
|
||||
|
||||
func (c *Compiler) setLastInstruction(op code.Opcode, pos int) {
|
||||
previous := c.scopes[c.scopeIndex].lastInstruction
|
||||
last := EmittedInstruction{Opcode: op, Position: pos}
|
||||
@@ -553,7 +561,7 @@ func (c *Compiler) setLastInstruction(op code.Opcode, pos int) {
|
||||
func (c *Compiler) Bytecode() *Bytecode {
|
||||
return &Bytecode{
|
||||
Instructions: c.currentInstructions(),
|
||||
Constants: c.constants,
|
||||
Constants: *c.constants,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -630,11 +638,6 @@ func (c *Compiler) replaceLastPopWithReturn() {
|
||||
c.scopes[c.scopeIndex].lastInstruction.Opcode = code.OpReturn
|
||||
}
|
||||
|
||||
type Bytecode struct {
|
||||
Instructions code.Instructions
|
||||
Constants []object.Object
|
||||
}
|
||||
|
||||
func (c *Compiler) loadSymbol(s Symbol) {
|
||||
switch s.Scope {
|
||||
case GlobalScope:
|
||||
|
||||
Reference in New Issue
Block a user