Skip to content

Commit ad5a1ec

Browse files
committed
stash
1 parent 98270a5 commit ad5a1ec

File tree

2 files changed

+21
-64
lines changed

2 files changed

+21
-64
lines changed

GLOBAL_SEMANTIC_REQUIREMENTS.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,28 +233,16 @@ global x <close> -- 错误:global 不能有 close 属性
233233

234234
**P0 - 高优先级(确定性错误):**
235235
1. **GLOBAL_CLOSE_ATTRIBUTE** - global 不能有 close 属性(✅ 已在 parser 实现)
236-
2. **GLOBAL_DUPLICATE_INIT** - 同一文件中同一全局变量的多次初始化
237-
3. **ASSIGN_TO_CONST** - 赋值给 const 全局变量
236+
2. **ASSIGN_TO_CONST** - 赋值给 const 全局变量
238237

239238
**P1 - 中优先级(作用域错误):**
240-
4. **VARIABLE_NOT_DECLARED** - 在显式 global 作用域内使用未声明变量
239+
3. **VARIABLE_NOT_DECLARED** - 在显式 global 作用域内使用未声明变量
241240

242241
**P2 - 低优先级(警告):**
243242
5. **POSSIBLE_GLOBAL_REDEFINITION** - 可能的跨文件重复初始化(警告)
244243

245244
#### 详细实现说明:
246245

247-
**GLOBAL_DUPLICATE_INIT**:
248-
```lua
249-
-- 检测逻辑:
250-
-- 1. 扫描 AST,找到所有 setglobal 节点
251-
-- 2. 对于有 value 的节点(初始化),记录变量名和位置
252-
-- 3. 如果同一变量名出现第二次初始化,报错
253-
254-
global X = 10 -- 记录:X 在此初始化
255-
global X = 20 -- 错误!X 已在第 1 行初始化
256-
```
257-
258246
**ASSIGN_TO_CONST**:
259247
```lua
260248
-- 检测逻辑:

script/parser/compile.lua

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,41 +2907,23 @@ local function bindValue(n, v, index, lastValue, isLocal, isSet)
29072907
if n.type == 'setglobal' then
29082908
local gi = State.globalInfo
29092909
local nameKey = n[1]
2910-
if n.isGlobalDecl then
2911-
-- duplicate initialization check (only for global declarations with value)
2910+
-- assignment / definition in strict global scope
2911+
if gi.active then
29122912
local info = gi.names[nameKey]
2913-
if v then
2914-
if info and info.initialized then
2915-
pushError {
2916-
type = 'GLOBAL_DUPLICATE_INIT',
2917-
start = n.start,
2918-
finish = n.finish,
2919-
}
2920-
else
2921-
if info then
2922-
info.initialized = true
2923-
end
2924-
end
2913+
if not gi.hasAll and not info then
2914+
pushError {
2915+
type = 'UNDEFINED_IN_GLOBAL_SCOPE',
2916+
start = n.start,
2917+
finish = n.finish,
2918+
}
29252919
end
2926-
else
2927-
-- assignment / definition in strict global scope
2928-
if gi.active then
2929-
local info = gi.names[nameKey]
2930-
if not gi.hasAll and not info then
2931-
pushError {
2932-
type = 'UNDEFINED_IN_GLOBAL_SCOPE',
2933-
start = n.start,
2934-
finish = n.finish,
2935-
}
2936-
end
2937-
local isConst = (info and info.const) or gi.allConst
2938-
if isConst then
2939-
pushError {
2940-
type = 'ASSIGN_CONST_GLOBAL',
2941-
start = n.start,
2942-
finish = n.finish,
2943-
}
2944-
end
2920+
local isConst = (info and info.const) or gi.allConst
2921+
if isConst then
2922+
pushError {
2923+
type = 'ASSIGN_CONST_GLOBAL',
2924+
start = n.start,
2925+
finish = n.finish,
2926+
}
29452927
end
29462928
end
29472929
end
@@ -3223,21 +3205,10 @@ local function parseGlobal()
32233205
name.vstart = func.start
32243206
name.range = func.finish
32253207
func.parent = name
3226-
name.isGlobalDecl = true
32273208
local gi = State.globalInfo
32283209
gi.active = true
32293210
local nameKey = name[1]
3230-
local info = gi.names[nameKey]
3231-
if info and info.initialized then
3232-
pushError {
3233-
type = 'GLOBAL_DUPLICATE_INIT',
3234-
start = name.start,
3235-
finish = name.finish,
3236-
}
3237-
else
3238-
gi.names[nameKey] = gi.names[nameKey] or { declared = true, const = false, initialized = false }
3239-
gi.names[nameKey].initialized = true
3240-
end
3211+
gi.names[nameKey] = gi.names[nameKey] or { declared = true, const = false }
32413212
pushActionIntoCurrentChunk(name)
32423213
return name
32433214
else
@@ -3293,10 +3264,9 @@ local function parseGlobal()
32933264
finish = name.finish,
32943265
[1] = name[1],
32953266
}
3296-
glob.isGlobalDecl = true
32973267
local gi = State.globalInfo
32983268
gi.active = true
3299-
gi.names[glob[1]] = gi.names[glob[1]] or { declared = true, const = false, initialized = false }
3269+
gi.names[glob[1]] = gi.names[glob[1]] or { declared = true, const = false }
33003270

33013271
if attrs then
33023272
glob.attrs = attrs
@@ -3356,8 +3326,7 @@ local function parseGlobal()
33563326
finish = nameN.finish,
33573327
[1] = nameN[1],
33583328
}
3359-
gN.isGlobalDecl = true
3360-
gi.names[gN[1]] = gi.names[gN[1]] or { declared = true, const = false, initialized = false }
3329+
gi.names[gN[1]] = gi.names[gN[1]] or { declared = true, const = false }
33613330
if attrsN then
33623331
gN.attrs = attrsN
33633332
attrsN.parent = gN
@@ -4406,7 +4375,7 @@ local function initState(lua, version, options)
44064375
end
44074376
-- Lua 5.5 global keyword semantic tracking (syntax-level errors)
44084377
state.globalInfo = {
4409-
names = {}, -- map name -> { declared=true, const=bool, initialized=bool }
4378+
names = {}, -- map name -> { declared=true, const=bool }
44104379
hasAll = false, -- global * encountered
44114380
allConst = false, -- global <const> * encountered
44124381
active = false, -- any global declaration activates strict global scope

0 commit comments

Comments
 (0)