Merge pull request #91 from rhysd/fix-crystal-0.8.0
[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
40 static ns = [
41 "=": { a -> a[0]==a[1]},
42 "throw": { a -> throw new MalException(a[0]) },
43
44 "nil?": { a -> a[0] == null },
45 "true?": { a -> a[0] == true },
46 "false?": { a -> a[0] == false },
47 "symbol": { a -> new MalSymbol(a[0]) },
48 "symbol?": { a -> a[0] instanceof MalSymbol },
49 "keyword": { a -> types.keyword(a[0]) },
50 "keyword?": { a -> types.keyword_Q(a[0]) },
51
52 "pr-str": core.&do_pr_str,
53 "str": core.&do_str,
54 "prn": core.&do_prn,
55 "println": core.&do_println,
56 "read-string": reader.&read_str,
57 "readline": { a -> System.console().readLine(a[0]) },
58 "slurp": { a -> new File(a[0]).text },
59
60 "<": { a -> a[0]<a[1]},
61 "<=": { a -> a[0]<=a[1]},
62 ">": { a -> a[0]>a[1]},
63 ">=": { a -> a[0]>=a[1]},
64 "+": { a -> a[0]+a[1]},
65 "-": { a -> a[0]-a[1]},
66 "*": { a -> a[0]*a[1]},
67 "/": { a -> a[0]/a[1]}, // /
68 "time-ms": { a -> System.currentTimeMillis() },
69
70 "list": { a -> a},
71 "list?": { a -> types.list_Q(a[0]) },
72 "vector": { a -> types.vector(a) },
73 "vector?": { a -> types.vector_Q(a[0]) },
74 "hash-map": { a -> types.hash_map(a) },
75 "map?": { a -> types.hash_map_Q(a[0]) },
76 "assoc": { a -> types.assoc_BANG(types.copy(a[0]), a.drop(1)) },
77 "dissoc": { a -> types.dissoc_BANG(types.copy(a[0]), a.drop(1)) },
78 "get": { a -> a[0] == null ? null : a[0][a[1]] },
79 "contains?": { a -> a[0].containsKey(a[1]) },
80 "keys": { a -> a[0].keySet() as List },
81 "vals": { a -> a[0].values() as List },
82
83 "sequential?": { a -> types.&sequential_Q(a[0]) },
84 "cons": { a -> [a[0]] + (a[1] as List) },
85 "concat": core.&do_concat,
86 "nth": core.&do_nth,
87 "first": { a -> a[0].size() == 0 ? null : a[0][0] },
88 "rest": { a -> a[0].drop(1) },
89 "empty?": { a -> a[0] == null || a[0].size() == 0 },
90 "count": { a -> a[0] == null ? 0 : a[0].size() },
91 "apply": core.&do_apply,
92 "map": { a -> a[1].collect { x -> a[0].call([x]) } },
93
94 "conj": null,
95
96 "meta": { a -> a[0].hasProperty("meta") ? a[0].getProperties().meta : null },
97 "with-meta": { a -> def b = types.copy(a[0]); b.getMetaClass().meta = a[1]; b },
98 "atom": { a -> new types.MalAtom(a[0]) },
99 "atom?": { a -> a[0] instanceof types.MalAtom },
100 "deref": { a -> a[0].value },
101 "reset!": { a -> a[0].value = a[1] },
102 "swap!": core.&do_swap_BANG
103 ]
104}
105