aboutsummaryrefslogtreecommitdiff
path: root/go/src/types
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-10-09 19:14:43 -0500
committerJoel Martin <github@martintribe.org>2014-10-09 19:14:43 -0500
commitf2c9811fd8cbb205fad68952ebc1ba5d310f148d (patch)
tree0a1d80a4ccfb6378014098475c70a90093b7efa7 /go/src/types
parentad7e866ea1d4d035d876e58bca681a72099449af (diff)
downloadmal-f2c9811fd8cbb205fad68952ebc1ba5d310f148d.tar.gz
mal-f2c9811fd8cbb205fad68952ebc1ba5d310f148d.zip
go: add hash-map support.
Diffstat (limited to 'go/src/types')
-rw-r--r--go/src/types/types.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/go/src/types/types.go b/go/src/types/types.go
index a489aa3..03269f5 100644
--- a/go/src/types/types.go
+++ b/go/src/types/types.go
@@ -62,6 +62,15 @@ func Symbol_Q(obj MalType) bool {
}
+// Strings
+func String_Q(obj MalType) bool {
+ switch obj.(type) {
+ case string: return true
+ default: return false
+ }
+}
+
+
// Functions
type MalFunc struct {
Eval func(MalType, EnvType) (MalType, error)
@@ -141,7 +150,24 @@ func GetSlice(seq MalType) ([]MalType, error) {
}
// Hash Maps
-func Hash_Map_Q(obj MalType) bool {
+func NewHashMap(seq MalType) (MalType, error) {
+ lst, e := GetSlice(seq)
+ if e != nil { return nil, e }
+ if len(lst) % 2 == 1 {
+ return nil, errors.New("Odd number of arguments to NewHashMap")
+ }
+ m := map[string]MalType{}
+ for i := 0; i < len(lst); i+=2 {
+ str, ok := lst[i].(string)
+ if !ok {
+ return nil, errors.New("expected hash-map key string")
+ }
+ m[str] = lst[i+1]
+ }
+ return m, nil
+}
+
+func HashMap_Q(obj MalType) bool {
switch obj.(type) {
case map[string]MalType: return true
default: return false