Skip to content

Commit 9fd8a30

Browse files
committed
feat: new dicts builtin
1 parent 1515a18 commit 9fd8a30

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

code_examples/dicts/dictKeys.zum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var a << {"Lucas":2, "João":39};
2+
show(dictValues(a));//[João, Lucas]

code_examples/dicts/dictValues.zum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var a << {"Lucas":2, "João":39};
2+
show(dictValues(a));//[2, 39]

evaluator/builtins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func init() {
1414
"show", "middleOf", "input", "max", "min",
1515
"indexOf", "addToDict", "deleteFromDict",
1616
"toString", "toInt", "toFloat", "toBool", "date", "organize", "toUppercase", "toLowercase", "capitalize",
17-
"removeWhiteSpaces", "sum", "bhaskara", "getFromDict", "sendEmail", "randomInteger", "randomFloat", "sendWhatsapp",
17+
"removeWhiteSpaces", "sum", "bhaskara", "getFromDict", "sendEmail", "randomInteger", "randomFloat", "sendWhatsapp", "dictKeys", "dictValues",
1818
}
1919

2020
for _, name := range names {

object/builtins/builtins.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ var Builtins = []struct {
103103
{
104104
"sendWhatsapp", SendWhatsappBuiltin(),
105105
},
106+
{
107+
"dictKeys", DictKeysBuiltin(),
108+
},
109+
{
110+
"dictValues", DictValuesBuiltin(),
111+
},
106112
}
107113

108114
func NewBoolean(value bool) *object.Boolean {

object/builtins/dict_builtins.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,59 @@ func GetFromDictBuiltin() *object.Builtin {
9393
},
9494
}
9595
}
96+
97+
func DictKeysBuiltin() *object.Builtin {
98+
return &object.Builtin{
99+
Fn: func(args ...object.Object) object.Object {
100+
if len(args) != 1 {
101+
return NewError("wrong number of arguments. got=%d, want=1", len(args))
102+
}
103+
104+
if args[0].Type() != object.DICT_OBJ {
105+
return NewError("argument to `dictKeys` must be DICT, got %s", args[0].Type())
106+
}
107+
108+
dict := args[0].(*object.Dict)
109+
110+
var keys []object.Object
111+
for _, key := range dict.Pairs {
112+
keys = append(keys, key.Key)
113+
}
114+
115+
if len(keys) == 0 {
116+
return &object.Array{Elements: []object.Object{}}
117+
}
118+
119+
return &object.Array{Elements: keys}
120+
121+
},
122+
}
123+
}
124+
125+
func DictValuesBuiltin() *object.Builtin {
126+
return &object.Builtin{
127+
Fn: func(args ...object.Object) object.Object {
128+
if len(args) != 1 {
129+
return NewError("wrong number of arguments. got=%d, want=1", len(args))
130+
}
131+
132+
if args[0].Type() != object.DICT_OBJ {
133+
return NewError("argument to `dictValues` must be DICT, got %s", args[0].Type())
134+
}
135+
136+
dict := args[0].(*object.Dict)
137+
138+
var values []object.Object
139+
for _, value := range dict.Pairs {
140+
values = append(values, value.Value)
141+
}
142+
143+
if len(values) == 0 {
144+
return &object.Array{Elements: []object.Object{}}
145+
}
146+
147+
return &object.Array{Elements: values}
148+
149+
},
150+
}
151+
}

0 commit comments

Comments
 (0)