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/switch.lua | |
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/switch.lua')
-rw-r--r-- | spec/outputs/switch.lua | 264 |
1 files changed, 257 insertions, 7 deletions
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 | ||