1 local insert, remove, concat
4 insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat
6 local unpack = unpack or table.unpack
7 local LuaCode, NomsuCode, Source
11 __tostring = function(self)
12 return "@" .. tostring(self.filename) .. "[" .. tostring(self.start) .. tostring(self.stop and ':' .. self.stop or '') .. "]"
14 as_lua = function(self)
15 return "Source(" .. tostring(self.filename:as_lua()) .. ", " .. tostring(self.start) .. tostring(self.stop and ', ' .. self.stop or '') .. ")"
17 __eq = function(self, other)
18 return getmetatable(self) == getmetatable(other) and self.filename == other.filename and self.start == other.start and self.stop == other.stop
20 __lt = function(self, other)
21 assert(self.filename == other.filename, "Cannot compare sources from different files")
22 if self.start == other.start then
23 return (self.stop or self.start) < (other.stop or other.start)
25 return self.start < other.start
28 __le = function(self, other)
29 assert(self.filename == other.filename, "Cannot compare sources from different files")
30 if self.start == other.start then
31 return (self.stop or self.start) <= (other.stop or other.start)
33 return self.start <= other.start
36 __add = function(self, offset)
37 if type(self) == 'number' then
38 offset, self = self, offset
40 if type(offset) ~= 'number' then
41 error("Cannot add Source and " .. tostring(type(offset)))
44 return Source(self.filename, self.start + offset, self.stop)
47 _base_0.__index = _base_0
48 _class_0 = setmetatable({
49 __init = function(self, filename, start, stop)
50 self.filename, self.start, self.stop = filename, start, stop
56 __call = function(cls, ...)
57 local _self_0 = setmetatable({}, _base_0)
58 cls.__init(_self_0, ...)
62 _base_0.__class = _class_0
64 self.from_string = function(self, str)
65 local filename, start, stop = str:match("^@(.-)%[(%d+):(%d+)%]$")
66 if not (filename) then
67 filename, start = str:match("^@(.-)%[(%d+)%]$")
69 return self(filename or str, tonumber(start or 1), tonumber(stop))
71 self.is_instance = function(self, x)
72 return type(x) == 'table' and x.__class == self
81 if self.__str == nil then
82 local buff, indent = { }, 0
83 local match, gsub, rep
86 match, gsub, rep = _obj_0.match, _obj_0.gsub, _obj_0.rep
88 for i, b in ipairs(self.bits) do
89 if type(b) == 'string' then
91 local spaces = match(b, "\n([ ]*)[^\n]*$")
99 b = gsub(b, "\n", "\n" .. rep(" ", indent))
104 self.__str = concat(buff, "")
108 last = function(self, n)
110 return self.__str:sub(-n, -1)
113 for i = #self.bits, 1, -1 do
114 local b = self.bits[i]
115 last = (type(b) == 'string' and b:sub(-(n - #last)) or b:last(n - #last)) .. last
122 first = function(self, n)
124 return self.__str:sub(1, n)
127 local _list_0 = self.bits
128 for _index_0 = 1, #_list_0 do
129 local b = _list_0[_index_0]
130 first = first .. (type(b) == 'string' and b:sub(1, n - #first + 1) or b:first(n - #first + 1))
137 __tostring = function(self)
140 as_lua = function(self)
142 return tostring(self.__class.__name) .. ":from(" .. tostring(concat({
143 tostring(self.source):as_lua(),
147 local _list_0 = self.bits
148 for _index_0 = 1, #_list_0 do
149 local b = _list_0[_index_0]
150 _accum_0[_len_0] = b:as_lua()
157 return tostring(self.__class.__name) .. "(" .. tostring(concat((function()
160 local _list_0 = self.bits
161 for _index_0 = 1, #_list_0 do
162 local b = _list_0[_index_0]
163 _accum_0[_len_0] = b:as_lua()
167 end)(), ", ")) .. ")"
170 __len = function(self)
175 local _list_0 = self.bits
176 for _index_0 = 1, #_list_0 do
177 local b = _list_0[_index_0]
182 match = function(self, ...)
183 return self:text():match(...)
185 gmatch = function(self, ...)
186 return self:text():gmatch(...)
188 dirty = function(self)
190 self._trailing_line_len = nil
191 if self._is_multiline == false then
192 self._is_multiline = nil
195 add = function(self, ...)
196 local n = select("#", ...)
197 local match = string.match
198 local bits = self.bits
200 local _continue_0 = false
202 local b = select(i, ...)
203 assert(b, "code bit is nil")
204 assert(not Source:is_instance(b), "code bit is a Source")
212 if not _continue_0 then
218 trailing_line_len = function(self)
219 if self._trailing_line_len == nil then
220 self._trailing_line_len = #self:text():match("[^\n]*$")
222 return self._trailing_line_len
224 is_multiline = function(self)
225 if self._is_multiline == nil then
226 local match = string.match
227 self._is_multiline = false
228 local _list_0 = self.bits
229 for _index_0 = 1, #_list_0 do
230 local b = _list_0[_index_0]
231 if type(b) == 'string' then
232 if match(b, '\n') then
233 self._is_multiline = true
236 elseif b:is_multiline() then
237 self._is_multiline = true
242 return self._is_multiline
244 concat_add = function(self, values, joiner, wrapping_joiner)
245 wrapping_joiner = wrapping_joiner or joiner
246 local match = string.match
247 local bits = self.bits
249 for i = 1, #values do
252 if line_len > 80 then
253 bits[#bits + 1] = wrapping_joiner
256 bits[#bits + 1] = joiner
260 if type(b) ~= 'string' then
263 local line = b:match("\n([^\n]*)$")
267 line_len = line_len + #b
272 prepend = function(self, ...)
273 local n = select("#", ...)
274 local bits = self.bits
275 for i = #bits + n, n + 1, -1 do
276 bits[i] = bits[i - n]
279 local b = select(i, ...)
280 if type(b) ~= 'string' then
287 parenthesize = function(self)
292 _base_0.__index = _base_0
293 _class_0 = setmetatable({
294 __init = function(self, ...)
302 __call = function(cls, ...)
303 local _self_0 = setmetatable({}, _base_0)
304 cls.__init(_self_0, ...)
308 _base_0.__class = _class_0
309 local self = _class_0
310 self.from = function(self, source, ...)
311 local inst = self(...)
312 if type(source) == 'string' then
313 source = Source:from_string(source)
318 self.is_instance = function(self, x)
319 return type(x) == 'table' and x.__class == self
325 local _parent_0 = Code
327 __tostring = Code.__tostring,
328 as_lua = Code.as_lua,
330 add_free_vars = function(self, vars)
331 if not (#vars > 0) then
337 local _list_0 = self.free_vars
338 for _index_0 = 1, #_list_0 do
339 local v = _list_0[_index_0]
340 local _key_0, _val_0 = {
343 _tbl_0[_key_0] = _val_0
347 for _index_0 = 1, #vars do
348 local var = vars[_index_0]
349 assert(type(var) == 'string')
350 if not (seen[var]) then
351 self.free_vars[#self.free_vars + 1] = var
357 remove_free_vars = function(self, vars)
361 vars = vars or self:get_free_vars()
362 if not (#vars > 0) then
366 for _index_0 = 1, #vars do
367 local var = vars[_index_0]
368 assert(type(var) == 'string')
376 lua, stack[#stack] = stack[#stack], nil
377 for i = #lua.free_vars, 1, -1 do
378 local free_var = lua.free_vars[i]
379 if removals[free_var] then
380 remove(lua.free_vars, i)
383 local _list_0 = lua.bits
384 for _index_0 = 1, #_list_0 do
385 local b = _list_0[_index_0]
386 if type(b) ~= 'string' then
387 stack[#stack + 1] = b
393 get_free_vars = function(self)
394 local vars, seen = { }, { }
396 gather_from = function(self)
397 local _list_0 = self.free_vars
398 for _index_0 = 1, #_list_0 do
399 local var = _list_0[_index_0]
400 if not (seen[var]) then
402 vars[#vars + 1] = var
405 local _list_1 = self.bits
406 for _index_0 = 1, #_list_1 do
407 local bit = _list_1[_index_0]
408 if not (type(bit) == 'string') then
416 declare_locals = function(self, to_declare)
417 if to_declare == nil then
420 to_declare = to_declare or self:get_free_vars()
421 if #to_declare > 0 then
422 self:remove_free_vars(to_declare)
423 self:prepend("local " .. tostring(concat(to_declare, ", ")) .. ";\n")
427 make_offset_table = function(self)
428 assert(self.source, "This code doesn't have a source")
429 local lua_to_nomsu, nomsu_to_lua = { }, { }
431 walk = function(lua, pos)
432 local _list_0 = lua.bits
433 for _index_0 = 1, #_list_0 do
434 local b = _list_0[_index_0]
435 if type(b) == 'string' then
437 lua_to_nomsu[pos] = lua.source.start
438 nomsu_to_lua[lua.source.start] = pos
448 nomsu_filename = self.source.filename,
449 lua_filename = tostring(self.source) .. ".lua",
450 lua_file = self:text(),
451 lua_to_nomsu = lua_to_nomsu,
452 nomsu_to_lua = nomsu_to_lua
455 parenthesize = function(self)
460 _base_0.__index = _base_0
461 setmetatable(_base_0, _parent_0.__base)
462 _class_0 = setmetatable({
463 __init = function(self, ...)
464 _class_0.__parent.__init(self, ...)
471 __index = function(cls, name)
472 local val = rawget(_base_0, name)
474 local parent = rawget(cls, "__parent")
482 __call = function(cls, ...)
483 local _self_0 = setmetatable({}, _base_0)
484 cls.__init(_self_0, ...)
488 _base_0.__class = _class_0
489 if _parent_0.__inherited then
490 _parent_0.__inherited(_parent_0, _class_0)
496 local _parent_0 = Code
498 __tostring = Code.__tostring,
499 as_lua = Code.as_lua,
502 _base_0.__index = _base_0
503 setmetatable(_base_0, _parent_0.__base)
504 _class_0 = setmetatable({
505 __init = function(self, ...)
506 return _class_0.__parent.__init(self, ...)
509 __name = "NomsuCode",
512 __index = function(cls, name)
513 local val = rawget(_base_0, name)
515 local parent = rawget(cls, "__parent")
523 __call = function(cls, ...)
524 local _self_0 = setmetatable({}, _base_0)
525 cls.__init(_self_0, ...)
529 _base_0.__class = _class_0
530 if _parent_0.__inherited then
531 _parent_0.__inherited(_parent_0, _class_0)
535 Code.__base.add_1_joined_with = assert(Code.__base.concat_add)
536 Code.__base.add = assert(Code.__base.add)
539 NomsuCode = NomsuCode,