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
if len(statements) > 0 {
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"
}
@ -79,7 +79,8 @@ func convert(n_ node.Node) (string, error) {
return n.Value, nil
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
@ -95,7 +96,7 @@ func convert(n_ node.Node) (string, error) {
return "", parseErr{s, err}
}
ret += line + "\n"
ret += line // Statements already added a trailing newline
}
return ret + "}\n", nil
@ -922,20 +923,18 @@ func convertFunctionCommon(params []node.Node, returnType node.Node, returnsRef
}
// Build function prototype
ret := "(" + strings.Join(funcParams, ", ") + ") (" + funcReturn + ", error) {\n"
ret := "(" + strings.Join(funcParams, ", ") + ") (" + funcReturn + ", error) "
// 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
// No extra trailing newline in case this is part of a large expression
ret += "}"
return ret, nil
}