diff options
Diffstat (limited to 'server-beta/src')
-rw-r--r-- | server-beta/src/core/engineer.lua | 15 | ||||
-rw-r--r-- | server-beta/src/parser/compile.lua | 6 | ||||
-rw-r--r-- | server-beta/src/parser/guide.lua | 51 |
3 files changed, 70 insertions, 2 deletions
diff --git a/server-beta/src/core/engineer.lua b/server-beta/src/core/engineer.lua index 8afbeac2..0def3ff1 100644 --- a/server-beta/src/core/engineer.lua +++ b/server-beta/src/core/engineer.lua @@ -26,6 +26,19 @@ function mt:eachRefAsLocal(obj, callback) end end +--- 查找所有域的引用 +function mt:eachRefAsField(obj, callback) + local node = obj.node + guide.eachFieldOf(node, guide.getKeyName(obj), function (value) + local tp = value.type + if tp == 'setglobal' or tp == 'setfield' then + callback(value, 'set') + elseif tp == 'getglobal' or tp == 'getfield' then + callback(value, 'get') + end + end) +end + --- 查找所有引用 function mt:eachRef(obj, callback) if obj.type == 'local' then @@ -33,7 +46,7 @@ function mt:eachRef(obj, callback) elseif obj.type == 'getlocal' or obj.type == 'setlocal' then self:eachRefAsLocal(obj.loc, callback) elseif obj.type == 'setglobal' or obj.type == 'getglobal' then - self:eachRefAsGlobal(obj, callback) + self:eachRefAsField(obj, callback) end end diff --git a/server-beta/src/parser/compile.lua b/server-beta/src/parser/compile.lua index 90b3f269..fe4198f0 100644 --- a/server-beta/src/parser/compile.lua +++ b/server-beta/src/parser/compile.lua @@ -43,7 +43,11 @@ local function getValue(obj) if vref then return vref end - addValue(obj, {}) + if guide.isLiteral(obj) then + addValue(obj, obj) + else + addValue(obj, {}) + end return obj.vref end diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua index 69b49f85..fdd29be7 100644 --- a/server-beta/src/parser/guide.lua +++ b/server-beta/src/parser/guide.lua @@ -1,5 +1,6 @@ local error = error local type = type +local next = next _ENV = nil @@ -58,6 +59,16 @@ m.childMap = { ['list'] = {'#'}, } +--- 是否是字面量 +function m.isLiteral(obj) + local tp = obj.type + return tp == 'nil' + or tp == 'boolean' + or tp == 'string' + or tp == 'number' + or tp == 'table' +end + --- 寻找所在函数 function m.getParentFunction(obj) for _ = 1, 1000 do @@ -338,6 +349,7 @@ function m.lineRange(lines, row) return line.start + 1, line.finish end +--- 获取对象作为key时的名字 function m.getKeyName(obj) if obj.type == 'getglobal' or obj.type == 'setglobal' then return 'string|' .. obj[1] @@ -346,4 +358,43 @@ function m.getKeyName(obj) end end +--- 获取对象所有field的key与valueObj +function m.eachField(obj, callback) + local vref = obj.vref + if not vref then + return + end + for i = 1, #vref do + local v = vref[i] + local child = v.child + if child then + for fieldName, cvref in next, child do + for j = 1, #cvref do + callback(fieldName, cvref[j]) + end + end + end + end +end + +--- 获取对象所有指定field的key与valueObj +function m.eachFieldOf(obj, field, callback) + local vref = obj.vref + if not vref then + return + end + for i = 1, #vref do + local v = vref[i] + local child = v.child + if child then + local cvref = child[field] + if cvref then + for j = 1, #cvref do + callback(cvref[j]) + end + end + end + end +end + return m |