1 local utils
= require('utils')
2 local types
= require('types')
3 local reader
= require('reader')
4 local printer
= require('printer')
5 local readline
= require('readline')
6 local socket
= require('socket')
8 local Nil
, List
, _pr_str
= types
.Nil
, types
.List
, printer
._pr_str
16 utils
.map(function(e
) return _pr_str(e
, true) end, arg
), " ")
21 utils
.map(function(e
) return _pr_str(e
, false) end, arg
), "")
26 utils
.map(function(e
) return _pr_str(e
, true) end, arg
), " "))
33 utils
.map(function(e
) return _pr_str(e
, false) end, arg
), " "))
40 for line
in io
.lines(file
) do
41 lines
[#lines
+1] = line
43 return table.concat(lines
, "\n") .. "\n"
46 function do_readline(prompt
)
47 local line
= readline
.readline(prompt
)
57 function assoc(hm
, ...)
58 return types
._assoc_BANG(types
.copy(hm
), unpack(arg
))
61 function dissoc(hm
, ...)
62 return types
._dissoc_BANG(types
.copy(hm
), unpack(arg
))
67 if res
== nil then return Nil
end
73 for k
,v
in pairs(hm
) do
81 for k
,v
in pairs(hm
) do
87 -- sequential functions
90 local new_lst
= lst
:slice(1)
91 table.insert(new_lst
, 1, a
)
92 return List
:new(new_lst
)
99 table.insert(new_lst
, arg
[i
][j
])
102 return List
:new(new_lst
)
105 function nth(seq
, idx
)
106 if idx
+1 <= #seq
then
109 types
.throw("nth: index out of range")
121 function apply(f
, ...)
122 if types
._malfunc_Q(f
) then
125 local args
= concat(types
.slice(arg
, 1, #arg
-1),
127 return f(unpack(args
))
131 if types
._malfunc_Q(f
) then
134 return List
:new(utils
.map(f
, lst
))
137 -- metadata functions
140 local m
= getmetatable(obj
)
141 if m
== nil or m
.meta
== nil then return Nil
end
145 function with_meta(obj
, meta
)
146 local new_obj
= types
.copy(obj
)
147 getmetatable(new_obj
).meta
= meta
153 function swap_BANG(atm
,f
,...)
154 if types
._malfunc_Q(f
) then
157 local args
= List
:new(arg
)
158 table.insert(args
, 1, atm
.val
)
159 atm
.val
= f(unpack(args
))
163 local function conj(obj
, ...)
164 local new_obj
= types
.copy(obj
)
165 if types
._list_Q(new_obj
) then
166 for i
, v
in ipairs(arg
) do
167 table.insert(new_obj
, 1, v
)
170 for i
, v
in ipairs(arg
) do
171 table.insert(new_obj
, v
)
178 ['='] = types
._equal_Q
,
181 ['nil?'] = function(a
) return a
==Nil
end,
182 ['true?'] = function(a
) return a
==true end,
183 ['false?'] = function(a
) return a
==false end,
184 symbol
= function(a
) return types
.Symbol
:new(a
) end,
185 ['symbol?'] = function(a
) return types
._symbol_Q(a
) end,
186 keyword
= function(a
) return "\177"..a
end,
187 ['keyword?'] = function(a
) return types
._keyword_Q(a
) end,
193 ['read-string'] = reader
.read_str
,
194 readline
= do_readline
,
197 ['<'] = function(a
,b
) return a
<b
end,
198 ['<='] = function(a
,b
) return a
<=b
end,
199 ['>'] = function(a
,b
) return a
>b
end,
200 ['>='] = function(a
,b
) return a
>=b
end,
201 ['+'] = function(a
,b
) return a
+b
end,
202 ['-'] = function(a
,b
) return a
-b
end,
203 ['*'] = function(a
,b
) return a
*b
end,
204 ['/'] = function(a
,b
) return math
.floor(a
/b
) end,
205 ['time-ms'] = function() return math
.floor(socket
.gettime() * 1000) end,
207 list
= function(...) return List
:new(arg
) end,
208 ['list?'] = function(a
) return types
._list_Q(a
) end,
209 vector
= function(...) return types
.Vector
:new(arg
) end,
210 ['vector?'] = types
._vector_Q
,
211 ['hash-map'] = types
.hash_map
,
212 ['map?'] = types
._hash_map_Q
,
216 ['contains?'] = function(a
,b
) return a
[b
] ~= nil end,
220 ['sequential?'] = types
._sequential_Q
,
225 rest
= function(a
) return List
:new(a
:slice(2)) end,
226 ['empty?'] = function(a
) return a
==Nil
or #a
== 0 end,
227 count
= function(a
) return #a
end,
233 ['with-meta'] = with_meta
,
234 atom
= function(a
) return types
.Atom
:new(a
) end,
235 ['atom?'] = types
._atom_Q
,
236 deref
= function(a
) return a
.val
end,
237 ['reset!'] = function(a
,b
) a
.val
= b
; return b
end,
238 ['swap!'] = swap_BANG
,