Skip to content

Commit e6527e2

Browse files
committed
global function
1 parent cc9d2e6 commit e6527e2

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

script/parser/compile.lua

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,7 @@ local function parseParams(params, isLambda)
24442444
return params
24452445
end
24462446

2447-
local function parseFunction(isLocal, isAction)
2447+
local function parseFunction(declareType, isAction)
24482448
local funcLeft = getPosition(Tokens[Index], 'left')
24492449
local funcRight = getPosition(Tokens[Index] + 7, 'right')
24502450
local func = {
@@ -2464,7 +2464,7 @@ local function parseFunction(isLocal, isAction)
24642464
local name = parseName()
24652465
if name then
24662466
local simple = parseSimple(name, true)
2467-
if isLocal then
2467+
if declareType == 'local' then
24682468
if simple == name then
24692469
createLocal(name)
24702470
else
@@ -2475,6 +2475,17 @@ local function parseFunction(isLocal, isAction)
24752475
finish = simple.finish,
24762476
}
24772477
end
2478+
elseif declareType == 'global' then
2479+
if simple == name then
2480+
createGlobalDeclare(name)
2481+
else
2482+
resolveName(name)
2483+
pushError {
2484+
type = 'UNEXPECT_GFUNC_NAME',
2485+
start = simple.start,
2486+
finish = simple.finish,
2487+
}
2488+
end
24782489
else
24792490
resolveName(name)
24802491
end
@@ -3305,7 +3316,7 @@ local function parseLocal()
33053316
end
33063317

33073318
if word == 'function' then
3308-
local func = parseFunction(true, true)
3319+
local func = parseFunction('local', true)
33093320
local name = func.name
33103321
if name then
33113322
func.name = nil
@@ -3346,7 +3357,7 @@ local function parseGlobal()
33463357

33473358
-- global function Name funcbody
33483359
if word == 'function' then
3349-
local func = parseFunction(false, true)
3360+
local func = parseFunction('global', true)
33503361
local name = func.name
33513362
if name then
33523363
func.name = nil
@@ -3355,7 +3366,6 @@ local function parseGlobal()
33553366
name.vstart = func.start
33563367
name.range = func.finish
33573368
func.parent = name
3358-
createGlobalDeclare(name)
33593369
pushActionIntoCurrentChunk(name)
33603370
return name
33613371
else

test/parser_test/syntax_check.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,3 +974,12 @@ global x, y, z = 1, 2, 3
974974
local n = x + y + z
975975
]]
976976
(nil)
977+
978+
TEST [[
979+
global function f()
980+
<!print!>(1)
981+
end
982+
]]
983+
{
984+
type = 'VARIABLE_NOT_DECLARED',
985+
}

0 commit comments

Comments
 (0)