node: elide parentheses in some cases

This commit is contained in:
mappu 2020-04-08 19:58:35 +12:00
parent 334db21fd6
commit 760d1ddbbd

14
node.go
View File

@ -598,7 +598,7 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
return "", parseErr{n, err}
}
args = append(args, exprGo)
args = append(args, removeParens(exprGo))
}
this.importPackages["fmt"] = struct{}{}
@ -1182,6 +1182,16 @@ func constructorName(className string) string {
return `New` + className
}
// removeParens removes surrounding parentheses from an expression.
// This is only safe in cases where there is ambiguously a single rvalue wanted,
// e.g. between ( and , in a function call argument
func removeParens(expr string) string {
for len(expr) > 2 && expr[0] == '(' && expr[len(expr)-1] == ')' {
expr = expr[1 : len(expr)-1]
}
return expr
}
// resolveName turns a `*name.Name` node into a Go string.
func (this *conversionState) resolveName(n node.Node) (string, error) {
// TODO support namespace lookups
@ -1346,7 +1356,7 @@ func (this *conversionState) convertFuncCallArgsCommon(args *node.ArgumentList)
rvalue = "..." + rvalue
}
callParams = append(callParams, rvalue)
callParams = append(callParams, removeParens(rvalue))
}
return "(" + strings.Join(callParams, `, `) + ")", nil // expr only, no semicolon/newline