factor: Add number?, fn?, macro?
[jackhill/mal.git] / groovy / core.groovy
CommitLineData
a9cd6543
JM
1import types
2import types.MalException
3import types.MalSymbol
4import reader
5import printer
6
7class core {
8 def static do_pr_str(args) {
9 return printer._pr_list(args, " ", true)
10 }
11 def static do_str(args) {
12 return printer._pr_list(args, "", false)
13 }
14 def static do_prn(args) {
15 println(printer._pr_list(args, " ", true))
16 }
17 def static do_println(args) {
18 println(printer._pr_list(args, " ", false))
19 }
20
21 def static do_concat(args) {
22 args.inject([], { a, b -> a + (b as List) })
23 }
24 def static do_nth(args) {
25 if (args[0].size() <= args[1]) {
26 throw new MalException("nth: index out of range")
27 }
28 args[0][args[1]]
29 }
30 def static do_apply(args) {
31 def start_args = args.drop(1).take(args.size()-2) as List
32 args[0](start_args + (args.last() as List))
33 }
34
35 def static do_swap_BANG(args) {
36 def (atm,f) = [args[0], args[1]]
37 atm.value = f([atm.value] + (args.drop(2) as List))
38 }
39
8e32dbb6
JM
40 def static do_conj(args) {
41 if (types.list_Q(args[0])) {
42 args.drop(1).inject(args[0], { a, b -> [b] + a })
43 } else {
44 types.vector(args.drop(1).inject(args[0], { a, b -> a + [b] }))
45 }
46 }
47 def static do_seq(args) {
48 def obj = args[0]
49 switch (obj) {
50 case { types.list_Q(obj) }:
51 return obj.size() == 0 ? null : obj
52 case { types.vector_Q(obj) }:
53 return obj.size() == 0 ? null : obj.clone()
54 case { types.string_Q(obj) }:
55 return obj.size() == 0 ? null : obj.collect{ it.toString() }
56 case null:
57 return null
58 default:
59 throw new MalException("seq: called on non-sequence")
60 }
61 }
62
a9cd6543
JM
63 static ns = [
64 "=": { a -> a[0]==a[1]},
65 "throw": { a -> throw new MalException(a[0]) },
66
67 "nil?": { a -> a[0] == null },
68 "true?": { a -> a[0] == true },
69 "false?": { a -> a[0] == false },
8e32dbb6 70 "string?": { a -> types.string_Q(a[0]) },
a9cd6543
JM
71 "symbol": { a -> new MalSymbol(a[0]) },
72 "symbol?": { a -> a[0] instanceof MalSymbol },
73 "keyword": { a -> types.keyword(a[0]) },
74 "keyword?": { a -> types.keyword_Q(a[0]) },
75
76 "pr-str": core.&do_pr_str,
77 "str": core.&do_str,
78 "prn": core.&do_prn,
79 "println": core.&do_println,
80 "read-string": reader.&read_str,
81 "readline": { a -> System.console().readLine(a[0]) },
82 "slurp": { a -> new File(a[0]).text },
83
84 "<": { a -> a[0]<a[1]},
85 "<=": { a -> a[0]<=a[1]},
86 ">": { a -> a[0]>a[1]},
87 ">=": { a -> a[0]>=a[1]},
88 "+": { a -> a[0]+a[1]},
89 "-": { a -> a[0]-a[1]},
90 "*": { a -> a[0]*a[1]},
91 "/": { a -> a[0]/a[1]}, // /
92 "time-ms": { a -> System.currentTimeMillis() },
93
94 "list": { a -> a},
95 "list?": { a -> types.list_Q(a[0]) },
96 "vector": { a -> types.vector(a) },
97 "vector?": { a -> types.vector_Q(a[0]) },
98 "hash-map": { a -> types.hash_map(a) },
99 "map?": { a -> types.hash_map_Q(a[0]) },
100 "assoc": { a -> types.assoc_BANG(types.copy(a[0]), a.drop(1)) },
101 "dissoc": { a -> types.dissoc_BANG(types.copy(a[0]), a.drop(1)) },
102 "get": { a -> a[0] == null ? null : a[0][a[1]] },
103 "contains?": { a -> a[0].containsKey(a[1]) },
104 "keys": { a -> a[0].keySet() as List },
105 "vals": { a -> a[0].values() as List },
106
107 "sequential?": { a -> types.&sequential_Q(a[0]) },
108 "cons": { a -> [a[0]] + (a[1] as List) },
109 "concat": core.&do_concat,
110 "nth": core.&do_nth,
c36d5b39
DM
111 "first": { a -> a[0] == null || a[0].size() == 0 ? null : a[0][0] },
112 "rest": { a -> a[0] == null ? [] as List : a[0].drop(1) },
a9cd6543
JM
113 "empty?": { a -> a[0] == null || a[0].size() == 0 },
114 "count": { a -> a[0] == null ? 0 : a[0].size() },
115 "apply": core.&do_apply,
116 "map": { a -> a[1].collect { x -> a[0].call([x]) } },
117
4ed89670 118 "conj": core.&do_conj,
8e32dbb6 119 "seq": core.&do_seq,
a9cd6543
JM
120
121 "meta": { a -> a[0].hasProperty("meta") ? a[0].getProperties().meta : null },
122 "with-meta": { a -> def b = types.copy(a[0]); b.getMetaClass().meta = a[1]; b },
123 "atom": { a -> new types.MalAtom(a[0]) },
124 "atom?": { a -> a[0] instanceof types.MalAtom },
125 "deref": { a -> a[0].value },
126 "reset!": { a -> a[0].value = a[1] },
127 "swap!": core.&do_swap_BANG
128 ]
129}
130