Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with map returned from lamba single arg, missing outer braces that are needed #177

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions main_test.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ stdout '^{"63":63,"abc":42,"x":{"\[3\]":122,"true":false}}$'
grol -quiet -c 'println(json_go({"abc":42, 63:63, "x": {[3]:122, true:false} }, " "))'
cmp stdout json_output

# returning a map in lambda shouldn't lose needed extra {} despite being solo argument
grol -quiet -c '()=>{{"a":1,"b":2}}'
stdout '^\(\)=>{{"a":1,"b":2}}$'

-- json_output --
{
"63": 63,
Expand Down
6 changes: 4 additions & 2 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"grol.io/grol/ast"
"grol.io/grol/token"
)

type Type uint8
Expand Down Expand Up @@ -535,11 +536,12 @@ func (f *Function) lambdaPrint(ps *ast.PrintState, out *strings.Builder) string
} else {
out.WriteString("=>")
}
if len(f.Body.Statements) != 1 {
needBraces := len(f.Body.Statements) != 1 || f.Body.Statements[0].Value().Type() == token.LBRACE
if needBraces {
out.WriteString("{")
}
f.Body.PrettyPrint(ps)
if len(f.Body.Statements) != 1 {
if needBraces {
out.WriteString("}")
}
return out.String()
Expand Down