Skip to content

Commit fa06e61

Browse files
committed
delayed line
1 parent 0306608 commit fa06e61

File tree

3 files changed

+77
-17
lines changed

3 files changed

+77
-17
lines changed

moonscript/compile.lua

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ local concat, insert = table.concat, table.insert
3030
local pos_to_line, get_closest_line, trim, unpack = util.pos_to_line, util.get_closest_line, util.trim, util.unpack
3131
local mtype = util.moon.type
3232
local indent_char = " "
33-
local Line, Lines, Block, RootBlock
33+
local Line, DelayedLine, Lines, Block, RootBlock
3434
do
3535
local _parent_0 = nil
3636
local _base_0 = {
@@ -62,13 +62,13 @@ do
6262
end
6363
local posmap = self.posmap
6464
for i, l in ipairs(self) do
65-
local _exp_0 = type(l)
66-
if "table" == _exp_0 then
67-
local _
68-
_, line_no = l:flatten_posmap(line_no, out)
69-
elseif "string" == _exp_0 then
65+
local _exp_0 = mtype(l)
66+
if "string" == _exp_0 or DelayedLine == _exp_0 then
7067
line_no = line_no + 1
7168
out[line_no] = posmap[i]
69+
elseif "table" == _exp_0 then
70+
local _
71+
_, line_no = l:flatten_posmap(line_no, out)
7272
end
7373
end
7474
return out, line_no
@@ -82,7 +82,12 @@ do
8282
end
8383
for i = 1, #self do
8484
local l = self[i]
85-
local _exp_0 = type(l)
85+
local t = mtype(l)
86+
if t == DelayedLine then
87+
l = l:render()
88+
t = "string"
89+
end
90+
local _exp_0 = t
8691
if "string" == _exp_0 then
8792
if indent then
8893
insert(buffer, indent)
@@ -264,6 +269,47 @@ do
264269
end
265270
Line = _class_0
266271
end
272+
do
273+
local _parent_0 = nil
274+
local _base_0 = {
275+
prepare = function() end,
276+
render = function(self)
277+
self:prepare()
278+
return concat(self)
279+
end
280+
}
281+
_base_0.__index = _base_0
282+
if _parent_0 then
283+
setmetatable(_base_0, _parent_0.__base)
284+
end
285+
local _class_0 = setmetatable({
286+
__init = function(self, fn)
287+
self.prepare = fn
288+
end,
289+
__base = _base_0,
290+
__name = "DelayedLine",
291+
__parent = _parent_0
292+
}, {
293+
__index = function(cls, name)
294+
local val = rawget(_base_0, name)
295+
if val == nil and _parent_0 then
296+
return _parent_0[name]
297+
else
298+
return val
299+
end
300+
end,
301+
__call = function(cls, ...)
302+
local _self_0 = setmetatable({}, _base_0)
303+
cls.__init(_self_0, ...)
304+
return _self_0
305+
end
306+
})
307+
_base_0.__class = _class_0
308+
if _parent_0 and _parent_0.__inherited then
309+
_parent_0.__inherited(_parent_0, _class_0)
310+
end
311+
DelayedLine = _class_0
312+
end
267313
do
268314
local _parent_0 = nil
269315
local _base_0 = {

moonscript/compile.moon

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mtype = util.moon.type
1717

1818
indent_char = " "
1919

20-
local Line, Lines, Block, RootBlock
20+
local Line, DelayedLine, Lines, Block, RootBlock
2121

2222
-- a buffer for building up lines
2323
class Lines
@@ -34,26 +34,32 @@ class Lines
3434
item\render self
3535
when Block
3636
item\render self
37-
else
37+
else -- also captures DelayedLine
3838
@[#@ + 1] = item
3939
@
4040

4141
flatten_posmap: (line_no=0, out={}) =>
4242
posmap = @posmap
4343
for i, l in ipairs @
44-
switch type l
45-
when "table"
46-
_, line_no = l\flatten_posmap line_no, out
47-
when "string"
44+
switch mtype l
45+
when "string", DelayedLine
4846
line_no += 1
4947
out[line_no] = posmap[i]
48+
when "table"
49+
_, line_no = l\flatten_posmap line_no, out
5050

5151
out, line_no
5252

5353
flatten: (indent=nil, buffer={}) =>
5454
for i = 1, #@
5555
l = @[i]
56-
switch type l
56+
t = mtype l
57+
58+
if t == DelayedLine
59+
l = l\render!
60+
t = "string"
61+
62+
switch t
5763
when "string"
5864
insert buffer, indent if indent
5965
insert buffer, l
@@ -66,7 +72,7 @@ class Lines
6672

6773
insert buffer, "\n"
6874
last = l
69-
when "table"
75+
when "table" -- Lines
7076
l\flatten indent and indent .. indent_char or indent_char, buffer
7177
buffer
7278

@@ -134,6 +140,16 @@ class Line
134140
__tostring: =>
135141
"Line<#{util.dump(@)\sub 1, -2}>"
136142

143+
class DelayedLine
144+
new: (fn) =>
145+
@prepare = fn
146+
147+
prepare: ->
148+
149+
render: =>
150+
@prepare!
151+
concat @
152+
137153
class Block
138154
header: "do"
139155
footer: "end"

moonscript/data.moon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,5 @@ lua_keywords = Set{
3333
'until', 'while'
3434
}
3535

36-
3736
{ :Set, :Stack, :lua_keywords }
3837

39-

0 commit comments

Comments
 (0)