fixed mutability
This commit is contained in:
@@ -210,13 +210,11 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
|
||||
if symbol.Scope == GlobalScope {
|
||||
c.emit(code.OpGetGlobal, symbol.Index)
|
||||
c.emit(code.OpAssignGlobal, symbol.Index)
|
||||
} else {
|
||||
c.emit(code.OpGetLocal, symbol.Index)
|
||||
c.emit(code.OpAssignLocal, symbol.Index)
|
||||
}
|
||||
|
||||
c.emit(code.OpAssign)
|
||||
|
||||
case *ast.LetStatement:
|
||||
symbol, ok := c.symbolTable.Resolve(node.Name.Value)
|
||||
if !ok {
|
||||
|
||||
@@ -779,8 +779,7 @@ func TestAssignmentStatementScopes(t *testing.T) {
|
||||
55,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpGetGlobal, 0),
|
||||
code.Make(code.OpAssign),
|
||||
code.Make(code.OpAssignGlobal, 0),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
@@ -791,48 +790,19 @@ func TestAssignmentStatementScopes(t *testing.T) {
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
input: `
|
||||
fn() {
|
||||
let num = 55;
|
||||
num
|
||||
}
|
||||
`,
|
||||
fn() { let num = 0; num = 55; }
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
0,
|
||||
55,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSetLocal, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpClosure, 1, 0),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `
|
||||
fn() {
|
||||
let a = 55;
|
||||
let b = 77;
|
||||
a + b
|
||||
}
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
55,
|
||||
77,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSetLocal, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpSetLocal, 1),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpGetLocal, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpAssignLocal, 0),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -840,24 +810,6 @@ func TestAssignmentStatementScopes(t *testing.T) {
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `
|
||||
let a = 0;
|
||||
let a = a + 1;
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
0,
|
||||
1,
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSetGlobal, 0),
|
||||
code.Make(code.OpGetGlobal, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpSetGlobal, 0),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runCompilerTests(t, tests)
|
||||
|
||||
Reference in New Issue
Block a user