node: basic support for *expr.Closure

This commit is contained in:
mappu 2020-04-11 12:46:28 +12:00
parent 6b8d4d879a
commit 5105293ce6
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,9 @@
<?php
const FOO = 'X';
$x = function(string $param): string {
return $param . FOO;
};
echo $x("x");

18
node.go
View File

@ -1036,6 +1036,24 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
return className + constName, nil
case *expr.Closure:
// Need to decide what to do with `this` binding
// TODO figure out what it means to use `this` in all types of nested closures
if n.Static {
return "", parseErr{n, fmt.Errorf("use of `static` in closure implies `this`-insensitive, but we can't make that true")}
}
// TODO n.PhpDocComment
// TODO n.ClosureUse
body, err := this.convertFunctionCommon(n.Params, n.ReturnType, n.ReturnsRef, n.Stmts)
if err != nil {
return "", parseErr{n, err}
}
return "(func" + strings.TrimRight(body, " \n") + ")", nil
case *expr.Exit:
// die(0) - set process exit code and exit
// die("message") - print to stdout and exit 0