diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-06-15 19:00:20 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-06-15 19:00:20 +0800 |
commit | a86a5884ddbdb32fad8dce1b50c6d0c76c862256 (patch) | |
tree | 33e5592de19171eb61f76e1ef10967a533a149bb | |
parent | a93effc7b08c95e1c9f3f852648e09a28e685d05 (diff) | |
download | lua-language-server-a86a5884ddbdb32fad8dce1b50c6d0c76c862256.zip |
resolve #1212
-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 |