stmt: add failing test for assignment expressions (need hoisting)

This commit is contained in:
mappu 2020-04-05 19:41:21 +12:00
parent 628e4c1781
commit 2568412a50
3 changed files with 23 additions and 0 deletions

View File

@ -34,6 +34,9 @@ The goal is to produce idiomatic, maintainable Go code as part of a one-off conv
[ ] `instanceof`
[ ] `try`/`catch`/`finally`
[ ] Abandon upon sight of `eval` / `extract` / ...
[ ] Assignment expressions
- Go doesn't support assignment in rvalues, only as a statement
- When walking below stmt level, need to first fully walk and check for any function calls + assignments that may need hoisting (and we can probably only do that correctly if there is no short-circuiting)
[ ] Closures
- [ ] Handle value/reference captures
[ ] Sort callbacks

View File

@ -0,0 +1,11 @@
<?php
function Bar(): ?\stdClass {
return new \stdClass();
}
if ( ($f = Bar()) !== null) {
var_dump($f);
} else {
echo "x\n";
}

View File

@ -968,11 +968,20 @@ func (this *conversionState) convertBinaryCommon(left, right node.Node, goBinary
if err != nil {
return "", parseErr{left, err}
}
rhs, err := this.convert(right)
if err != nil {
return "", parseErr{right, err}
}
// In case of an rvalue assignment expression, we need extra parens
if _, ok := left.(*assign.Assign); ok {
lhs = "(" + lhs + ")"
}
if _, ok := right.(*assign.Assign); ok {
rhs = "(" + rhs + ")"
}
return "(" + lhs + " " + goBinaryOperator + " " + rhs + ")", nil
}