diff options
author | Li Jin <dragon-fly@qq.com> | 2022-07-12 17:41:19 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-07-12 17:41:19 +0800 |
commit | 68e167e9f0b90968ea67b7f21fdc50a48d129173 (patch) | |
tree | 64612476c7cc5636d7a9eea68ea1e5b449bb9732 /spec/outputs | |
parent | 1a7c8e3c38fcf0af94ca799a3cd9fa7c4ef1bf08 (diff) | |
download | yuescript-68e167e9f0b90968ea67b7f21fdc50a48d129173.tar.gz yuescript-68e167e9f0b90968ea67b7f21fdc50a48d129173.tar.bz2 yuescript-68e167e9f0b90968ea67b7f21fdc50a48d129173.zip |
add table pattern matching syntax and fix issue #93, remove a confusing default value syntax for destructuring.
Diffstat (limited to 'spec/outputs')
-rw-r--r-- | spec/outputs/destructure.lua | 121 | ||||
-rw-r--r-- | spec/outputs/switch.lua | 264 |
2 files changed, 328 insertions, 57 deletions
diff --git a/spec/outputs/destructure.lua b/spec/outputs/destructure.lua index 9b16181..2fe4ba9 100644 --- a/spec/outputs/destructure.lua +++ b/spec/outputs/destructure.lua | |||
@@ -229,12 +229,12 @@ do | |||
229 | do | 229 | do |
230 | local _obj_0 = person | 230 | local _obj_0 = person |
231 | name, job = _obj_0.name, _obj_0.job | 231 | name, job = _obj_0.name, _obj_0.job |
232 | end | 232 | if name == nil then |
233 | if name == nil then | 233 | name = "nameless" |
234 | name = "nameless" | 234 | end |
235 | end | 235 | if job == nil then |
236 | if job == nil then | 236 | job = "jobless" |
237 | job = "jobless" | 237 | end |
238 | end | 238 | end |
239 | local request | 239 | local request |
240 | request = function(url, options) | 240 | request = function(url, options) |
@@ -261,29 +261,29 @@ do | |||
261 | do | 261 | do |
262 | local _obj_0 = tb | 262 | local _obj_0 = tb |
263 | value1, key3 = _obj_0.key1.key2, _obj_0.key3 | 263 | value1, key3 = _obj_0.key1.key2, _obj_0.key3 |
264 | end | 264 | if value1 == nil then |
265 | if value1 == nil then | 265 | value1 = 123 |
266 | value1 = 123 | 266 | end |
267 | end | 267 | if key3 == nil then |
268 | if key3 == nil then | 268 | key3 = "abc" |
269 | key3 = "abc" | 269 | end |
270 | end | 270 | end |
271 | local mt, call, add | 271 | local mt, call, add |
272 | do | 272 | do |
273 | local _obj_0 = getmetatable(tb) | 273 | local _obj_0 = getmetatable(tb) |
274 | mt, call, add = _obj_0, getmetatable(_obj_0).__call, getmetatable(_obj_0).__add | 274 | mt, call, add = _obj_0, getmetatable(_obj_0).__call, getmetatable(_obj_0).__add |
275 | end | 275 | if mt == nil then |
276 | if mt == nil then | 276 | mt = { |
277 | mt = { | 277 | __index = { |
278 | __index = { | 278 | abc = 123 |
279 | abc = 123 | 279 | } |
280 | } | 280 | } |
281 | } | 281 | end |
282 | end | 282 | if call == nil then |
283 | if call == nil then | 283 | call = (function() |
284 | call = (function() | 284 | return { } |
285 | return { } | 285 | end) |
286 | end) | 286 | end |
287 | end | 287 | end |
288 | local _obj_0 = tb | 288 | local _obj_0 = tb |
289 | local mtx, y, zItem = getmetatable(_obj_0.x), _obj_0.y, _obj_0.z | 289 | local mtx, y, zItem = getmetatable(_obj_0.x), _obj_0.y, _obj_0.z |
@@ -296,27 +296,33 @@ do | |||
296 | return nil | 296 | return nil |
297 | end | 297 | end |
298 | end | 298 | end |
299 | local _obj_1 = getmetatable(tb).func | 299 | do |
300 | if _obj_1 == nil then | 300 | local _tmp_0 |
301 | do | 301 | do |
302 | local _obj_2 = item | 302 | local _obj_1 = getmetatable(tb) |
303 | if _obj_2 ~= nil then | 303 | _tmp_0 = _obj_1.func |
304 | _obj_1 = _obj_2.defVal | 304 | end |
305 | if _tmp_0 == nil then | ||
306 | do | ||
307 | local _obj_1 = item | ||
308 | if _obj_1 ~= nil then | ||
309 | _tmp_0 = _obj_1.defVal | ||
310 | end | ||
305 | end | 311 | end |
306 | end | 312 | end |
307 | end | 313 | a.b(function() |
308 | a.b(function() | ||
309 | return 123 | 314 | return 123 |
310 | end).c = _obj_1 | 315 | end).c = _tmp_0 |
316 | end | ||
311 | end | 317 | end |
312 | do | 318 | do |
313 | local mt, subFunc | 319 | local mt, subFunc |
314 | do | 320 | do |
315 | local _obj_0 = getmetatable(tb.x) | 321 | local _obj_0 = getmetatable(tb.x) |
316 | mt, subFunc = _obj_0, _obj_0.__sub | 322 | mt, subFunc = _obj_0, _obj_0.__sub |
317 | end | 323 | if mt == nil then |
318 | if mt == nil then | 324 | mt = { } |
319 | mt = { } | 325 | end |
320 | end | 326 | end |
321 | end | 327 | end |
322 | do | 328 | do |
@@ -324,27 +330,28 @@ do | |||
324 | do | 330 | do |
325 | local _obj_0 = tb | 331 | local _obj_0 = tb |
326 | mt, subFunc = getmetatable(_obj_0.x), getmetatable(_obj_0.x).__sub | 332 | mt, subFunc = getmetatable(_obj_0.x), getmetatable(_obj_0.x).__sub |
327 | end | 333 | if mt == nil then |
328 | if mt == nil then | 334 | mt = { } |
329 | mt = { } | 335 | end |
330 | end | 336 | end |
331 | end | 337 | end |
332 | do | 338 | do |
333 | local a, b, _obj_0 | 339 | local a, b |
334 | do | 340 | do |
335 | local _obj_1 = tb | 341 | local _obj_0 = tb |
336 | a, b, _obj_0 = _obj_1[1], _obj_1[2], _obj_1.c[1] | 342 | local _tmp_0 |
337 | end | 343 | a, b, _tmp_0 = _obj_0[1], _obj_0[2], _obj_0.c[1] |
338 | if a == nil then | 344 | if a == nil then |
339 | a = 1 | 345 | a = 1 |
340 | end | 346 | end |
341 | if b == nil then | 347 | if b == nil then |
342 | b = 2 | 348 | b = 2 |
343 | end | 349 | end |
344 | if _obj_0 == nil then | 350 | if _tmp_0 == nil then |
345 | _obj_0 = 3 | 351 | _tmp_0 = 3 |
352 | end | ||
353 | d.e = _tmp_0 | ||
346 | end | 354 | end |
347 | d.e = _obj_0 | ||
348 | local _list_0 = tuples | 355 | local _list_0 = tuples |
349 | for _index_0 = 1, #_list_0 do | 356 | for _index_0 = 1, #_list_0 do |
350 | local _des_0 = _list_0[_index_0] | 357 | local _des_0 = _list_0[_index_0] |
@@ -365,3 +372,17 @@ do | |||
365 | a, b = _obj_0[2], _obj_0[4] | 372 | a, b = _obj_0[2], _obj_0[4] |
366 | end | 373 | end |
367 | end | 374 | end |
375 | do | ||
376 | do | ||
377 | local _obj_0 = x.x.x | ||
378 | local _tmp_0, _tmp_1 = _obj_0.x, _obj_0.y | ||
379 | if _tmp_0 == nil then | ||
380 | _tmp_0 = 1 | ||
381 | end | ||
382 | if _tmp_1 == nil then | ||
383 | _tmp_1 = 2 | ||
384 | end | ||
385 | a.b = _tmp_0 | ||
386 | a.c = _tmp_1 | ||
387 | end | ||
388 | end | ||
diff --git a/spec/outputs/switch.lua b/spec/outputs/switch.lua index 81f6d5a..03a0d37 100644 --- a/spec/outputs/switch.lua +++ b/spec/outputs/switch.lua | |||
@@ -77,11 +77,261 @@ do | |||
77 | local _ = no | 77 | local _ = no |
78 | end | 78 | end |
79 | end | 79 | end |
80 | local _exp_0 = hi | 80 | do |
81 | if (3 + 1) == _exp_0 or hello() == _exp_0 or (function() | 81 | local _exp_0 = hi |
82 | return 4 | 82 | if (3 + 1) == _exp_0 or hello() == _exp_0 or (function() |
83 | end)() == _exp_0 then | 83 | return 4 |
84 | return yello | 84 | end)() == _exp_0 then |
85 | else | 85 | local _ = yello |
86 | return print("cool") | 86 | else |
87 | print("cool") | ||
88 | end | ||
89 | end | ||
90 | do | ||
91 | local dict = { | ||
92 | { }, | ||
93 | { | ||
94 | 1, | ||
95 | 2, | ||
96 | 3 | ||
97 | }, | ||
98 | a = { | ||
99 | b = { | ||
100 | c = 1 | ||
101 | } | ||
102 | }, | ||
103 | x = { | ||
104 | y = { | ||
105 | z = 1 | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | do | ||
110 | local _tab_0 = "table" == type(dict) | ||
111 | if _tab_0 then | ||
112 | local first = dict[1] | ||
113 | local one | ||
114 | do | ||
115 | local _obj_0 = dict[2] | ||
116 | if _obj_0 ~= nil then | ||
117 | one = _obj_0[1] | ||
118 | end | ||
119 | end | ||
120 | local two | ||
121 | do | ||
122 | local _obj_0 = dict[2] | ||
123 | if _obj_0 ~= nil then | ||
124 | two = _obj_0[2] | ||
125 | end | ||
126 | end | ||
127 | local three | ||
128 | do | ||
129 | local _obj_0 = dict[2] | ||
130 | if _obj_0 ~= nil then | ||
131 | three = _obj_0[3] | ||
132 | end | ||
133 | end | ||
134 | local c | ||
135 | do | ||
136 | local _obj_0 = dict.a | ||
137 | if _obj_0 ~= nil then | ||
138 | do | ||
139 | local _obj_1 = _obj_0.b | ||
140 | if _obj_1 ~= nil then | ||
141 | c = _obj_1.c | ||
142 | end | ||
143 | end | ||
144 | end | ||
145 | end | ||
146 | local z | ||
147 | do | ||
148 | local _obj_0 = dict.x | ||
149 | if _obj_0 ~= nil then | ||
150 | do | ||
151 | local _obj_1 = _obj_0.y | ||
152 | if _obj_1 ~= nil then | ||
153 | z = _obj_1.z | ||
154 | end | ||
155 | end | ||
156 | end | ||
157 | end | ||
158 | if first ~= nil and one ~= nil and two ~= nil and three ~= nil and c ~= nil and z ~= nil then | ||
159 | print(first, one, two, three, c, z) | ||
160 | end | ||
161 | end | ||
162 | end | ||
163 | end | ||
164 | do | ||
165 | local items = { | ||
166 | { | ||
167 | x = 100, | ||
168 | y = 200 | ||
169 | }, | ||
170 | { | ||
171 | width = 300, | ||
172 | height = 400 | ||
173 | }, | ||
174 | false | ||
175 | } | ||
176 | for _index_0 = 1, #items do | ||
177 | local item = items[_index_0] | ||
178 | do | ||
179 | local _tab_0 = "table" == type(item) | ||
180 | local _match_0 = false | ||
181 | if _tab_0 then | ||
182 | local x = item.x | ||
183 | local y = item.y | ||
184 | if x ~= nil and y ~= nil then | ||
185 | print("Vec2 " .. tostring(x) .. ", " .. tostring(y)) | ||
186 | _match_0 = true | ||
187 | end | ||
188 | end | ||
189 | if not _match_0 then | ||
190 | local _match_1 = false | ||
191 | if _tab_0 then | ||
192 | local width = item.width | ||
193 | local height = item.height | ||
194 | if width ~= nil and height ~= nil then | ||
195 | print("Size " .. tostring(width) .. ", " .. tostring(height)) | ||
196 | _match_1 = true | ||
197 | end | ||
198 | end | ||
199 | if not _match_1 then | ||
200 | if false == item then | ||
201 | print("None") | ||
202 | else | ||
203 | local _match_2 = false | ||
204 | if _tab_0 then | ||
205 | local cls = item.__class | ||
206 | if cls ~= nil then | ||
207 | if ClassA == cls then | ||
208 | print("Object A") | ||
209 | elseif ClassB == cls then | ||
210 | print("Object B") | ||
211 | end | ||
212 | _match_2 = true | ||
213 | end | ||
214 | end | ||
215 | if not _match_2 then | ||
216 | if _tab_0 then | ||
217 | local mt = getmetatable(item) | ||
218 | if mt ~= nil then | ||
219 | print("A table with metatable") | ||
220 | end | ||
221 | else | ||
222 | print("item not accepted!") | ||
223 | end | ||
224 | end | ||
225 | end | ||
226 | end | ||
227 | end | ||
228 | end | ||
229 | end | ||
230 | end | ||
231 | do | ||
232 | local tb = { } | ||
233 | do | ||
234 | local _tab_0 = "table" == type(tb) | ||
235 | if _tab_0 then | ||
236 | local a = tb.a | ||
237 | local b = tb.b | ||
238 | if a == nil then | ||
239 | a = 1 | ||
240 | end | ||
241 | if b == nil then | ||
242 | b = 2 | ||
243 | end | ||
244 | if a ~= nil and b ~= nil then | ||
245 | print(a, b) | ||
246 | end | ||
247 | end | ||
248 | end | ||
249 | end | ||
250 | do | ||
251 | local tb = { | ||
252 | x = "abc" | ||
253 | } | ||
254 | do | ||
255 | local _tab_0 = "table" == type(tb) | ||
256 | local _match_0 = false | ||
257 | if _tab_0 then | ||
258 | local x = tb.x | ||
259 | local y = tb.y | ||
260 | if x ~= nil and y ~= nil then | ||
261 | print("x: " .. tostring(x) .. " with y: " .. tostring(y)) | ||
262 | _match_0 = true | ||
263 | end | ||
264 | end | ||
265 | if not _match_0 then | ||
266 | if _tab_0 then | ||
267 | local x = tb.x | ||
268 | if x ~= nil then | ||
269 | print("x: " .. tostring(x) .. " only") | ||
270 | end | ||
271 | end | ||
272 | end | ||
273 | end | ||
274 | end | ||
275 | do | ||
276 | local matched | ||
277 | do | ||
278 | local _exp_0 = tb | ||
279 | if 1 == _exp_0 then | ||
280 | matched = "1" | ||
281 | else | ||
282 | local _tab_0 = "table" == type(_exp_0) | ||
283 | local _match_0 = false | ||
284 | if _tab_0 then | ||
285 | local x = _exp_0.x | ||
286 | if x ~= nil then | ||
287 | matched = x | ||
288 | _match_0 = true | ||
289 | end | ||
290 | end | ||
291 | if not _match_0 then | ||
292 | if false == _exp_0 then | ||
293 | matched = "false" | ||
294 | else | ||
295 | matched = nil | ||
296 | end | ||
297 | end | ||
298 | end | ||
299 | end | ||
300 | end | ||
301 | do | ||
302 | local _exp_0 = tb | ||
303 | if nil == _exp_0 then | ||
304 | return "invalid" | ||
305 | else | ||
306 | do | ||
307 | local _tab_0 = "table" == type(_exp_0) | ||
308 | local _match_0 = false | ||
309 | if _tab_0 then | ||
310 | local a = _exp_0.a | ||
311 | local b = _exp_0.b | ||
312 | if a ~= nil and b ~= nil then | ||
313 | return tostring(a + b) | ||
314 | _match_0 = true | ||
315 | end | ||
316 | end | ||
317 | if not _match_0 then | ||
318 | if 1 == _exp_0 or 2 == _exp_0 or 3 == _exp_0 or 4 == _exp_0 or 5 == _exp_0 then | ||
319 | return "number 1 - 5" | ||
320 | else | ||
321 | if _tab_0 then | ||
322 | local alwaysMatch = _exp_0.alwaysMatch | ||
323 | if alwaysMatch == nil then | ||
324 | alwaysMatch = "fallback" | ||
325 | end | ||
326 | if alwaysMatch ~= nil then | ||
327 | return alwaysMatch | ||
328 | end | ||
329 | else | ||
330 | return "should not reach here" | ||
331 | end | ||
332 | end | ||
333 | end | ||
334 | end | ||
335 | end | ||
87 | end | 336 | end |
337 | return nil | ||