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

[object/string] Add support for HASH and ARRAY to .format() #148

Merged
merged 3 commits into from
Nov 1, 2022
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
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ require (
github.com/spf13/pflag v1.0.5
)

require golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@ github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
29 changes: 29 additions & 0 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,32 @@ func AnyToObject(a any) Object {
}
return NIL
}

func ObjectToAny(o Object) any {
switch v := o.(type) {
case *Boolean:
return v.Value
case *String:
return v.Value
case *Integer:
return v.Value
case *Float:
return v.Value
case *Array:
array := make([]any, len(v.Elements))
for idx, element := range v.Elements {
array[idx] = ObjectToAny(element)
}
return array
case *Hash:
hash := make(map[any]any)
for _, pair := range v.Pairs {
key := ObjectToAny(pair.Key)
if key != nil {
hash[key] = ObjectToAny(pair.Value)
}
}
return hash
}
return nil
}
27 changes: 25 additions & 2 deletions object/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package object_test

import (
"errors"
"testing"

"github.com/stretchr/testify/require"

"github.com/flipez/rocket-lang/evaluator"
"github.com/flipez/rocket-lang/lexer"
"github.com/flipez/rocket-lang/object"
"github.com/flipez/rocket-lang/parser"

"testing"
)

type inputTestCase struct {
Expand Down Expand Up @@ -149,3 +150,25 @@ func TestAnyToObject(t *testing.T) {
}
}
}

func TestObjectToAny(t *testing.T) {
stringObj := object.NewString("a")
intObj := object.NewInteger(1)
floatObj := object.NewFloat(1.2)
testcases := map[object.Object]any{
stringObj: "a",
intObj: int64(1),
floatObj: float64(1.2),
object.TRUE: true,
object.NewArrayWithObjects(stringObj, intObj, floatObj): []any{"a", int64(1), float64(1.2)},
object.NIL: nil,
}

hash := object.NewHash(nil)
hash.Set("a", int64(1))
testcases[hash] = map[any]any{"a": int64(1)}

for input, expected := range testcases {
require.Equal(t, expected, object.ObjectToAny(input))
}
}
18 changes: 4 additions & 14 deletions object/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,18 @@ func init() {
🚀 » "test%s".format("test")
» "testtest"`,
ArgPattern: Args(
OverloadArg(STRING_OBJ, INTEGER_OBJ, FLOAT_OBJ, BOOLEAN_OBJ),
OverloadArg(STRING_OBJ, INTEGER_OBJ, FLOAT_OBJ, BOOLEAN_OBJ, ARRAY_OBJ, HASH_OBJ),
),
ReturnPattern: Args(
Arg(STRING_OBJ),
),
},
method: func(o Object, args []Object, _ Environment) Object {
s := o.(*String)
nativeObjects := []interface{}{}
for _, arg := range args {
switch e := arg.(type) {
case *String:
nativeObjects = append(nativeObjects, e.Value)
case *Integer:
nativeObjects = append(nativeObjects, e.Value)
case *Float:
nativeObjects = append(nativeObjects, e.Value)
case *Boolean:
nativeObjects = append(nativeObjects, e.Value)
}
nativeObjects := make([]any, len(args))
for idx, arg := range args {
nativeObjects[idx] = ObjectToAny(arg)
}

return NewString(fmt.Sprintf(s.Value, nativeObjects...))
},
},
Expand Down