fixed mutability
Some checks failed
Build / build (push) Failing after 1m30s
Test / build (push) Failing after 12m1s

This commit is contained in:
Chuck Smith
2024-03-15 15:50:11 -04:00
parent 80f1a2d78c
commit 7463b3af39
7 changed files with 37 additions and 118 deletions

View File

@@ -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 {

View File

@@ -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)