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
|
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
nil
|