Skip to content

Commit

Permalink
fix for in loop symbols (#290)
Browse files Browse the repository at this point in the history
* fix for in loop symbols

* fix comments
  • Loading branch information
Ozan Hacıbekiroğlu authored May 22, 2020
1 parent 22e1e35 commit e059953
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,8 @@ func (c *Compiler) compileForInStmt(stmt *parser.ForInStmt) error {
// ... body ...
// }
//
// ":it" is a local variable but will be conflict with other user variables
// because character ":" is not allowed.
// ":it" is a local variable but it will not conflict with other user variables
// because character ":" is not allowed in the variable names.

// init
// :it = iterator(iterable)
Expand Down Expand Up @@ -893,6 +893,7 @@ func (c *Compiler) compileForInStmt(stmt *parser.ForInStmt) error {
if keySymbol.Scope == ScopeGlobal {
c.emit(stmt, parser.OpSetGlobal, keySymbol.Index)
} else {
keySymbol.LocalAssigned = true
c.emit(stmt, parser.OpDefineLocal, keySymbol.Index)
}
}
Expand All @@ -909,6 +910,7 @@ func (c *Compiler) compileForInStmt(stmt *parser.ForInStmt) error {
if valueSymbol.Scope == ScopeGlobal {
c.emit(stmt, parser.OpSetGlobal, valueSymbol.Index)
} else {
valueSymbol.LocalAssigned = true
c.emit(stmt, parser.OpDefineLocal, valueSymbol.Index)
}
}
Expand Down
16 changes: 16 additions & 0 deletions vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,23 @@ func TestFunction(t *testing.T) {
add2 := newAdder(2);
out = add2(5);
`, nil, 7)
expectRun(t, `
m := {a: 1}
for k,v in m {
func(){
out = k
}()
}
`, nil, "a")

expectRun(t, `
m := {a: 1}
for k,v in m {
func(){
out = v
}()
}
`, nil, 1)
// function as a argument
expectRun(t, `
add := func(a, b) { return a + b };
Expand Down

0 comments on commit e059953

Please sign in to comment.