From a9dfd3d6b96ad8b57e266eebbae40caa9b5c456a Mon Sep 17 00:00:00 2001 From: mappu Date: Thu, 16 Apr 2020 18:36:59 +1200 Subject: [PATCH] hoist: add experimental support for postincrement too --- hoistpass.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/hoistpass.go b/hoistpass.go index fff0465..c9c92e1 100644 --- a/hoistpass.go +++ b/hoistpass.go @@ -6,6 +6,7 @@ import ( "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node/expr" "github.com/z7zmey/php-parser/node/expr/binary" + "github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/stmt" "php2go/parseutil" @@ -53,9 +54,27 @@ func (this *hoistPass) Enter(n_ *node.Node) error { 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 *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