stmt: remove extra newlines in output

This commit is contained in:
mappu 2020-04-05 18:23:28 +12:00
parent cdfa7df4fa
commit 6b40079884

21
node.go
View File

@ -69,7 +69,7 @@ func convert(n_ node.Node) (string, error) {
// Emit deferred statements // Emit deferred statements
if len(statements) > 0 { if len(statements) > 0 {
ret += "func init() {\n" ret += "func init() {\n"
ret += "\t" + strings.Join(statements, "\n\t") + "\n" ret += "\t" + strings.Join(statements, "\t") // Statements already added their own newline
ret += "}\n" ret += "}\n"
} }
@ -79,7 +79,8 @@ func convert(n_ node.Node) (string, error) {
return n.Value, nil return n.Value, nil
case Literal: case Literal:
return n.Value, nil // We expect literal statements to act like a *Stmt, i.e. be emitted with a trailing NL
return n.Value + "\n", nil
// //
// stmt // stmt
@ -95,7 +96,7 @@ func convert(n_ node.Node) (string, error) {
return "", parseErr{s, err} return "", parseErr{s, err}
} }
ret += line + "\n" ret += line // Statements already added a trailing newline
} }
return ret + "}\n", nil return ret + "}\n", nil
@ -922,20 +923,18 @@ func convertFunctionCommon(params []node.Node, returnType node.Node, returnsRef
} }
// Build function prototype // Build function prototype
ret := "(" + strings.Join(funcParams, ", ") + ") (" + funcReturn + ", error) {\n" ret := "(" + strings.Join(funcParams, ", ") + ") (" + funcReturn + ", error) "
// Recurse through body statements // Recurse through body statements
for _, s := range bodyStmts {
bodyStmt, err := convert(s)
if err != nil {
return "", parseErr{s, err}
}
ret += bodyStmt + "\n" fullBody, err := convert(stmt.NewStmtList(bodyStmts))
if err != nil {
return "", err
} }
ret += fullBody + "\n"
// Done // Done
// No extra trailing newline in case this is part of a large expression // No extra trailing newline in case this is part of a large expression
ret += "}"
return ret, nil return ret, nil
} }