Skip to content

Commit

Permalink
update map object String method
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidesu committed Oct 26, 2023
1 parent 7a53f8a commit f857740
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion examples/hello.ghost
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ bar = {
}

print(bar.foo)
print(foo)
print(foo)
print(bar)
26 changes: 24 additions & 2 deletions object/map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package object

import "github.com/shopspring/decimal"
import (
"bytes"
"fmt"
"strings"

"github.com/shopspring/decimal"
)

const MAP = "MAP"

Expand All @@ -16,7 +22,23 @@ type MapPair struct {

// String represents the map object's value as a string.
func (mapObject *Map) String() string {
return "map"
var out bytes.Buffer

length := len(mapObject.Pairs)
pairs := make([]string, length)

var index int

for _, pair := range mapObject.Pairs {
pairs[index] = fmt.Sprintf("%s: %s", pair.Key.String(), pair.Value.String())
index++
}

out.WriteString("{")
out.WriteString(strings.Join(pairs, ", "))
out.WriteString("}")

return out.String()
}

// Type returns the map object type.
Expand Down

0 comments on commit f857740

Please sign in to comment.