Skip to content

Commit aff7970

Browse files
committed
Fix small issues in unparser after adding some typing
1 parent b687c99 commit aff7970

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

output.lua

+27-27
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ print("hi")
99

1010
A = 123
1111

12-
local function a([object Object], [object Object], [object Object])
12+
local function a(p1, p2, ...)
1313
print('b')
1414
end
1515
local function a()
@@ -20,14 +20,14 @@ end
2020
--[[ LOGGING ]]--[[]]
2121

2222

23-
local TostringValue, TupleToString, List, pprint = do
23+
local TostringValue, TupleToString, List, pprint do
2424
local tostring, pcall = tostring, pcall
25-
local function doPathKey([object Object])
25+
local function doPathKey(key)
2626
if type(key) == "string" and key:find("^[%a_][%w_]*$")then
2727
return'.' .. key
2828
end return'[' .. TostringValue(key, true) .. ']'
2929
end local prims = {["boolean"] = true["number"] = true["string"] = true["userdata"] = true}
30-
function TostringValue([object Object], [object Object], [object Object], [object Object])
30+
function TostringValue(val, short, tab, inline)
3131
tab = tab or 0
3232
local typ = type(val)
3333
local tss, ts = pcall(tostring, val)
@@ -72,13 +72,13 @@ local TostringValue, TupleToString, List, pprint = do
7272

7373
else return prefix .. typ .. '\32' .. ts end
7474
end
75-
function TupleToString([object Object])
75+
function TupleToString(...)
7676
local res = {...}
7777
for i = 1, select('#', ...) do
7878
res[i] = TostringValue(res[i])
7979
end return table.concat(res, ",\32")
8080
end
81-
function List([object Object])print()
81+
function List(tab)print()
8282
for k, v in pairs(tab) do
8383
print("-\32[" .. TostringValue(k, true) .. "]\32=", TostringValue(v, true) .. ';')
8484
end
@@ -105,7 +105,7 @@ local TostringValue, TupleToString, List, pprint = do
105105
end
106106
end
107107
BLOCKS = 0
108-
function pprint([object Object])
108+
function pprint(...)
109109
print('[' .. level() .. '|' .. BLOCKS .. ']', TupleToString(...))
110110
end
111111
end
@@ -114,7 +114,7 @@ function a()
114114
out:write()
115115
end
116116

117-
local function cprint([object Object], [object Object], [object Object])
117+
local function cprint(txt, col, maxl)
118118
local out = io.open("out.ps1", 'w')
119119
local ln, nl = 1
120120
txt = txt:gsub('\n', "\32\n")
@@ -130,7 +130,7 @@ local function cprint([object Object], [object Object], [object Object])
130130
--[[os.execute("powershell write-host -NoNewline -foreground "..col.." "..test)]]
131131
end
132132

