aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/switch.yue
blob: 2b0669c0ac82cfdef2312ea4b5fda3d3c0ed6489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

switch value
	when "cool"
		print "hello world"


switch value
	when "cool"
		print "hello world"
	else
		print "okay rad"


switch value
	when "cool"
		print "hello world"
	when "yeah"
		_ = [[FFFF]] + [[MMMM]]
	when 2323 + 32434
		print "okay"
	else
		print "okay rad"

out = switch value
	when "cool" then print "hello world"
	else print "okay rad"

out = switch value
	when "cool" then xxxx
	when "umm" then 34340
	else error "this failed big time"

with something
	switch \value!
		when .okay
			_ = "world"
		else
			_ = "yesh"

fix this
call_func switch something
	when 1 then "yes"
	else "no"

--

switch hi
	when hello or world
		_ = greene

--

switch hi
	when "one", "two"
		print "cool"
	when "dad"
		_ = no

switch hi
	when 3+1, hello!, (-> 4)!
		_ = yello
	else
		print "cool"

do
	dict = {
		{}
		{1, 2, 3}
		a: b: c: 1
		x: y: z: 1
	}

	switch dict
		when {
				first
				{one, two, three}
				a: b: :c
				x: y: :z
			}
			print first, one, two, three, c, z

do
	items =
		* x: 100
			y: 200
		* width: 300
			height: 400
		* false

	for item in *items
		switch item
			when :x, :y
				print "Vec2 #{x}, #{y}"
			when :width, :height
				print "Size #{width}, #{height}"
			when false
				print "None"
			when __class: cls
				switch cls
					when ClassA
						print "Object A"
					when ClassB
						print "Object B"
			when <>: mt
				print "A table with metatable"
			else
				print "item not accepted!"

do
	tb = {}

	switch tb
		when {:a = 1, :b = 2}
			print a, b

	switch tb
		when {:a, :b = 2}
			print "partially matched", a, b

	switch tb
		when {:a, :b}
			print a, b
		else
			print "not matched"
do
	tb = x: "abc"
	switch tb
		when :x, :y
			print "x: #{x} with y: #{y}"
		when :x
			print "x: #{x} only"

do
	matched = switch tb
		when 1
			"1"
		when :x
			x
		when false
			"false"
		else
			nil

do
	return switch tb
		when nil
			"invalid"
		when :a, :b
			"#{a + b}"
		when 1, 2, 3, 4, 5
			"number 1 - 5"
		when {:matchAnyTable = "fallback"}
			matchAnyTable
		else
			"should not reach here unless it is not a table"

do
	switch y
		when {x: <>: mt}
			print mt

do
	switch tb
		when [ [item,],]
			print item
		when [a = 1, b = "abc"]
			print a, b

do
	switch tb
		when [1, 2, 3]
			print "1, 2, 3"
		when [1, b, 3]
			print "1, #{b}, 3"
		when [1, 2, b = 3]
			print "1, 2, #{b}"

do
	switch tb
		when success: true, :result
			print "success", result
		when success: false
			print "failed", result
		else
			print "invalid"

do
	switch tb
		when {type: "success", :content}
			print "success", content
		when {type: "error", :content}
			print "failed", content
		else
			print "invalid"

do
	switch tb
		when [
				{a: 1, b: 2}
				{a: 3, b: 4}
				{a: 5, b: 6}
				fourth
			]
			print "matched", fourth

	switch tb
		when [
				{c: 1, d: 2}
				{c: 3, d: 4}
				{c: 5, d: 6}
			]
			print "OK"
		when [
				_
				_
				{a: 1, b: 2}
				{a: 3, b: 4}
				{a: 5, b: 6}
				sixth
			]
			print "matched", sixth

do
	switch v := "hello"
		when "hello"
			print "matched hello"
		else
			print "not matched"
	-- output: matched hello

do
	f = -> "ok"
	switch val := f!
		when "ok"
			print "it's ok"
	-- output: it's ok


do
	g = -> 42
	switch result := g!
		when 1, 2
			print "small"
		when 42
			print "life universe everything"
		else
			print "other #{result}"
	-- output: life universe everything

do
	check = ->
		if true
			"yes"
		else
			"no"

	switch x := check!
		when "yes"
			print "affirmative"
		else
			print "negative"
	-- output: affirmative

do
	t = (): tb ->
		tb = {a: 1}
		tb.a = 2

	switch data := t!
		when {a: 2}
			print "matched"
		else
			print "not matched"

do
	clientData = ["Meta", "CUST_1001", "CHK123"]
	switch clientData
		when [...metadata, customerId, checksum]
			print metadata -- {"Meta"}
			print customerId -- "CUST_1001"
			print checksum -- "CHK123"

do
	handlePath = (segments) ->
		switch segments
			when [..._, resource, action]
				print "Resource:", resource
				print "Action:", action

	handlePath ["admin", "logs", "view"]

nil