aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/switch.yue
blob: 49d47f37ec0c06afef314636515776aed0d77106 (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

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
nil