diff options
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/core/local.lua | 45 | ||||
-rw-r--r-- | server/src/core/value.lua | 29 | ||||
-rw-r--r-- | server/src/core/vm.txt | 52 |
3 files changed, 109 insertions, 17 deletions
diff --git a/server/src/core/local.lua b/server/src/core/local.lua new file mode 100644 index 00000000..7deac275 --- /dev/null +++ b/server/src/core/local.lua @@ -0,0 +1,45 @@ +local function getDefaultSource() + return { + start = 0, + finish = 0, + uri = '', + } +end + +local mt = {} +mt.__index = mt +mt.type = 'local' + +function mt:setValue(value) + if self.value then + self.value:mergeValue(value) + else + self.value = value + end +end + +function mt:getValue() + return self.value +end + +function mt:addInfo(tp, source) + self[#self+1] = { + type = tp, + source = source, + } +end + +function mt:eachInfo(callback) + for _, info in ipairs(self) do + callback(info) + end +end + +return function (name, source, value) + local self = setmetatable({ + name = name, + source = source or getDefaultSource(), + value = value, + }, mt) + return self +end diff --git a/server/src/core/value.lua b/server/src/core/value.lua index adb37071..fdd9d902 100644 --- a/server/src/core/value.lua +++ b/server/src/core/value.lua @@ -34,9 +34,6 @@ function mt:setType(tp, rate) if tp == 'any' or tp == 'nil' then rate = 0.0 end - if not self._type then - self._type = {} - end local current = self._type[tp] or 0.0 self._type[tp] = current + (1 - current) * rate end @@ -169,31 +166,29 @@ function mt:mergeValue(value) self._child[k] = v end end + if value._meta then + self._meta = value._meta + end + for _, info in ipairs(value) do + self[#self+1] = info + end end function mt:addInfo(tp, source) if source and not source.start then error('Miss start: ' .. table.dump(source)) end - if not self._info then - self._info = {} - end - self._info[#self._info+1] = { + self[#self] = { type = tp, source = source or getDefaultSource(), } end function mt:eachInfo(callback) - if not self._info then - return nil - end - for _, infos in pairs(self._info) do - for i = 1, #infos do - local res = callback(infos[i]) - if res ~= nil then - return res - end + for _, info in ipairs(self) do + local res = callback(info) + if res ~= nil then + return res end end return nil @@ -203,10 +198,10 @@ return function (tp, source, v) if tp == '...' then error('Value type cant be ...') end - -- TODO lib里的多类型 local self = setmetatable({ source = source or getDefaultSource(), _value = v, + _type = {}, }, mt) if type(tp) == 'table' then for i = 1, #tp do diff --git a/server/src/core/vm.txt b/server/src/core/vm.txt new file mode 100644 index 00000000..458a7c81 --- /dev/null +++ b/server/src/core/vm.txt @@ -0,0 +1,52 @@ +source -- ast生成的对象,是唯一对象 +{ + start: integer, -- source的开始位置,所有的活动source都应该由这个值,0表示是个马甲source + finish: integer, -- source的结束位置 + bindLocal: local, -- 绑定的local + bindField: field, -- 绑定的field + bindValue: value, -- 绑定的value + block: block, -- 所在的代码块 + uri: string, -- source所在的文件 + + isIndex: boolean, -- 是否作为构造表时的显性key + isSuffix: boolean, -- 是否作为simple的后缀 + parent: source, -- 作为simple后缀时的父source +} + +block -- ast生成的对象,是唯一对象 +{ + start: integer, -- block开始的位置,0表示主文件 + finish: integer, -- block结束的位置 +} + +local +{ + name: string, -- 局部变量一定有一个名字 + source: source, -- 声明该局部变量的source + value: value, -- 当前的value + + setValue(value), -- 设置value + getValue(value), -- 读取value + addInfo(tp, source), + eachInfo(function(info)), +} + +value +{ + source: source, -- 创建该值使用的source + + setType(type, rate), + getType(), + setChild(index, value), + getChild(index), + eachChild(function (value)), + rawSet(index, value), + rawGet(index), + setMetaTable(value), + getMetaMethod(name), + eachChild(function (value)), + rawEach(function (value)), + mergeValue(value), + addInfo(tp, source), + eachInfo(function (info)), +} |