Merge pull request #310 from bendudson/master
[jackhill/mal.git] / livescript / core.ls
1
2 {
3 zip, map, apply, and-list, join, Obj, concat, all,
4 pairs-to-obj, obj-to-pairs, reject, keys, values,
5 difference, empty, reverse, chars
6 } = require 'prelude-ls'
7 {pr_str} = require './printer'
8 {read_str, list-to-map, map-keyword, keyword-prefix} = require './reader'
9 fs = require 'fs'
10 {readline} = require './node_readline'
11
12
13 export runtime-error = (msg) -> throw new Error msg
14
15 export unpack-tco = (ast) ->
16 if ast.type == \tco
17 then ast.eval!
18 else ast
19
20 fn = (body) -> {type: \function, value: body}
21 const-nil = -> {type: \const, value: \nil}
22 const-int = (int) -> {type: \int, value: int}
23 const-bool = (bool) -> {type: \const, value: if bool then \true else \false}
24 const-str = (str) -> {type: \string, value: str}
25
26 list-or-vector = ({type}) -> type in [\list \vector]
27
28 are-lists-equal = (equals-fn, a, b) ->
29 if a.length != b.length then false
30 else zip a, b |> map (apply equals-fn) |> and-list
31
32 deep-equals = (a, b) ->
33 if (list-or-vector a) and (list-or-vector b) then
34 are-lists-equal deep-equals, a.value, b.value
35 else if a.type == \map and b.type == \map then
36 a-keys = keys a.value
37 b-keys = keys b.value
38 if a-keys.length == b-keys.length and \
39 empty (difference a-keys, b-keys)
40 #if are-lists-equal (==), a-keys, b-keys
41 a-keys |> map (key) -> [a.value[key], b.value[key]]
42 |> map (apply deep-equals)
43 |> and-list
44 else false
45 else if a.type != b.type then false
46 else a.value == b.value
47
48
49 check-param = (name, idx, test, expected, actual) ->
50 if not test
51 runtime-error "'#{name}' expected parameter #{idx}
52 to be #{expected}, got #{actual}"
53
54
55 check-type = (name, idx, expected, actual) ->
56 check-param name, idx, expected == actual, expected, actual
57
58
59 export ns = do
60 '+': fn (a, b) -> const-int a.value + b.value
61 '-': fn (a, b) -> const-int a.value - b.value
62 '*': fn (a, b) -> const-int a.value * b.value
63 '/': fn (a, b) -> const-int parseInt (a.value / b.value)
64
65 'list': fn (...list) -> {type: \list, value: list}
66 'list?': fn (param) -> const-bool param.type == \list
67
68 'empty?': fn ({type, value}) ->
69 switch type
70 | \const =>
71 if value == \nil
72 then const-bool true
73 else runtime-error "'empty?' is not supported on #{value}"
74 | \list, \vector =>
75 const-bool value.length == 0
76 | \map =>
77 const-bool Obj.empty value
78 | otherwise =>
79 runtime-error "'empty?' is not supported on type #{type}"
80
81 'count': fn ({type, value}) ->
82 switch type
83 | \const =>
84 if value == \nil
85 then const-int 0
86 else runtime-error "'count' is not supported on #{value}"
87 | \list, \vector =>
88 const-int value.length
89 | \map =>
90 value |> Obj.keys |> (.length) |> const-int
91 | otherwise =>
92 runtime-error "'count' is not supported on type #{type}"
93
94 '=': fn (a, b) -> const-bool (deep-equals a, b)
95 '<': fn (a, b) -> const-bool a.value < b.value
96 '>': fn (a, b) -> const-bool a.value > b.value
97 '<=': fn (a, b) -> const-bool a.value <= b.value
98 '>=': fn (a, b) -> const-bool a.value >= b.value
99
100 'not': fn ({type, value}) ->
101 const-bool (type == \const and value in [\false \nil])
102
103 'pr-str': fn (...params) ->
104 params |> map (p) -> pr_str p, print_readably=true
105 |> join ' '
106 |> const-str
107
108 'str': fn (...params) ->
109 params |> map (p) -> pr_str p, print_readably=false
110 |> join ''
111 |> const-str
112
113 'prn': fn (...params) ->
114 params |> map (p) -> pr_str p, print_readably=true
115 |> join ' '
116 |> console.log
117 |> const-nil
118
119 'println': fn (...params) ->
120 params |> map (p) -> pr_str p, print_readbly=false
121 |> join ' '
122 |> console.log
123 |> const-nil
124
125 'read-string': fn ({type, value}) ->
126 check-type 'read-string', 0, \string, type
127 read_str value
128
129 'slurp': fn (filename) ->
130 if filename.type != \string
131 runtime-error "'slurp' expected the first parameter
132 to be a string, got a #{filename.type}"
133
134 const-str <| fs.readFileSync filename.value, 'utf8'
135
136 'atom': fn (value) -> {type: \atom, value: value}
137 'atom?': fn (atom) -> const-bool atom.type == \atom
138 'deref': fn (atom) ->
139 check-type 'deref', 0, \atom, atom.type
140 atom.value
141
142 'reset!': fn (atom, value) ->
143 check-type 'reset!', 0, \atom, atom.type
144 atom.value = value
145
146 'swap!': fn (atom, fn, ...args) ->
147 check-type 'swap!', 0, \atom, atom.type
148 if fn.type != \function
149 runtime-error "'swap!' expected the second parameter
150 to be a function, got a #{fn.type}"
151
152 atom.value = unpack-tco (fn.value.apply @, [atom.value] ++ args)
153
154 'cons': fn (value, list) ->
155 check-param 'cons', 1, (list-or-vector list),
156 'list or vector', list.type
157
158 {type: \list, value: [value] ++ list.value}
159
160 'concat': fn (...params) ->
161 if not all list-or-vector, params
162 runtime-error "'concat' expected all parameters to be a list or vector"
163
164 {type: \list, value: params |> map (.value) |> concat}
165
166 'nth': fn (list, index) ->
167 check-param 'nth', 0, (list-or-vector list),
168 'list or vector', list.type
169 check-param 'nth', 1, index.type == \int,
170 'int', index.type
171
172 if index.value < 0 or index.value >= list.value.length
173 runtime-error 'list index out of bounds'
174
175 list.value[index.value]
176
177 'first': fn (list) ->
178 if list.type == \const and list.value == \nil
179 return const-nil!
180
181 check-param 'first', 0, (list-or-vector list),
182 'list or vector', list.type
183
184 if list.value.length == 0
185 then const-nil!
186 else list.value[0]
187
188 'rest': fn (list) ->
189 if list.type == \const and list.value == \nil
190 return {type: \list, value: []}
191
192 check-param 'rest', 0, (list-or-vector list),
193 'list or vector', list.type
194
195 {type: \list, value: list.value.slice 1}
196
197 'throw': fn (value) -> throw value
198
199 'apply': fn (fn, ...params, list) ->
200 check-type 'apply', 0, \function, fn.type
201 if not list then runtime-error "apply expected at least two parameters"
202 check-param 'apply', params.length+1, (list-or-vector list),
203 'list or vector', list.type
204
205 unpack-tco fn.value.apply @, params ++ list.value
206
207 'map': fn (fn, list) ->
208 check-type 'map', 0, \function, fn.type
209 check-param 'map', 1, (list-or-vector list),
210 'list or vector', list.type
211
212 mapped-list = list.value |> map (value) ->
213 unpack-tco fn.value.apply @, [value]
214
215 {type: \list, value: mapped-list}
216
217 'nil?': fn (ast) -> const-bool (ast.type == \const and ast.value == \nil)
218 'true?': fn (ast) -> const-bool (ast.type == \const and ast.value == \true)
219 'false?': fn (ast) -> const-bool (ast.type == \const and ast.value == \false)
220 'symbol?': fn (ast) -> const-bool ast.type == \symbol
221
222 'symbol': fn (str) ->
223 check-type 'symbol', 0, \string, str.type
224 {type: \symbol, value: str.value}
225
226 'keyword': fn (str) ->
227 check-type 'keyword', 0, \string, str.type
228 {type: \keyword, value: ':' + str.value}
229
230 'keyword?': fn (ast) -> const-bool ast.type == \keyword
231
232 'number?': fn (ast) -> const-bool ast.type == \int
233 'fn?': fn (ast) -> const-bool (ast.type == \function and not ast.is_macro)
234 'macro?': fn (ast) -> const-bool (ast.type == \function and ast.is_macro)
235
236 'vector': fn (...params) -> {type: \vector, value: params}
237 'vector?': fn (ast) -> const-bool ast.type == \vector
238
239 'hash-map': fn (...params) -> list-to-map params
240
241 'map?': fn (ast) -> const-bool ast.type == \map
242
243 'assoc': fn (m, ...params) ->
244 check-type 'assoc', 0, \map, m.type
245
246 # Turn the params into a map, this is kind of hacky.
247 params-map = list-to-map params
248
249 # Copy the map by cloning (prototyping).
250 new-map = ^^m.value
251
252 for k, v of params-map.value
253 new-map[k] = v
254
255 {type: \map, value: new-map}
256
257 'dissoc': fn (m, ...keys) ->
258 check-type 'dissoc', 0, \map, m.type
259
260 # Convert keyword to map key strings.
261 str-keys = keys |> map map-keyword
262
263 new-map = m.value
264 |> obj-to-pairs
265 |> reject ([key, value]) -> key in str-keys
266 |> pairs-to-obj
267
268 {type: \map, value: new-map}
269
270 'get': fn (m, key) ->
271 if m.type == \const and m.value == \nil
272 then return const-nil!
273
274 check-type 'get', 0, \map, m.type
275 str-key = map-keyword key
276 value = m.value[str-key]
277 if value then value else const-nil!
278
279 'contains?': fn (m, key) ->
280 check-type 'contains?', 0, \map, m.type
281 str-key = map-keyword key
282 const-bool (str-key of m.value)
283
284 'keys': fn (m) ->
285 check-type 'keys', 0, \map, m.type
286 result = keys m.value |> map (key) ->
287 if key.startsWith keyword-prefix
288 then {type: \keyword, value: key.substring 1}
289 else {type: \string, value: key}
290 {type: \list, value: result}
291
292 'vals': fn (m) ->
293 check-type 'vals', 0, \map, m.type
294 {type: \list, value: values m.value}
295
296 'sequential?': fn (ast) -> const-bool list-or-vector ast
297
298 'with-meta': fn (ast, m) ->
299 ast with {meta: m}
300
301 'meta': fn (ast) ->
302 if ast.meta
303 then ast.meta
304 else const-nil!
305
306 'readline': fn (prompt) ->
307 check-type 'readline', 0, \string, prompt.type
308 result = readline prompt.value
309 if result?
310 then const-str result
311 else const-nil!
312
313 'time-ms': fn ->
314 const-int (new Date).getTime!
315
316 'conj': fn (list, ...params) ->
317 check-param 'conj', 0, (list-or-vector list),
318 'list or vector', list.type
319
320 if list.type == \list
321 type: \list
322 value: (reverse params) ++ list.value
323 else
324 type: \vector
325 value: list.value ++ params
326
327 'string?': fn (ast) -> const-bool ast.type == \string
328
329 'seq': fn (seq) ->
330 switch seq.type
331 | \list =>
332 if seq.value.length
333 then seq
334 else const-nil!
335 | \vector =>
336 if seq.value.length
337 then {type: \list, value: seq.value}
338 else const-nil!
339 | \string =>
340 if seq.value.length
341 then {type: \list, value: chars seq.value |> map const-str}
342 else const-nil!
343 | otherwise =>
344 if seq.type == \const and seq.value == \nil
345 then const-nil!
346 else runtime-error "unsupported type for 'seq': #{seq.type}"