summaryrefslogtreecommitdiff
path: root/script/vm/compiler.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-07-06 17:34:21 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-07-06 17:34:21 +0800
commit91ab8e7b4c3c17da0a1bcdfb02a38c23cc786910 (patch)
treeb8b9637292bf7656e9b3acefc11e71d98ffd6c9c /script/vm/compiler.lua
parentf407cb07ed559daf7a5a943d8896e849791ae5b7 (diff)
downloadlua-language-server-91ab8e7b4c3c17da0a1bcdfb02a38c23cc786910.zip
cleanup
Diffstat (limited to 'script/vm/compiler.lua')
-rw-r--r--script/vm/compiler.lua247
1 files changed, 2 insertions, 245 deletions
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 12cf102b..beb6e4e5 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -1022,193 +1022,6 @@ local function compileLocal(source)
vm.getNode(source):setData('hasDefined', hasMarkDoc or hasMarkParam or hasMarkValue)
end
-local binarySwich = util.switch()
- : case 'and'
- : call(function (source)
- local node1 = vm.compileNode(source[1])
- local node2 = vm.compileNode(source[2])
- local r1 = vm.testCondition(source[1])
- if r1 == true then
- vm.setNode(source, node2)
- elseif r1 == false then
- vm.setNode(source, node1)
- else
- local node = node1:copy():setFalsy():merge(node2)
- vm.setNode(source, node)
- end
- end)
- : case 'or'
- : call(function (source)
- local node1 = vm.compileNode(source[1])
- local node2 = vm.compileNode(source[2])
- local r1 = vm.testCondition(source[1])
- if r1 == true then
- vm.setNode(source, node1)
- elseif r1 == false then
- vm.setNode(source, node2)
- else
- local node = node1:copy():setTruthy():merge(node2)
- vm.setNode(source, node)
- end
- end)
- : case '=='
- : case '~='
- : call(function (source)
- local result = vm.equal(source[1], source[2])
- if result == nil then
- vm.setNode(source, vm.declareGlobal('type', 'boolean'))
- else
- if source.op.type == '~=' then
- result = not result
- end
- vm.setNode(source, {
- type = 'boolean',
- start = source.start,
- finish = source.finish,
- parent = source,
- [1] = result,
- })
- end
- end)
- : case '<<'
- : case '>>'
- : case '&'
- : case '|'
- : case '~'
- : call(function (source)
- local a = vm.getInteger(source[1])
- local b = vm.getInteger(source[2])
- if a and b then
- local op = source.op.type
- local result = op.type == '<<' and a << b
- or op.type == '>>' and a >> b
- or op.type == '&' and a & b
- or op.type == '|' and a | b
- or op.type == '~' and a ~ b
- vm.setNode(source, {
- type = 'integer',
- start = source.start,
- finish = source.finish,
- parent = source,
- [1] = result,
- })
- else
- vm.setNode(source, vm.declareGlobal('type', 'integer'))
- end
- end)
- : case '+'
- : case '-'
- : case '*'
- : case '/'
- : case '%'
- : case '//'
- : case '^'
- : call(function (source)
- local a = vm.getNumber(source[1])
- local b = vm.getNumber(source[2])
- local op = source.op.type
- local zero = b == 0
- and ( op == '%'
- or op == '/'
- or op == '//'
- )
- if a and b and not zero then
- local result = op == '+' and a + b
- or op == '-' and a - b
- or op == '*' and a * b
- or op == '/' and a / b
- or op == '%' and a % b
- or op == '//' and a // b
- or op == '^' and a ^ b
- vm.setNode(source, {
- type = math.type(result) == 'integer' and 'integer' or 'number',
- start = source.start,
- finish = source.finish,
- parent = source,
- [1] = result,
- })
- else
- if op == '+'
- or op == '-'
- or op == '*'
- or op == '//'
- or op == '%' then
- local uri = guide.getUri(source)
- local infer1 = vm.getInfer(source[1])
- local infer2 = vm.getInfer(source[2])
- if infer1:hasType(uri, 'integer')
- or infer2:hasType(uri, 'integer') then
- if not infer1:hasType(uri, 'number')
- and not infer2:hasType(uri, 'number') then
- vm.setNode(source, vm.declareGlobal('type', 'integer'))
- return
- end
- end
- end
- vm.setNode(source, vm.declareGlobal('type', 'number'))
- end
- end)
- : case '..'
- : call(function (source)
- local a = vm.getString(source[1])
- or vm.getNumber(source[1])
- local b = vm.getString(source[2])
- or vm.getNumber(source[2])
- if a and b then
- if type(a) == 'number' or type(b) == 'number' then
- local uri = guide.getUri(source)
- local version = config.get(uri, 'Lua.runtime.version')
- if math.tointeger(a) and math.type(a) == 'float' then
- if version == 'Lua 5.3' or version == 'Lua 5.4' then
- a = ('%.1f'):format(a)
- else
- a = ('%.0f'):format(a)
- end
- end
- if math.tointeger(b) and math.type(b) == 'float' then
- if version == 'Lua 5.3' or version == 'Lua 5.4' then
- b = ('%.1f'):format(b)
- else
- b = ('%.0f'):format(b)
- end
- end
- end
- vm.setNode(source, {
- type = 'string',
- start = source.start,
- finish = source.finish,
- parent = source,
- [1] = a .. b,
- })
- else
- vm.setNode(source, vm.declareGlobal('type', 'string'))
- end
- end)
- : case '>'
- : case '<'
- : case '>='
- : case '<='
- : call(function (source)
- local a = vm.getNumber(source[1])
- local b = vm.getNumber(source[2])
- if a and b then
- local op = source.op.type
- local result = op.type == '>' and a > b
- or op.type == '<' and a < b
- or op.type == '>=' and a >= b
- or op.type == '<=' and a <= b
- vm.setNode(source, {
- type = 'boolean',
- start = source.start,
- finish = source.finish,
- parent = source,
- [1] =result,
- })
- else
- vm.setNode(source, vm.declareGlobal('type', 'boolean'))
- end
- end)
-
local compilerSwitch = util.switch()
: case 'nil'
: case 'boolean'
@@ -1818,63 +1631,7 @@ local compilerSwitch = util.switch()
if not source[1] then
return
end
- if source.op.type == 'not' then
- local result = vm.testCondition(source[1])
- if result == nil then
- vm.setNode(source, vm.declareGlobal('type', 'boolean'))
- return
- else
- vm.setNode(source, {
- type = 'boolean',
- start = source.start,
- finish = source.finish,
- parent = source,
- [1] = not result,
- })
- return
- end
- end
- if source.op.type == '#' then
- vm.setNode(source, vm.declareGlobal('type', 'integer'))
- return
- end
- if source.op.type == '-' then
- local v = vm.getNumber(source[1])
- if v == nil then
- local infer = vm.getInfer(source[1])
- if infer:hasType(guide.getUri(source), 'integer') then
- vm.setNode(source, vm.declareGlobal('type', 'integer'))
- else
- vm.setNode(source, vm.declareGlobal('type', 'number'))
- end
- return
- else
- vm.setNode(source, {
- type = 'number',
- start = source.start,
- finish = source.finish,
- parent = source,
- [1] = -v,
- })
- return
- end
- end
- if source.op.type == '~' then
- local v = vm.getInteger(source[1])
- if v == nil then
- vm.setNode(source, vm.declareGlobal('type', 'integer'))
- return
- else
- vm.setNode(source, {
- type = 'integer',
- start = source.start,
- finish = source.finish,
- parent = source,
- [1] = ~v,
- })
- return
- end
- end
+ vm.unarySwich(source.op.type, source)
end)
: case 'binary'
: call(function (source)
@@ -1884,7 +1641,7 @@ local compilerSwitch = util.switch()
if not source[1] or not source[2] then
return
end
- binarySwich(source.op.type, source)
+ vm.binarySwitch(source.op.type, source)
end)
---@param source parser.object