fix for in loop symbols (#290)

* fix for in loop symbols

* fix comments
This commit is contained in:
Ozan Hacıbekiroğlu 2020-05-22 21:38:29 +03:00 committed by GitHub
parent 22e1e35e11
commit e059953c35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View file

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

View file

@ -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 };