Skip to content

Commit db1081f

Browse files
committed
new posmaps
1 parent 05d83f3 commit db1081f

File tree

3 files changed

+179
-226
lines changed

3 files changed

+179
-226
lines changed

moonscript/compile.lua

Lines changed: 98 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ local Line, Lines
2323
Lines = (function()
2424
local _parent_0 = nil
2525
local _base_0 = {
26+
mark_pos = function(self, pos, line)
27+
if line == nil then
28+
line = #self
29+
end
30+
if not (self.posmap[line]) then
31+
self.posmap[line] = pos
32+
end
33+
end,
2634
add = function(self, item)
2735
local _exp_0 = mtype(item)
2836
if Line == _exp_0 then
@@ -34,6 +42,55 @@ Lines = (function()
3442
end
3543
return self
3644
end,
45+
flatten_posmap = function(self, line_no, out)
46+
if line_no == nil then
47+
line_no = 0
48+
end
49+
if out == nil then
50+
out = { }
51+
end
52+
local posmap = self.posmap
53+
for i, l in ipairs(self) do
54+
local _exp_0 = type(l)
55+
if "table" == _exp_0 then
56+
local _
57+
_, line_no = l:flatten_posmap(line_no, out)
58+
elseif "string" == _exp_0 then
59+
line_no = line_no + 1
60+
out[line_no] = posmap[i]
61+
end
62+
end
63+
return out, line_no
64+
end,
65+
flatten = function(self, indent, buffer)
66+
if indent == nil then
67+
indent = nil
68+
end
69+
if buffer == nil then
70+
buffer = { }
71+
end
72+
for i = 1, #self do
73+
local l = self[i]
74+
local _exp_0 = type(l)
75+
if "string" == _exp_0 then
76+
if indent then
77+
insert(buffer, indent)
78+
end
79+
insert(buffer, l)
80+
if "string" == type(self[i + 1]) then
81+
local lc = l:sub(-1)
82+
if (lc == ")" or lc == "]") and self[i + 1]:sub(1, 1) == "(" then
83+
insert(buffer, ";")
84+
end
85+
end
86+
insert(buffer, "\n")
87+
local last = l
88+
elseif "table" == _exp_0 then
89+
l:flatten(indent and indent .. indent_char or indent_char, buffer)
90+
end
91+
end
92+
return buffer
93+
end,
3794
__tostring = function(self)
3895
local strip
3996
strip = function(t)
@@ -91,8 +148,12 @@ end)()
91148
Line = (function()
92149
local _parent_0 = nil
93150
local _base_0 = {
151+
pos = nil,
94152
_append_single = function(self, item)
95-
if util.moon.type(item) == Line then
153+
if Line == mtype(item) then
154+
if not (self.pos) then
155+
self.pos = item.pos
156+
end
96157
local _list_0 = item
97158
for _index_0 = 1, #_list_0 do
98159
value = _list_0[_index_0]
@@ -110,6 +171,7 @@ Line = (function()
110171
insert(self, delim)
111172
end
112173
end
174+
return nil
113175
end,
114176
append = function(self, ...)
115177
local _list_0 = {
@@ -125,7 +187,8 @@ Line = (function()
125187
local current = { }
126188
local add_current
127189
add_current = function()
128-
return buffer:add(concat(current))
190+
buffer:add(concat(current))
191+
return buffer:mark_pos(self.pos)
129192
end
130193
local _list_0 = self
131194
for _index_0 = 1, #_list_0 do
@@ -207,9 +270,6 @@ Block = (function()
207270
end
208271
return "Block<" .. tostring(h) .. "> <- " .. tostring(self.parent)
209272
end,
210-
line_table = function(self)
211-
return self._posmap
212-
end,
213273
set = function(self, name, value)
214274
self._state[name] = value
215275
end,
@@ -239,7 +299,7 @@ Block = (function()
239299
local name = _list_0[_index_0]
240300
local is_local = false
241301
local real_name
242-
local _exp_0 = util.moon.type(name)
302+
local _exp_0 = mtype(name)
243303
if LocalName == _exp_0 then
244304
is_local = true
245305
real_name = name:get_name(self)
@@ -274,7 +334,7 @@ Block = (function()
274334
if select("#", ...) == 0 then
275335
value = true
276336
end
277-
if util.moon.type(name) == NameProxy then
337+
if NameProxy == mtype(name) then
278338
name = name:get_name(self)
279339
end
280340
self._names[name] = value
@@ -328,27 +388,13 @@ Block = (function()
328388
})
329389
return name
330390
end,
331-
mark_pos = function(self, line_no, node)
332-
do
333-
local pos = node[-1]
334-
if pos then
335-
self.last_pos = pos
336-
if not (self._posmap[line_no]) then
337-
self._posmap[line_no] = pos
338-
end
339-
end
340-
end
341-
end,
342-
append_posmap = function(self, map)
343-
print("appending pos", self)
344-
self._posmap[#self._posmap + 1] = map
345-
end,
346391
add = function(self, item)
347392
self._lines:add(item)
348393
return item
349394
end,
350395
render = function(self, buffer)
351396
buffer:add(self.header)
397+
buffer:mark_pos(self.pos)
352398
if self.next then
353399
buffer:add(self._lines)
354400
self.next:render(buffer)
@@ -358,6 +404,7 @@ Block = (function()
358404
else
359405
buffer:add(self._lines)
360406
buffer:add(self.footer)
407+
buffer:mark_pos(self.pos)
361408
end
362409
end
363410
return buffer
@@ -394,7 +441,18 @@ Block = (function()
394441
if not fn then
395442
error("Failed to compile value: " .. dump.value(node))
396443
end
397-
return fn(self, node, ...)
444+
local out = fn(self, node, ...)
445+
if type(node) == "table" and node[-1] then
446+
if type(out) == "string" then
447+
do
448+
local _with_0 = Line()
449+
_with_0:append(out)
450+
out = _with_0
451+
end
452+
end
453+
out.pos = node[-1]
454+
end
455+
return out
398456
end,
399457
values = function(self, values, delim)
400458
delim = delim or ', '
@@ -420,17 +478,14 @@ Block = (function()
420478
end
421479
node = self.transform.statement(node)
422480
local before = #self._lines
423-
local added
481+
local result
424482
do
425483
local fn = line_compile[ntype(node)]
426484
if fn then
427-
local out = fn(self, node, ...)
428-
if out then
429-
added = self:add(out)
430-
end
485+
result = fn(self, node, ...)
431486
else
432487
if has_value(node) then
433-
added = self:stm({
488+
result = self:stm({
434489
"assign",
435490
{
436491
"_"
@@ -440,30 +495,15 @@ Block = (function()
440495
}
441496
})
442497
else
443-
added = self:add(self:value(node))
498+
result = self:value(node)
444499
end
445500
end
446501
end
447-
if added then
448-
print("added " .. tostring(#self._lines - before) .. " lines")
449-
local list
450-
if Line == mtype(added) then
451-
list = added
452-
else
453-
list = {
454-
added
455-
}
456-
end
457-
local next_block = block_iterator(list)
458-
for l = before + 1, #self._lines do
459-
if "table" == type(self._lines[l]) then
460-
local block = next_block()
461-
block._posmap.num_lines = #block._lines
462-
self._posmap[l] = block._posmap
463-
else
464-
self:mark_pos(l, node)
465-
end
502+
if result then
503+
if type(node) == "table" and type(result) == "table" and node[-1] then
504+
result.pos = node[-1]
466505
end
506+
self:add(result)
467507
end
468508
return nil
469509
end,
@@ -495,7 +535,6 @@ Block = (function()
495535
__init = function(self, parent, header, footer)
496536
self.parent, self.header, self.footer = parent, header, footer
497537
self._lines = Lines()
498-
self._posmap = { }
499538
self._names = { }
500539
self._state = { }
501540
self._listeners = { }
@@ -555,65 +594,6 @@ Block = (function()
555594
end
556595
return _class_0
557596
end)()
558-
local flatten_lines
559-
flatten_lines = function(lines, indent, buffer)
560-
if indent == nil then
561-
indent = nil
562-
end
563-
if buffer == nil then
564-
buffer = { }
565-
end
566-
for i = 1, #lines do
567-
local l = lines[i]
568-
local _exp_0 = type(l)
569-
if "string" == _exp_0 then
570-
if indent then
571-
insert(buffer, indent)
572-
end
573-
insert(buffer, l)
574-
if "string" == type(lines[i + 1]) then
575-
local lc = l:sub(-1)
576-
if (lc == ")" or lc == "]") and lines[i + 1]:sub(1, 1) == "(" then
577-
insert(buffer, ";")
578-
end
579-
end
580-
insert(buffer, "\n")
581-
local last = l
582-
elseif "table" == _exp_0 then
583-
flatten_lines(l, indent and indent .. indent_char or indent_char, buffer)
584-
end
585-
end
586-
return buffer
587-
end
588-
local flatten_posmap
589-
flatten_posmap = function(posmap, dl, out)
590-
if dl == nil then
591-
dl = 0
592-
end
593-
if out == nil then
594-
out = { }
595-
end
596-
for k, v in pairs(posmap) do
597-
local _continue_0 = false
598-
repeat
599-
if "string" == type(k) then
600-
_continue_0 = true
601-
break
602-
end
603-
if "table" == type(v) then
604-
flatten_posmap(v, k - 1 + dl, out)
605-
dl = dl + (v.num_lines - 1)
606-
else
607-
out[k + dl] = v
608-
end
609-
_continue_0 = true
610-
until true
611-
if not _continue_0 then
612-
break
613-
end
614-
end
615-
return out
616-
end
617597
local debug_posmap
618598
debug_posmap = function(posmap, fname, lua_code)
619599
if fname == nil then
@@ -662,7 +642,7 @@ RootBlock = (function()
662642
return "RootBlock<>"
663643
end,
664644
render = function(self)
665-
local buffer = flatten_lines(self._lines)
645+
local buffer = self._lines:flatten()
666646
if buffer[#buffer] == "\n" then
667647
buffer[#buffer] = nil
668648
end
@@ -732,31 +712,27 @@ tree = function(tree, scope)
732712
local line = _list_0[_index_0]
733713
scope:stm(line)
734714
end
735-
return scope:render()
736715
end)
737-
local success, result = coroutine.resume(runner)
716+
local success, err = coroutine.resume(runner)
738717
if not success then
739718
local error_msg
740-
if type(result) == "table" then
741-
local error_type = result[1]
719+
if type(err) == "table" then
720+
local error_type = err[1]
742721
if error_type == "user-error" then
743-
error_msg = result[2]
722+
error_msg = err[2]
744723
else
745724
error_msg = error("Unknown error thrown", util.dump(error_msg))
746725
end
747726
else
748727
error_msg = concat({
749-
result,
728+
err,
750729
debug.traceback(runner)
751730
}, "\n")
752731
end
753732
return nil, error_msg, scope.last_pos
754733
else
755-
local raw_posmap = scope:line_table()
756-
local posmap = flatten_posmap(raw_posmap)
757-
print(util.dump(raw_posmap))
758-
print(util.dump(posmap))
759-
print(debug_posmap(posmap, "scrap.moon", result))
760-
return result, posmap
734+
local lua_code = scope:render()
735+
local posmap = scope._lines:flatten_posmap()
736+
return lua_code, posmap
761737
end
762738
end

0 commit comments

Comments
 (0)