hoist: add experimental support for postincrement too

This commit is contained in:
mappu 2020-04-16 18:36:59 +12:00
parent fc0008a208
commit a9dfd3d6b9

View File

@ -6,6 +6,7 @@ import (
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/expr" "github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/expr/binary" "github.com/z7zmey/php-parser/node/expr/binary"
"github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"php2go/parseutil" "php2go/parseutil"
@ -53,9 +54,27 @@ func (this *hoistPass) Enter(n_ *node.Node) error {
return parseErr{n, err} return parseErr{n, err}
} }
// FIXME detect interior function calls since we may be dangerously doubling up on that
// For a more complex expression it is probably OK to use a temporary variable
// We also need to replace our own node with just the value itself // We also need to replace our own node with just the value itself
*n_ = n.Variable *n_ = n.Variable
case *expr.PostInc:
// Hoist the whole increment expression up to statement context
err := this.nextHoist(stmt.NewExpression(n))
if err != nil {
return parseErr{n, err}
}
// We also need to replace our own node. The resulting value would normally
// be one less than the current value
// PHP only supports lvalues for pre/postinc expressions so it should
// be fine in all Go contexts too to reuse the variable expression
// FIXME except for $foo[rand()]++
*n_ = binary.NewPlus(n.Variable, scalar.NewLnumber(`1`))
} }
return nil // TODO return nil // TODO