diff options
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | script/vm/compiler.lua | 30 | ||||
-rw-r--r-- | test/type_inference/init.lua | 4 |
3 files changed, 35 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md index ee0824e2..77d12c51 100644 --- a/changelog.md +++ b/changelog.md @@ -34,6 +34,7 @@ print(x) -- `x` is `string` here end ``` +* `CHG` infer type by `>`/`<`/`>=`/`<=` * `FIX` with clients that support LSP 3.17 (VSCode), workspace diagnostics are triggered every time when opening a file. * `FIX` [#1204](https://github.com/sumneko/lua-language-server/issues/1204) * `FIX` [#1208](https://github.com/sumneko/lua-language-server/issues/1208) diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 0cbe2b7a..acb39432 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -1786,6 +1786,36 @@ local compilerSwitch = util.switch() return end end + if source.op.type == '>' + or source.op.type == '<' + or source.op.type == '>=' + or source.op.type == '<=' then + local a = vm.getNumber(source[1]) + local b = vm.getNumber(source[2]) + if a and b then + local result + if source.op.type == '>' then + result = a > b + elseif source.op.type == '<' then + result = a < b + elseif source.op.type == '>=' then + result = a >= b + elseif source.op.type == '<=' then + result = a <= b + end + vm.setNode(source, { + type = 'boolean', + start = source.start, + finish = source.finish, + parent = source, + [1] = result, + }) + return + else + vm.setNode(source, vm.declareGlobal('type', 'boolean')) + return + end + end end) ---@param source vm.object diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 20d2c6e9..4d135004 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -154,6 +154,10 @@ TEST 'integer' [[ <?x?> = ~ 1 ]] +TEST 'boolean' [[ +<?x?> = 1 < 2 +]] + TEST 'integer' [[ local a = true local b = 1 |