Haxe: step7-A, hash-maps, metadata, self-hosting.
[jackhill/mal.git] / groovy / types.groovy
1 import groovy.transform.InheritConstructors
2 import groovy.transform.AutoClone
3
4 class types {
5 def static copy(obj) {
6 def new_obj = obj
7 if (obj instanceof Collection || obj instanceof Map) {
8 new_obj = obj.clone()
9 if (obj.hasProperty("meta")) {
10 new_obj.getMetaClass().meta = obj.getProperties().meta
11 }
12 if (obj.hasProperty("isvector")) {
13 new_obj.getMetaClass().isvector = obj.getProperties().isvector
14 }
15 } else if (obj instanceof Object) {
16 new_obj = obj.clone()
17 }
18 return new_obj
19 }
20
21 @InheritConstructors
22 static class MalException extends Exception {
23 def obj
24 MalException(String message) {
25 super(message)
26 obj = message
27 }
28 MalException(_obj) {
29 super("mal exception containing object")
30 obj = _obj
31 }
32 }
33
34 @AutoClone
35 static class MalSymbol implements Comparable {
36 String value
37 MalSymbol(String name) {
38 value = name
39 }
40 int compareTo(o) { value <=> o.value }
41 }
42
43 def static keyword(o) {
44 types.&keyword_Q(o) ? o : ("\u029e" + o)
45 }
46 def static keyword_Q(o) {
47 return o instanceof String && o.size() > 0 && o[0] == "\u029e"
48 }
49
50 def static list_Q(o) {
51 //return (o instanceof List || o instanceof Object[]) &&
52 return o instanceof List && !o.hasProperty("isvector")
53 }
54
55 def static vector(o) {
56 def v = o.collect()
57 v.metaClass.isvector = true
58 v
59 }
60 def static vector_Q(o) {
61 return o instanceof List && o.hasProperty("isvector") && o.isvector
62 }
63
64 def static hash_map(lst) {
65 def m = [:]
66 assoc_BANG(m, lst)
67 }
68 def static assoc_BANG(m, kvs) {
69 for (int i=0; i<kvs.size(); i+=2) {
70 m[kvs[i]] = kvs[i+1];
71 }
72 return m
73 }
74 def static dissoc_BANG(m, ks) {
75 for (int i=0; i<ks.size(); i++) {
76 m.remove(ks[i])
77 }
78 return m
79 }
80 def static hash_map_Q(o) {
81 return o instanceof Map
82 }
83
84 def static sequential_Q(o) {
85 return types.list_Q(o) || types.vector_Q(o)
86 }
87
88 @AutoClone
89 static class MalFunc {
90 def EVAL
91 def ast
92 def env
93 def params
94 def ismacro
95
96 MalFunc(_EVAL, _ast, _env, _params) {
97 EVAL = _EVAL
98 ast = _ast
99 env = _env
100 params = _params
101 ismacro = false
102 }
103
104 def call(args) {
105 def new_env = env.class.newInstance([env, params, args] as Object[])
106 return EVAL(ast, new_env)
107 }
108 }
109
110 @AutoClone
111 static class MalAtom {
112 def value
113 MalAtom(_value) {
114 value = _value
115 }
116 }
117 }