diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-03-07 15:10:49 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-03-07 15:10:49 +0800 |
commit | f082b09302feccb0f9bb8299ffd2690e085df487 (patch) | |
tree | 7fd940f8b1d4154c9939b0892cd0856cd48262ed /server/src/vm | |
parent | 285c7a640cd076455b7a04799fe0fbf9f242d129 (diff) | |
download | lua-language-server-f082b09302feccb0f9bb8299ffd2690e085df487.zip |
支持可选参数
Diffstat (limited to 'server/src/vm')
-rw-r--r-- | server/src/vm/function.lua | 14 | ||||
-rw-r--r-- | server/src/vm/value.lua | 8 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 36 |
3 files changed, 30 insertions, 28 deletions
diff --git a/server/src/vm/function.lua b/server/src/vm/function.lua index d5f67e25..700f1a19 100644 --- a/server/src/vm/function.lua +++ b/server/src/vm/function.lua @@ -138,7 +138,7 @@ function mt:hasRuned() return self._runed > 0 end -function mt:run() +function mt:run(vm) self._runed = self._runed + 1 if not self.source then return @@ -160,7 +160,7 @@ function mt:run() end -- 显性声明的参数 - self:createArgs() + self:createArgs(vm) end function mt:setArgs(values) @@ -172,7 +172,9 @@ function mt:setArgs(values) end end -function mt:createArg(arg, values) +function mt:createArg(vm, arg, values) + vm:instantSource(arg) + arg:set('arg', true) if arg.type == 'name' then local value = table.remove(values, 1) or createValue('nil', arg) local loc = createLocal(arg[1], arg, value) @@ -201,7 +203,7 @@ function mt:hasDots() return self._dots ~= nil end -function mt:createArgs() +function mt:createArgs(vm) if not self.source then return end @@ -215,10 +217,10 @@ function mt:createArgs() end if args.type == 'list' then for _, arg in ipairs(args) do - self:createArg(arg, values) + self:createArg(vm, arg, values) end else - self:createArg(args, values) + self:createArg(vm, args, values) end end diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua index 7513ebd1..d47d6c15 100644 --- a/server/src/vm/value.lua +++ b/server/src/vm/value.lua @@ -13,14 +13,6 @@ local mt = {} mt.__index = mt mt.type = 'value' -function mt:setValue(value) - self._value = value -end - -function mt:getValue() - return self._value -end - function mt:setType(tp, rate) if type(tp) == 'table' then for _, ctp in ipairs(tp) do diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index 0b994011..41015923 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -87,7 +87,7 @@ function mt:buildTable(source) end function mt:runFunction(func) - func:run() + func:run(self) if not func.source then return @@ -391,42 +391,49 @@ function mt:getIndex(source) end function mt:unpackList(list) - local res = createMulti() + local values = createMulti() + local exps = createMulti() if not list then - return res + return values end if list.type == 'list' or list.type == 'call' or list.type == 'return' then for i, exp in ipairs(list) do + self:instantSource(exp) + exps:push(exp) if exp.type == '...' then - res:merge(self:loadDots()) + values:merge(self:loadDots()) break end local value = self:getExp(exp) if value.type == 'multi' then if i == #list then value:eachValue(function (_, v) - res:push(v) + values:push(v) end) else - res:push(self:getFirstInMulti(value)) + values:push(self:getFirstInMulti(value)) end else - res:push(value) + values:push(value) end end elseif list.type == '...' then - res:merge(self:loadDots()) + self:instantSource(list) + exps:push(list) + values:merge(self:loadDots()) else + self:instantSource(list) + exps:push(list) local value = self:getExp(list) if value.type == 'multi' then value:eachValue(function (_, v) - res:push(v) + values:push(v) end) else - res:push(value) + values:push(value) end end - return res + return values, exps end function mt:getFirstInMulti(multi) @@ -456,14 +463,15 @@ function mt:getSimple(simple, max) value = self:getFirstInMulti(value) or createValue('nil') if source.type == 'call' then - local args = self:unpackList(source) + local values, args = self:unpackList(source) local func = value if object then - table.insert(args, 1, object) + table.insert(values, 1, object) + table.insert(args, 1, simple[i-1]) end object = nil source:bindCall(func, args) - value = self:call(func, args, source) or createValue('any') + value = self:call(func, values, source) or createValue('any') elseif source.type == 'index' then source:set('parent', value) local child = source[1] |