reassign values
This commit is contained in:
@@ -198,6 +198,25 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
}
|
||||
|
||||
case *ast.AssignmentStatement:
|
||||
symbol, ok := c.symbolTable.Resolve(node.Name.Value)
|
||||
if !ok {
|
||||
return fmt.Errorf("undefined variable %s", node.Value)
|
||||
}
|
||||
|
||||
err := c.Compile(node.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if symbol.Scope == GlobalScope {
|
||||
c.emit(code.OpGetGlobal, symbol.Index)
|
||||
} else {
|
||||
c.emit(code.OpGetLocal, symbol.Index)
|
||||
}
|
||||
|
||||
c.emit(code.OpAssign)
|
||||
|
||||
case *ast.LetStatement:
|
||||
symbol, ok := c.symbolTable.Resolve(node.Name.Value)
|
||||
if !ok {
|
||||
|
||||
@@ -767,6 +767,102 @@ func TestFunctionCalls(t *testing.T) {
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func TestAssignmentStatementScopes(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: `
|
||||
let num = 0;
|
||||
fn() { num = 55; }
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
0,
|
||||
55,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpGetGlobal, 0),
|
||||
code.Make(code.OpAssign),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSetGlobal, 0),
|
||||
code.Make(code.OpClosure, 2, 0),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
input: `
|
||||
fn() {
|
||||
let num = 55;
|
||||
num
|
||||
}
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
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),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpClosure, 2, 0),
|
||||
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)
|
||||
}
|
||||
|
||||
func TestLetStatementScopes(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user