hoist: new hoisting context inside closures and loops; don't hoist loop vars

This commit is contained in:
mappu 2020-04-16 18:37:16 +12:00
parent a9dfd3d6b9
commit ae04fa73ef

View File

@ -31,6 +31,9 @@ func (this *hoistPass) Enter(n_ *node.Node) error {
// can't contain any hoistable statements
return this.enterStmtList(&n.Stmts)
case *expr.Closure:
return this.enterStmtList(&n.Stmts)
case *node.Root:
return this.enterStmtList(&n.Stmts)
@ -46,6 +49,30 @@ func (this *hoistPass) Enter(n_ *node.Node) error {
case *binary.LogicalOr:
return this.enterShortCircuitingBinOp(&n.Left, &n.Right)
// Don't try to hoist a ++i statement in the loop conditional
// Bypass by just walking the stmts in a new hoist context
// TODO handle hoisting within closures in conditional statements
case *stmt.For:
if sl, ok := n.Stmt.(*stmt.StmtList); ok {
return this.enterStmtList(&sl.Stmts)
} else {
// single statement, ignore
}
case *stmt.Do:
if sl, ok := n.Stmt.(*stmt.StmtList); ok {
return this.enterStmtList(&sl.Stmts)
} else {
// single statement, ignore
}
case *stmt.While:
if sl, ok := n.Stmt.(*stmt.StmtList); ok {
return this.enterStmtList(&sl.Stmts)
} else {
// single statement, ignore
}
case *expr.PreInc:
// Hoist the whole increment expression up to statement context
// We also have to convert it to a post-increment