Skip to content

Commit 317c856

Browse files
committed
cleanup
1 parent 4cbbe55 commit 317c856

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

script/vm/compiler.lua

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,13 @@ local compilerSwitch = util.switch()
982982
else
983983
vm.setNode(src, vm.compileNode(src.value), true)
984984
end
985+
elseif src.value
986+
and src.value.type == 'binary'
987+
and src.value.op and src.value.op.type == 'or'
988+
and src.value[1] and src.value[1].type == 'getlocal' and src.value[1].node == source
989+
and src.value[2] and guide.isLiteral(src.value[2]) then
990+
-- x = x or 1
991+
vm.setNode(src, vm.compileNode(src.value))
985992
else
986993
vm.setNode(src, node, true)
987994
end
@@ -1420,28 +1427,32 @@ local compilerSwitch = util.switch()
14201427
return
14211428
end
14221429
if source.op.type == 'and' then
1430+
local node1 = vm.compileNode(source[1])
1431+
local node2 = vm.compileNode(source[2])
14231432
local r1 = vm.test(source[1])
14241433
if r1 == true then
1425-
vm.setNode(source, vm.compileNode(source[2]))
1426-
return
1427-
end
1428-
if r1 == false then
1429-
vm.setNode(source, vm.compileNode(source[1]))
1430-
return
1434+
vm.setNode(source, node2)
1435+
elseif r1 == false then
1436+
vm.setNode(source, node1)
1437+
else
1438+
vm.getNode(source):merge(node1)
1439+
vm.getNode(source):setTruly()
1440+
vm.getNode(source):merge(node2)
14311441
end
1432-
return
14331442
end
14341443
if source.op.type == 'or' then
1444+
local node1 = vm.compileNode(source[1])
1445+
local node2 = vm.compileNode(source[2])
14351446
local r1 = vm.test(source[1])
14361447
if r1 == true then
1437-
vm.setNode(source, vm.compileNode(source[1]))
1438-
return
1439-
end
1440-
if r1 == false then
1441-
vm.setNode(source, vm.compileNode(source[2]))
1442-
return
1448+
vm.setNode(source, node1)
1449+
elseif r1 == false then
1450+
vm.setNode(source, node2)
1451+
else
1452+
vm.getNode(source):merge(node1)
1453+
vm.getNode(source):setTruly()
1454+
vm.getNode(source):merge(node2)
14431455
end
1444-
return
14451456
end
14461457
if source.op.type == '==' then
14471458
local result = vm.equal(source[1], source[2])

script/vm/node.lua

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,23 @@ function mt:setTruly()
119119
self.optional = nil
120120
end
121121
local hasBoolean
122-
local index = 0
123-
while true do
124-
index = index + 1
122+
for index = #self, 1, -1 do
125123
local c = self[index]
126-
if not c then
127-
break
128-
end
129124
if c.type == 'nil'
130125
or (c.type == 'boolean' and c[1] == false)
131126
or (c.type == 'doc.type.boolean' and c[1] == false) then
132127
table.remove(self, index)
133128
self[c] = nil
134-
index = index - 1
129+
goto CONTINUE
135130
end
136131
if (c.type == 'global' and c.cate == 'type' and c.name == 'boolean')
137132
or (c.type == 'boolean' or c.type == 'doc.type.boolean') then
138133
hasBoolean = true
139134
table.remove(self, index)
140135
self[c] = nil
141-
index = index - 1
136+
goto CONTINUE
142137
end
138+
::CONTINUE::
143139
end
144140
if hasBoolean then
145141
self[#self+1] = {
@@ -155,13 +151,8 @@ function mt:setFalsy()
155151
self.optional = nil
156152
end
157153
local hasBoolean
158-
local index = 0
159-
while true do
160-
index = index + 1
154+
for index = #self, 1, -1 do
161155
local c = self[index]
162-
if not c then
163-
break
164-
end
165156
if c.type == 'nil'
166157
or (c.type == 'boolean' and c[1] == true)
167158
or (c.type == 'doc.type.boolean' and c[1] == true) then
@@ -174,7 +165,6 @@ function mt:setFalsy()
174165
end
175166
table.remove(self, index)
176167
self[c] = nil
177-
index = index - 1
178168
::CONTINUE::
179169
end
180170
if hasBoolean then
@@ -190,13 +180,8 @@ function mt:remove(name)
190180
if name == 'nil' and self.optional == true then
191181
self.optional = nil
192182
end
193-
local index = 0
194-
while true do
195-
index = index + 1
183+
for index = #self, 1, -1 do
196184
local c = self[index]
197-
if not c then
198-
break
199-
end
200185
if (c.type == 'global' and c.cate == 'type' and c.name == name)
201186
or (c.type == name)
202187
or (c.type == 'doc.type.integer' and (name == 'number' or name == 'integer'))
@@ -206,7 +191,6 @@ function mt:remove(name)
206191
or (c.type == 'doc.type.function' and name == 'function') then
207192
table.remove(self, index)
208193
self[c] = nil
209-
index = index - 1
210194
end
211195
end
212196
end

script/vm/value.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function vm.test(source)
1717
hasTrue = true
1818
end
1919
if n[1] == false then
20-
hasTrue = false
20+
hasFalse = true
2121
end
2222
end
2323
if n.type == 'nil' then

test/type_inference/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,13 @@ end
18331833
print(<?x?>)
18341834
]]
18351835

1836+
TEST 'integer' [[
1837+
---@type integer?
1838+
local x
1839+
1840+
<?x?> = x or 1
1841+
]]
1842+
18361843
TEST 'integer' [=[
18371844
local x
18381845

0 commit comments

Comments
 (0)