133-
local function splitprint([object Object], [object Object])
133+
local function splitprint(txt, i)
134134
local begin, lns = txt:sub(1, i - 1), {}
135135
for line in begin:gmatch("[^\n]+") do
136136
lns[ #lns + 1] = line
@@ -141,7 +141,7 @@ end
141141

142142
--[[ PARSER ]]--[[]]
143143

144-
local function map([object Object])
144+
local function map(tab)
145145
local res = {}
146146
for i = 1, #tab do
147147
res[tab[i]] = true
@@ -169,7 +169,7 @@ local Binops, BinopsPriority = {
169169

170170

171171
local Parser = {}
172-
function Parser.new([object Object])
172+
function Parser.new(str)
173173
local lines = {1}
174174
for i = 1, #str do
175175
if str:byte(i, i) == 10 then
@@ -183,7 +183,7 @@ function Parser.new([object Object])
183183
end
184184

185185
--[[ Parser stuff]]do
186-
function Parser:Line([object Object])
186+
function Parser:Line(i)
187187
i = i or self.i
188188
local lines = self.lines
189189
for line = #lines, 1, -1 do
@@ -193,7 +193,7 @@ end
193193
end
194194
end error()
195195
end
196-
function Parser:Peek([object Object], [object Object])
196+
function Parser:Peek(func, ...)
197197
local old = self.i
198198
return self[func](self, ...)old
199199
end
@@ -221,24 +221,24 @@ end
221221
while self:Comment()do end
222222
return self:ActualTrim()
223223
end
224-
function Parser:String([object Object], [object Object])
224+
function Parser:String(str, noTrim)
225225
local i = noTrim and self.i or self:Trim()
226226
if self.str:sub(i, i + #str - 1) == str then
227227
self.i = i + #str return str
228228
end
229229
end
230-
function Parser:Match([object Object], [object Object])
230+
function Parser:Match(pat, noTrim)
231231
local i = noTrim and self.i or self:Trim()
232232
local a, b = self.str:find(pat, i)
233233
if a == i then
234234
self.i = b + 1
235235
return self.str:sub(a, b)
236236
end
237237
end
238-
function Parser:Find([object Object], [object Object])
238+
function Parser:Find(pat, plain)
239239
return self.str:find(pat, self.i, plain)
240240
end
241-
function Parser:Keyword([object Object])
241+
function Parser:Keyword(word)
242242
local i, str = self:Trim(), self.str
243243
local a, b = str:find("%w+", i)
244244
if a == i then
@@ -253,7 +253,7 @@ end
253253
end
254254

255255
--[[ Error handling]]do
256-
function Parser:Assert([object Object], [object Object], [object Object])
256+
function Parser:Assert(cond, err, ...)
257257
if cond then return end
258258
err = err:gsub("%%l", self:Line())
259259
error(err:format(...))
@@ -631,7 +631,7 @@ end
631631
assert(expr, "Expected\32an expression")
632632
assert(self:Keyword("then"), "Expected\32`then`")
633633
blocks[ #blocks + 1] = {expr(self:Block())}
634-
end local otherwise =
634+
end local otherwise
635635
if self:Keyword("else")then
636636
otherwise = self:Block()
637637
end
@@ -652,7 +652,7 @@ end
652652
assert(self:String(','), "Expected\32a `,`")
653653
local limit = self:Expression()
654654
assert(limit, "Expected\32an expression")
655-
local step =
655+
local step
656656
if self:String(',')then
657657
step = self:Expression()
658658
assert(step, "Expected\32an expression")
@@ -771,7 +771,7 @@ end
771771
else self.i = old local expr, o = self:Peek("Expression")if expr and (expr.Type == "FunctionCall" or expr.Type == "FunctionSelfCall")then return expr end self.i = o local vars = self:Varlist()if vars then assert(self:String('\61'), "Expected\32`=`")local exprs = self:Explist()assert(exprs, "Expected\32an expression")return{["Line"] = start["Type"] = "Assignment"["Variables"] = vars["Expressions"] = exprs}end self.i = o end
772772
self.i = old
773773
end
774-
function Parser:CheckBinop([object Object])
774+
function Parser:CheckBinop(expr)
775775
local left = expr.Left
776776
if left.Priority then
777777
if left.Priority < expr.Priority then
@@ -787,7 +787,7 @@ end
787787
["FunctionSelfCall"] = true
788788
["Brackets"] = true}
789789

790-
function Parser:PostExpression([object Object])
790+
function Parser:PostExpression(prev)
791791
local start = self:Line()
792792
local binop, old = self:Peek("Binop")
793793
local isPrefix = IsPrefix[prev.Type]
@@ -958,11 +958,11 @@ end
958958

959959
end
960960
local escapes = {['a'] = 'a'['b'] = '\8'['f'] = '\12'['n'] = '\n'['r'] = '\13'['t'] = '\t'['v'] = '\11'['\13'] = '\n'["\\"] = "\\\\"}
961-
local function escaped([object Object])
961+
local function escaped(str)
962962
str = str:gsub("\\(%d%d?%d?)", string.char)
963963
return
964964

965-
str:gsub("\\x(%d+%d+)", function([object Object])return string.char(tonumber(c, 16))end):gsub("\\(.)", function([object Object])return escapes[c] or c end)
965+
str:gsub("\\x(%d+%d+)", function(c)return string.char(tonumber(c, 16))end):gsub("\\(.)", function(c)return escapes[c] or c end)
966966
end
967967
function Parser:StringConstant()
968968
local q = self:Match("['\"]")
@@ -1078,7 +1078,7 @@ end
10781078
local old = self.i
10791079
local prefix = self:Prefixexp()
10801080
if N prefix then self.i = old return end
1081-
local methodname =
1081+
local methodname
10821082
if self:String(':')then
10831083
methodname = self:Name()
10841084
assert(methodname, "Expected\32a name")
@@ -1104,7 +1104,7 @@ end
11041104
self.i = o return
11051105
end return m
11061106
end
1107-
function Parser:Namelist([object Object])
1107+
function Parser:Namelist(vararg)
11081108
local name = self:Name()
11091109
local names = {name}
11101110
if name then
@@ -1166,7 +1166,7 @@ xpcall(function()
11661166
print("Took", os.clock() - start, "to\32parse")
11671167
local f = io.open("output.lua", 'w')
11681168
f:write(TostringValue(res))f:close()
1169-
end, function([object Object])
1169+
end, function(e)
11701170
print("ERROR:", e)
11711171
print("stack\32traceback:")
11721172
print(debug.traceback():match(".-\n.-\10(.*)"))

output.typed.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

22
local a = "def"
33

4-
local b =
4+
local b
5+
6+
function test(par, ...)
7+
print(par, ...)
8+
end

src/unparser.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ export class Unparser {
170170
this.currentLine += ', ';
171171
this.unparseExpression(e);
172172
});
173-
this.currentLine += ' = ';
174173
first = expr.expressions[0];
175-
if (first) this.unparseExpression(first);
174+
if (first) {
175+
this.currentLine += ' = ';
176+
this.unparseExpression(first);
177+
}
176178
expr.expressions.slice(1).forEach((e) => {
177179
this.currentLine += ', ';
178180
this.unparseExpression(e);
@@ -250,7 +252,7 @@ export class Unparser {
250252
this.unparseExpression(expr.variable);
251253
}
252254
this.currentLine += '(';
253-
this.currentLine += expr.parameters.join(', ');
255+
this.currentLine += expr.parameters.map(v => v.name).join(', ');
254256
this.currentLine += ')';
255257
this.unparseExpressions(expr.chunk.block);
256258
this.ensureLine(this.line(expr.endIndex));

0 commit comments

Comments
 (0)