node: support die()

This commit is contained in:
mappu 2020-04-08 19:58:09 +12:00
parent daf5000404
commit 334db21fd6
1 changed files with 27 additions and 0 deletions

27
node.go
View File

@ -850,6 +850,33 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
return this.convert(expr.NewFunctionCall(transparentNameNode, n.ArgumentList))
case *expr.Exit:
// die(0) - set process exit code and exit
// die("message") - print to stdout and exit 0
// die - exit 0
this.importPackages["os"] = struct{}{}
if n.Expr == nil {
return "os.Exit(0)", nil
}
child, err := this.convert(n.Expr)
if err != nil {
return "", parseErr{n, err}
}
// Although it might have been a more complex string expression - we
// don't currently know that
// TODO type inference
switch n.Expr.(type) {
case *scalar.String:
this.importPackages["fmt"] = struct{}{}
return "fmt.Print(" + child + ")\nos.Exit(0)", nil // hopefully die() was standalone, and not "or die"
default:
return "os.Exit(" + child + ")", nil
}
case *expr.PreInc:
// """In Go, i++ is a statement, not an expression. So you can't use its value in another expression such as a function call."""
v, err := this.convert(n.Variable)