summaryrefslogtreecommitdiff
path: root/script/src/emmy
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-22 23:26:32 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-22 23:26:32 +0800
commitd0ff66c9abe9d6abbca12fd811e0c3cb69c1033a (patch)
treebb34518d70b85de7656dbdbe958dfa221a3ff3b3 /script/src/emmy
parent0a2c2ad15e1ec359171fb0dd4c72e57c5b66e9ba (diff)
downloadlua-language-server-d0ff66c9abe9d6abbca12fd811e0c3cb69c1033a.zip
整理一下目录结构
Diffstat (limited to 'script/src/emmy')
-rw-r--r--script/src/emmy/alias.lua42
-rw-r--r--script/src/emmy/arrayType.lua37
-rw-r--r--script/src/emmy/class.lua61
-rw-r--r--script/src/emmy/field.lua45
-rw-r--r--script/src/emmy/funcType.lua64
-rw-r--r--script/src/emmy/generic.lua27
-rw-r--r--script/src/emmy/init.lua1
-rw-r--r--script/src/emmy/manager.lua221
-rw-r--r--script/src/emmy/param.lua70
-rw-r--r--script/src/emmy/return.lua36
-rw-r--r--script/src/emmy/tableType.lua40
-rw-r--r--script/src/emmy/type.lua55
-rw-r--r--script/src/emmy/typeUnit.lua51
13 files changed, 750 insertions, 0 deletions
diff --git a/script/src/emmy/alias.lua b/script/src/emmy/alias.lua
new file mode 100644
index 00000000..be47cb4f
--- /dev/null
+++ b/script/src/emmy/alias.lua
@@ -0,0 +1,42 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyAlias
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.alias'
+
+function mt:getName()
+ return self.name
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:bindType(type)
+ if type then
+ self._bindType = type
+ else
+ return self._bindType
+ end
+end
+
+function mt:addEnum(enum)
+ self._enum[#self._enum+1] = enum
+end
+
+function mt:eachEnum(callback)
+ for _, enum in ipairs(self._enum) do
+ callback(enum)
+ end
+end
+
+return function (manager, source)
+ local self = setmetatable({
+ name = source[1][1],
+ source = source.id,
+ _manager = manager,
+ _enum = {},
+ }, mt)
+ return self
+end
diff --git a/script/src/emmy/arrayType.lua b/script/src/emmy/arrayType.lua
new file mode 100644
index 00000000..c5d73500
--- /dev/null
+++ b/script/src/emmy/arrayType.lua
@@ -0,0 +1,37 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyArrayType
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.arrayType'
+
+function mt:getType()
+ return 'table'
+end
+
+function mt:getName()
+ return self.name
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:setValue(value)
+ self.value = value
+ self._child:setValue(value)
+end
+
+function mt:getValue()
+ return self.value
+end
+
+return function (manager, source, child)
+ local self = setmetatable({
+ name = child:getName(),
+ source = source.id,
+ _child = child,
+ _manager = manager,
+ }, mt)
+ return self
+end
diff --git a/script/src/emmy/class.lua b/script/src/emmy/class.lua
new file mode 100644
index 00000000..6728e2c4
--- /dev/null
+++ b/script/src/emmy/class.lua
@@ -0,0 +1,61 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyClass
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.class'
+
+function mt:getType()
+ return self.name
+end
+
+function mt:getName()
+ return self.name
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:setValue(value)
+ self.value = value
+end
+
+function mt:getValue()
+ return self.value
+end
+
+function mt:eachChild(callback)
+ self._manager:eachClass(self.name, function (obj)
+ if obj.type == 'emmy.typeUnit' then
+ callback(obj)
+ end
+ end)
+end
+
+function mt:addField(field)
+ if not self._fields then
+ self._fields = {}
+ end
+ self._fields[#self._fields+1] = field
+end
+
+function mt:eachField(callback)
+ if not self._fields then
+ return
+ end
+ ---@param field EmmyField
+ for _, field in ipairs(self._fields) do
+ callback(field)
+ end
+end
+
+return function (manager, name, extends, source)
+ local self = setmetatable({
+ name = name,
+ source = source.id,
+ extends = extends,
+ _manager = manager,
+ }, mt)
+ return self
+end
diff --git a/script/src/emmy/field.lua b/script/src/emmy/field.lua
new file mode 100644
index 00000000..f9e9cbf6
--- /dev/null
+++ b/script/src/emmy/field.lua
@@ -0,0 +1,45 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyField
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.field'
+
+function mt:getName()
+ return self.name
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:bindType(type)
+ if type then
+ self._bindType = type
+ else
+ return self._bindType
+ end
+end
+
+function mt:bindValue(value)
+ if value then
+ self._bindValue = value
+ else
+ if self._bindValue then
+ if not self._bindValue:getSource() then
+ self._bindValue = nil
+ end
+ end
+ return self._bindValue
+ end
+end
+
+return function (manager, source)
+ local self = setmetatable({
+ name = source[2][1],
+ source = source.id,
+ visible = source[1],
+ _manager = manager,
+ }, mt)
+ return self
+end
diff --git a/script/src/emmy/funcType.lua b/script/src/emmy/funcType.lua
new file mode 100644
index 00000000..2c073fe2
--- /dev/null
+++ b/script/src/emmy/funcType.lua
@@ -0,0 +1,64 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyFunctionType
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.functionType'
+
+function mt:getType()
+ return 'function'
+end
+
+function mt:getName()
+ return 'function'
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:setValue(value)
+ self.value = value
+end
+
+function mt:getValue()
+ return self.value
+end
+
+function mt:addParam(name, type)
+ self._params[#self._params+1] = { name, type }
+end
+
+function mt:addReturn(type)
+ self._returns[#self._returns+1] = type
+end
+
+function mt:eachParam(callback)
+ for _, data in ipairs(self._params) do
+ callback(data[1], data[2])
+ end
+end
+
+function mt:eachReturn(callback)
+ for _, rtn in ipairs(self._returns) do
+ callback(rtn)
+ end
+end
+
+function mt:bindFunction(func)
+ if func then
+ self._bindFunction = func
+ else
+ return self._bindFunction
+ end
+end
+
+return function (manager, source)
+ local self = setmetatable({
+ source = source.id,
+ _params = {},
+ _returns = {},
+ _manager = manager,
+ }, mt)
+ return self
+end
diff --git a/script/src/emmy/generic.lua b/script/src/emmy/generic.lua
new file mode 100644
index 00000000..82fd0c4b
--- /dev/null
+++ b/script/src/emmy/generic.lua
@@ -0,0 +1,27 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyGeneric
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.generic'
+
+function mt:getName()
+ return self.name:getName()
+end
+
+function mt:setValue(value)
+ self._value = value
+end
+
+function mt:getValue()
+ return self._value
+end
+
+return function (manager, defs)
+ for _, def in ipairs(defs) do
+ setmetatable(def, mt)
+ def._manager = manager
+ def._binds = {}
+ end
+ return defs
+end
diff --git a/script/src/emmy/init.lua b/script/src/emmy/init.lua
new file mode 100644
index 00000000..769c9e05
--- /dev/null
+++ b/script/src/emmy/init.lua
@@ -0,0 +1 @@
+return require 'vm.emmy.emmy'
diff --git a/script/src/emmy/manager.lua b/script/src/emmy/manager.lua
new file mode 100644
index 00000000..c80e05df
--- /dev/null
+++ b/script/src/emmy/manager.lua
@@ -0,0 +1,221 @@
+local listMgr = require 'vm.list'
+local sourceMgr = require 'vm.source'
+local newClass = require 'emmy.class'
+local newType = require 'emmy.type'
+local newTypeUnit = require 'emmy.typeUnit'
+local newAlias = require 'emmy.alias'
+local newParam = require 'emmy.param'
+local newReturn = require 'emmy.return'
+local newField = require 'emmy.field'
+local newGeneric = require 'emmy.generic'
+local newArrayType = require 'emmy.arrayType'
+local newTableType = require 'emmy.tableType'
+local newFuncType = require 'emmy.funcType'
+
+local mt = {}
+mt.__index = mt
+mt.__name = 'emmyMgr'
+
+function mt:flushClass(name)
+ local list = self._class[name]
+ if not list then
+ return
+ end
+ local version = listMgr.getVersion()
+ if version == list.version then
+ return
+ end
+ for srcId in pairs(list) do
+ if not listMgr.get(srcId) then
+ list[srcId] = nil
+ end
+ end
+ if not next(list) then
+ self._class[name] = nil
+ return
+ end
+ list.version = version
+end
+
+function mt:eachClassByName(name, callback)
+ self:flushClass(name)
+ local list = self._class[name]
+ if not list then
+ return
+ end
+ for k, class in pairs(list) do
+ if k ~= 'version' then
+ local res = callback(class)
+ if res ~= nil then
+ return res
+ end
+ end
+ end
+end
+
+function mt:eachClass(...)
+ local n = select('#', ...)
+ if n == 1 then
+ local callback = ...
+ for name in pairs(self._class) do
+ local res = self:eachClassByName(name, callback)
+ if res ~= nil then
+ return res
+ end
+ end
+ else
+ local name, callback = ...
+ return self:eachClassByName(name, callback)
+ end
+end
+
+function mt:getClass(name)
+ self:flushClass(name)
+ local list = self._class[name]
+ local version = listMgr.getVersion()
+ if not list then
+ list = {
+ version = version,
+ }
+ self._class[name] = list
+ end
+ return list
+end
+
+function mt:newClass(name, extends, source)
+ local list = self:getClass(name)
+ list[source.id] = newClass(self, name, extends, source)
+ return list[source.id]
+end
+
+function mt:addClass(source)
+ local className = source[1][1]
+ local extends = source[2] and source[2][1]
+ local class = self:newClass(className, extends, source)
+ return class
+end
+
+function mt:addType(source)
+ local typeObj = newType(self, source)
+ for i, obj in ipairs(source) do
+ local typeUnit = newTypeUnit(self, obj)
+ local className = obj[1]
+ if className then
+ local list = self:getClass(className)
+ list[source.id] = typeUnit
+ end
+ typeUnit:setParent(typeObj)
+ typeObj._childs[i] = typeUnit
+ obj:set('emmy.typeUnit', typeUnit)
+ end
+ return typeObj
+end
+
+function mt:addArrayType(source)
+ local typeObj = self:addType(source)
+ local arrayTypeObj = newArrayType(self, source, typeObj)
+ return arrayTypeObj
+end
+
+function mt:addTableType(source, keyType, valueType)
+ local typeObj = newTableType(self, source, keyType, valueType)
+ return typeObj
+end
+
+function mt:addFunctionType(source)
+ local typeObj = newFuncType(self, source)
+ return typeObj
+end
+
+function mt:addAlias(source, typeObj)
+ local aliasName = source[1][1]
+ local aliasObj = newAlias(self, source)
+ aliasObj:bindType(typeObj)
+ local list = self:getClass(aliasName)
+ list[source.id] = aliasObj
+ for i = 3, #source do
+ aliasObj:addEnum(source[i])
+ end
+ return aliasObj
+end
+
+function mt:addParam(source, bind)
+ local paramObj = newParam(self, source)
+ if bind.type == 'emmy.generic' then
+ paramObj:bindGeneric(bind)
+ else
+ paramObj:bindType(bind)
+ self:eachClass(bind:getType(), function (class)
+ if class.type == 'emmy.alias' then
+ class:eachEnum(function (enum)
+ paramObj:addEnum(enum)
+ end)
+ end
+ end)
+ end
+ for i = 3, #source do
+ paramObj:addEnum(source[i])
+ end
+ paramObj:setOption(source.option)
+ return paramObj
+end
+
+function mt:addReturn(source, bind, name)
+ local returnObj = newReturn(self, source, name)
+ if bind then
+ if bind.type == 'emmy.generic' then
+ returnObj:bindGeneric(bind)
+ else
+ returnObj:bindType(bind)
+ end
+ end
+ return returnObj
+end
+
+function mt:addField(source, typeObj, value)
+ local fieldObj = newField(self, source)
+ fieldObj:bindType(typeObj)
+ fieldObj:bindValue(value)
+ return fieldObj
+end
+
+function mt:addGeneric(defs)
+ local genericObj = newGeneric(self, defs)
+ return genericObj
+end
+
+function mt:remove()
+end
+
+function mt:count()
+ local count = 0
+ for _, list in pairs(self._class) do
+ for k in pairs(list) do
+ if k ~= 'version' then
+ count = count + 1
+ end
+ end
+ end
+ return count
+end
+
+return function ()
+ ---@class emmyMgr
+ local self = setmetatable({
+ _class = {},
+ }, mt)
+
+ local source = sourceMgr.dummy()
+ self:newClass('any', nil, source)
+ self:newClass('string', 'any', source)
+ self:newClass('number', 'any', source)
+ self:newClass('integer', 'number', source)
+ self:newClass('boolean', 'any', source)
+ self:newClass('table', 'any', source)
+ self:newClass('function', 'any', source)
+ self:newClass('nil', 'any', source)
+ self:newClass('userdata', 'any', source)
+ self:newClass('thread', 'any', source)
+
+ return self
+end
diff --git a/script/src/emmy/param.lua b/script/src/emmy/param.lua
new file mode 100644
index 00000000..12ac0633
--- /dev/null
+++ b/script/src/emmy/param.lua
@@ -0,0 +1,70 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyParam
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.param'
+
+function mt:getName()
+ return self.name
+end
+
+function mt:getType()
+ if self._bindType then
+ return self._bindType:getType()
+ else
+ return 'any'
+ end
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:bindType(type)
+ if type then
+ self._bindType = type
+ else
+ return self._bindType
+ end
+end
+
+function mt:bindGeneric(generic)
+ if generic then
+ self._bindGeneric = generic
+ else
+ return self._bindGeneric
+ end
+end
+
+function mt:addEnum(enum)
+ self._enum[#self._enum+1] = enum
+end
+
+function mt:eachEnum(callback)
+ for _, enum in ipairs(self._enum) do
+ callback(enum)
+ end
+end
+
+function mt:setOption(option)
+ self._option = option
+end
+
+function mt:getOption()
+ return self._option
+end
+
+return function (manager, source)
+ local self = setmetatable({
+ source = source.id,
+ _manager = manager,
+ _enum = {},
+ }, mt)
+ if source.type == 'emmyParam' then
+ self.name = source[1][1]
+ elseif source.type == 'emmyVararg' then
+ self.name = '...'
+ end
+ return self
+end
diff --git a/script/src/emmy/return.lua b/script/src/emmy/return.lua
new file mode 100644
index 00000000..1fc97923
--- /dev/null
+++ b/script/src/emmy/return.lua
@@ -0,0 +1,36 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyReturn
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.return'
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:bindType(type)
+ if type then
+ self._bindType = type
+ else
+ return self._bindType
+ end
+end
+
+function mt:bindGeneric(generic)
+ if generic then
+ self._bindGeneric = generic
+ else
+ return self._bindGeneric
+ end
+end
+
+return function (manager, source, name)
+ local self = setmetatable({
+ source = source.id,
+ name = name and name[1],
+ option = source.option,
+ _manager = manager,
+ }, mt)
+ return self
+end
diff --git a/script/src/emmy/tableType.lua b/script/src/emmy/tableType.lua
new file mode 100644
index 00000000..3882cd61
--- /dev/null
+++ b/script/src/emmy/tableType.lua
@@ -0,0 +1,40 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyTableType
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.tableType'
+
+function mt:getType()
+ return 'table'
+end
+
+function mt:getKeyType()
+ return self.keyType
+end
+
+function mt:getValueType()
+ return self.valueType
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:setValue(value)
+ self.value = value
+end
+
+function mt:getValue()
+ return self.value
+end
+
+return function (manager, source, keyType, valueType)
+ local self = setmetatable({
+ source = source.id,
+ keyType = keyType,
+ valueType = valueType,
+ _manager = manager,
+ }, mt)
+ return self
+end
diff --git a/script/src/emmy/type.lua b/script/src/emmy/type.lua
new file mode 100644
index 00000000..b7e451b2
--- /dev/null
+++ b/script/src/emmy/type.lua
@@ -0,0 +1,55 @@
+local listMgr = require 'vm.list'
+
+local function buildName(source)
+ local names = {}
+ for i, type in ipairs(source) do
+ names[i] = type[1]
+ end
+ return table.concat(names, '|')
+end
+
+---@class EmmyType
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.type'
+
+function mt:getType()
+ return self.name
+end
+
+function mt:getName()
+ return self.name
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:eachClass(callback)
+ for _, typeUnit in ipairs(self._childs) do
+ ---@type EmmyTypeUnit
+ local emmyTypeUnit = typeUnit
+ emmyTypeUnit:getClass(callback)
+ end
+end
+
+function mt:setValue(value)
+ self.value = value
+ for _, typeUnit in ipairs(self._childs) do
+ typeUnit:setValue(value)
+ end
+end
+
+function mt:getValue()
+ return self.value
+end
+
+return function (manager, source)
+ local self = setmetatable({
+ name = buildName(source),
+ source = source.id,
+ _manager = manager,
+ _childs = {},
+ }, mt)
+ return self
+end
diff --git a/script/src/emmy/typeUnit.lua b/script/src/emmy/typeUnit.lua
new file mode 100644
index 00000000..73d7ea6b
--- /dev/null
+++ b/script/src/emmy/typeUnit.lua
@@ -0,0 +1,51 @@
+local listMgr = require 'vm.list'
+
+---@class EmmyTypeUnit
+local mt = {}
+mt.__index = mt
+mt.type = 'emmy.typeUnit'
+
+function mt:getType()
+ return self.name
+end
+
+function mt:getName()
+ return self.name
+end
+
+function mt:getSource()
+ return listMgr.get(self.source)
+end
+
+function mt:getClass(callback)
+ self._manager:eachClass(self:getName(), function (class)
+ if class.type == 'emmy.class' then
+ callback(class)
+ end
+ end)
+end
+
+function mt:setValue(value)
+ self.value = value
+end
+
+function mt:getValue()
+ return self.value
+end
+
+function mt:setParent(parent)
+ self.parent = parent
+end
+
+function mt:getParent()
+ return self.parent
+end
+
+return function (manager, source)
+ local self = setmetatable({
+ name = source[1],
+ source = source.id,
+ _manager = manager,
+ }, mt)
+ return self
+end