Skip to content

Commit 05d83f3

Browse files
committed
add Lines
1 parent 6d52237 commit 05d83f3

File tree

4 files changed

+135
-93
lines changed

4 files changed

+135
-93
lines changed

moonscript/compile.lua

Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,75 @@ end
1919
local concat, insert = table.concat, table.insert
2020
local pos_to_line, get_line, get_closest_line, trim = util.pos_to_line, util.get_line, util.get_closest_line, util.trim
2121
local mtype = util.moon.type
22-
local Line
22+
local Line, Lines
23+
Lines = (function()
24+
local _parent_0 = nil
25+
local _base_0 = {
26+
add = function(self, item)
27+
local _exp_0 = mtype(item)
28+
if Line == _exp_0 then
29+
item:render(self)
30+
elseif Block == _exp_0 then
31+
item:render(self)
32+
else
33+
self[#self + 1] = item
34+
end
35+
return self
36+
end,
37+
__tostring = function(self)
38+
local strip
39+
strip = function(t)
40+
if "table" == type(t) then
41+
return (function()
42+
local _accum_0 = { }
43+
local _len_0 = 0
44+
local _list_0 = t
45+
for _index_0 = 1, #_list_0 do
46+
local v = _list_0[_index_0]
47+
_len_0 = _len_0 + 1
48+
_accum_0[_len_0] = strip(v)
49+
end
50+
return _accum_0
51+
end)()
52+
else
53+
return t
54+
end
55+
end
56+
return "Lines<" .. tostring(util.dump(strip(self)):sub(1, -2)) .. ">"
57+
end
58+
}
59+
_base_0.__index = _base_0
60+
if _parent_0 then
61+
setmetatable(_base_0, _parent_0.__base)
62+
end
63+
local _class_0 = setmetatable({
64+
__init = function(self)
65+
self.posmap = { }
66+
end,
67+
__base = _base_0,
68+
__name = "Lines",
69+
__parent = _parent_0
70+
}, {
71+
__index = function(cls, name)
72+
local val = rawget(_base_0, name)
73+
if val == nil and _parent_0 then
74+
return _parent_0[name]
75+
else
76+
return val
77+
end
78+
end,
79+
__call = function(cls, ...)
80+
local _self_0 = setmetatable({}, _base_0)
81+
cls.__init(_self_0, ...)
82+
return _self_0
83+
end
84+
})
85+
_base_0.__class = _class_0
86+
if _parent_0 and _parent_0.__inherited then
87+
_parent_0.__inherited(_parent_0, _class_0)
88+
end
89+
return _class_0
90+
end)()
2391
Line = (function()
2492
local _parent_0 = nil
2593
local _base_0 = {
@@ -57,21 +125,21 @@ Line = (function()
57125
local current = { }
58126
local add_current
59127
add_current = function()
60-
return insert(buffer, concat(current))
128+
return buffer:add(concat(current))
61129
end
62130
local _list_0 = self
63131
for _index_0 = 1, #_list_0 do
64132
local chunk = _list_0[_index_0]
65133
local _exp_0 = mtype(chunk)
66134
if Block == _exp_0 then
67-
local _list_1 = chunk:render({ })
135+
local _list_1 = chunk:render(Lines())
68136
for _index_1 = 1, #_list_1 do
69137
local block_chunk = _list_1[_index_1]
70138
if "string" == type(block_chunk) then
71139
insert(current, block_chunk)
72140
else
73141
add_current()
74-
insert(buffer, block_chunk)
142+
buffer:add(block_chunk)
75143
current = { }
76144
end
77145
end
@@ -85,7 +153,7 @@ Line = (function()
85153
return buffer
86154
end,
87155
__tostring = function(self)
88-
return "Line<" .. tostring(self:render()) .. ">"
156+
return "Line<" .. tostring(util.dump(self):sub(1, -2)) .. ">"
89157
end
90158
}
91159
_base_0.__index = _base_0
@@ -123,7 +191,7 @@ Line = (function()
123191
return _class_0
124192
end)()
125193
Block = (function()
126-
local add_to_buffer, block_iterator
194+
local block_iterator
127195
local _parent_0 = nil
128196
local _base_0 = {
129197
header = "do",
@@ -275,44 +343,21 @@ Block = (function()
275343
print("appending pos", self)
276344
self._posmap[#self._posmap + 1] = map
277345
end,
278-
add_raw = function(self, item)
279-
return insert(self._lines, item)
280-
end,
281-
add = function(self, line)
282-
local _exp_0 = util.moon.type(line)
283-
if "string" == _exp_0 then
284-
insert(self._lines, line)
285-
elseif Block == _exp_0 then
286-
line:render(self._lines)
287-
elseif Line == _exp_0 then
288-
line:render(self._lines)
289-
else
290-
error("Adding unknown item")
291-
end
292-
return line
346+
add = function(self, item)
347+
self._lines:add(item)
348+
return item
293349
end,
294350
render = function(self, buffer)
295-
add_to_buffer(buffer, self.header)
296-
local lines = (function()
297-
local _accum_0 = { }
298-
local _len_0 = 0
299-
local _list_0 = self._lines
300-
for _index_0 = 1, #_list_0 do
301-
local l = _list_0[_index_0]
302-
_len_0 = _len_0 + 1
303-
_accum_0[_len_0] = l
304-
end
305-
return _accum_0
306-
end)()
351+
buffer:add(self.header)
307352
if self.next then
308-
insert(buffer, lines)
353+
buffer:add(self._lines)
309354
self.next:render(buffer)
310355
else
311-
if #lines == 0 and "string" == type(buffer[#buffer]) then
312-
buffer[#buffer] = buffer[#buffer] .. (" " .. (unpack(add_to_buffer({ }, self.footer))))
356+
if #self._lines == 0 and "string" == type(buffer[#buffer]) then
357+
buffer[#buffer] = buffer[#buffer] .. (" " .. (unpack(Lines():add(self.footer))))
313358
else
314-
insert(buffer, lines)
315-
add_to_buffer(buffer, self.footer)
359+
buffer:add(self._lines)
360+
buffer:add(self.footer)
316361
end
317362
end
318363
return buffer
@@ -438,7 +483,7 @@ Block = (function()
438483
"lines",
439484
self._lines
440485
}
441-
self._lines = { }
486+
self._lines = Lines()
442487
return self:stms(fn(lines))
443488
end
444489
}
@@ -449,7 +494,7 @@ Block = (function()
449494
local _class_0 = setmetatable({
450495
__init = function(self, parent, header, footer)
451496
self.parent, self.header, self.footer = parent, header, footer
452-
self._lines = { }
497+
self._lines = Lines()
453498
self._posmap = { }
454499
self._names = { }
455500
self._state = { }
@@ -494,15 +539,6 @@ Block = (function()
494539
})
495540
_base_0.__class = _class_0
496541
local self = _class_0
497-
add_to_buffer = function(buffer, line)
498-
local _exp_0 = mtype(line)
499-
if Line == _exp_0 then
500-
line:render(buffer)
501-
else
502-
insert(buffer, line)
503-
end
504-
return buffer
505-
end
506542
block_iterator = function(list)
507543
return coroutine.wrap(function()
508544
local _list_0 = list

moonscript/compile.moon

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,34 @@ mtype = util.moon.type
2121
export tree, value, format_error
2222
export Block, RootBlock
2323

24-
-- buffer for building up a line
24+
local Line, Lines
25+
26+
-- a buffer for building up lines
27+
class Lines
28+
new: =>
29+
@posmap = {}
30+
31+
-- append a line or lines to the buffer
32+
add: (item) =>
33+
switch mtype item
34+
when Line
35+
item\render self
36+
when Block
37+
item\render self
38+
else
39+
@[#@ + 1] = item
40+
@
41+
42+
__tostring: =>
43+
-- strip non-array elements
44+
strip = (t) ->
45+
if "table" == type t
46+
[strip v for v in *t]
47+
else
48+
t
49+
50+
-- copy with only array elements
51+
"Lines<#{util.dump(strip @)\sub 1, -2}>"
2552
class Line
2653
_append_single: (item) =>
2754
if util.moon.type(item) == Line
@@ -39,22 +66,22 @@ class Line
3966
@_append_single item for item in *{...}
4067
nil
4168

42-
-- todo: remove concats from here
69+
-- todo: try to remove concats from here
4370
render: (buffer) =>
4471
current = {}
4572

4673
add_current = ->
47-
insert buffer, concat current
74+
buffer\add concat current
4875

4976
for chunk in *@
5077
switch mtype chunk
5178
when Block
52-
for block_chunk in *chunk\render{}
79+
for block_chunk in *chunk\render Lines!
5380
if "string" == type block_chunk
5481
insert current, block_chunk
5582
else
5683
add_current!
57-
insert buffer, block_chunk
84+
buffer\add block_chunk
5885
current = {}
5986
else
6087
insert current, chunk
@@ -64,7 +91,8 @@ class Line
6491

6592
buffer
6693

67-
__tostring: => "Line<#{@render!}>"
94+
__tostring: =>
95+
"Line<#{util.dump(@)\sub 1, -2}>"
6896

6997
class Block
7098
header: "do"
@@ -82,8 +110,9 @@ class Block
82110
"Block<#{h}> <- " .. tostring @parent
83111

84112
new: (@parent, @header, @footer) =>
85-
@_lines = {}
86-
@_posmap = {}
113+
@_lines = Lines!
114+
115+
@_posmap = {} -- todo: kill me
87116
@_names = {}
88117
@_state = {}
89118
@_listeners = {}
@@ -186,10 +215,6 @@ class Block
186215
print "appending pos", self
187216
@_posmap[#@_posmap + 1] = map
188217

189-
-- add raw text as new line
190-
add_raw: (item) =>
191-
insert @_lines, item
192-
193218
-- append_line_table: (sub_table, offset) =>
194219
-- offset = offset + @current_line
195220

@@ -211,44 +236,24 @@ class Block
211236
-- current = current.next
212237

213238
-- add a line object
214-
add: (line) =>
215-
-- print "adding", line
216-
switch util.moon.type line
217-
when "string"
218-
insert @_lines, line
219-
when Block
220-
line\render @_lines
221-
when Line
222-
line\render @_lines
223-
else
224-
error "Adding unknown item"
225-
line
226-
227-
add_to_buffer = (buffer, line) ->
228-
switch mtype line
229-
when Line
230-
line\render buffer
231-
else
232-
insert buffer, line
233-
buffer
239+
add: (item) =>
240+
@_lines\add item
241+
item
234242

235243
-- todo: pass in buffer as argument
236244
render: (buffer) =>
237-
add_to_buffer buffer, @header
238-
239-
-- copy lines
240-
lines = [l for l in *@_lines]
245+
buffer\add @header
241246

242247
if @next
243-
insert buffer, lines
248+
buffer\add @_lines
244249
@next\render buffer
245250
else
246251
-- join an empty block into a single line
247-
if #lines == 0 and "string" == type buffer[#buffer]
248-
buffer[#buffer] ..= " " .. (unpack add_to_buffer {}, @footer)
252+
if #@_lines == 0 and "string" == type buffer[#buffer]
253+
buffer[#buffer] ..= " " .. (unpack Lines!\add @footer)
249254
else
250-
insert buffer, lines
251-
add_to_buffer buffer, @footer
255+
buffer\add @_lines
256+
buffer\add @footer
252257

253258
buffer
254259

@@ -330,9 +335,10 @@ class Block
330335

331336
splice: (fn) =>
332337
lines = {"lines", @_lines}
333-
@_lines = {}
338+
@_lines = Lines!
334339
@stms fn lines
335340

341+
-- move this into Lines
336342
flatten_lines = (lines, indent=nil, buffer={}) ->
337343
for i = 1, #lines
338344
l = lines[i]

moonscript/compile/statement.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ line_compile = {
1818
local _list_0 = node[2]
1919
for _index_0 = 1, #_list_0 do
2020
local line = _list_0[_index_0]
21-
self:add_raw(line)
21+
self:add(line)
2222
end
2323
end,
2424
declare = function(self, node)

moonscript/compile/statement.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ line_compile =
1717

1818
lines: (node) =>
1919
for line in *node[2]
20-
@add_raw line
20+
@add line
2121

2222
declare: (node) =>
2323
names = node[2]

0 commit comments

Comments
 (0)