Skip to content

Commit

Permalink
Introduce explicit declaration of function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
franko committed Feb 25, 2016
1 parent 7f3bb9c commit 6e41eb9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
11 changes: 11 additions & 0 deletions lang/lua-ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ function AST.function_decl(ast, path, args, body, proto)
return func_decl(path, body, args, proto.varargs, false, proto.firstline, proto.lastline)
end

function AST.func_parameters_decl(ast, args, vararg)
local params = {}
for i = 1, #args do
params[i] = ast:var_declare(args[i])
end
if vararg then
params[#params + 1] = ast:expr_vararg()
end
return params
end

function AST.chunk(ast, body, chunkname, firstline, lastline)
return build("Chunk", { body = body, chunkname = chunkname, firstline = firstline, lastline = lastline })
end
Expand Down
16 changes: 9 additions & 7 deletions lang/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -479,26 +479,26 @@ end
local function parse_params(ast, ls, needself)
lex_check(ls, "(")
local args = { }
local vararg = false
if needself then
args[1] = ast:var_declare("self")
args[1] = "self"
end
if ls.token ~= ")" then
repeat
if ls.token == 'TK_name' or (not LJ_52 and ls.token == 'TK_goto') then
local name = lex_str(ls)
args[#args+1] = ast:var_declare(name)
args[#args+1] = name
elseif ls.token == 'TK_dots' then
ls:next()
ls.fs.varargs = true
args[#args + 1] = ast:expr_vararg()
vararg = true
break
else
err_syntax(ls, "<name> or \"...\" expected")
end
until not lex_opt(ls, ',')
end
lex_check(ls, ")")
return args
return args, vararg
end

local function new_proto(ls, varargs)
Expand Down Expand Up @@ -528,7 +528,9 @@ function parse_body(ast, ls, line, needself)
ls.fs = new_proto(ls, false)
ast:fscope_begin()
ls.fs.firstline = line
local args = parse_params(ast, ls, needself)
local args, vararg = parse_params(ast, ls, needself)
local params = ast:func_parameters_decl(args, vararg)
ls.fs.varargs = vararg
local body = parse_block(ast, ls)
ast:fscope_end()
local proto = ls.fs
Expand All @@ -538,7 +540,7 @@ function parse_body(ast, ls, line, needself)
ls.fs.lastline = ls.linenumber
ls:next()
ls.fs = pfs
return args, body, proto
return params, body, proto
end

function parse_block(ast, ls, firstline)
Expand Down

0 comments on commit 6e41eb9

Please sign in to comment.