aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/upvalue_func.yue
blob: 378bf602304118b03fe4ab78ed235364b89cb3e0 (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
-- In module root scope the anonymous functions won't be moved to up-values because they just run once.

-- 1. if expr
func if cond
	998
else
	"abc"

-- 2. nil coalesed expr
func valueA + valueB ?? 123

-- 3. vararg passing
do
	ok, ... = func 1, 2, 3
	print select 3, ...

-- 4. chain with existential operator
if tb?.abc?\call? 123
	print "OK"

-- 5. colon chain with metamethod accessing with string or expr
func(
	tb\<"fn"> 123
	tb\<[1 + 1]> "abc"
)

-- 6. colon chain with Lua keyword or unicode name
func tb\end()\🤣 123

-- 7. in expr with short check
itemA = 1
listA = []
if itemA in listA
	print "itemA in listA"

-- 8. in expr without short check
if itemB? and itemB in listB
	print "itemB in listB"

-- 9. spread table
func [...listA, ...listB]

-- 10. comprehension
func [i for i = 1, 10], [k for k in pairs tb]

-- 11. for expr
func for i = 1, 10
	i + 1

-- 12. for each expr
func for k, v in pairs tb
	[k, v]

-- 13. class declaration expr
func class
	new: => @value = 1

-- 14. with expr
func with tb
	.field = 1
	\func "a"

-- 15. table comprehension expr
func {"#{k}-post-fix", v * 2 for k, v in pairs tb}

-- 16. do expr
func do
	print 123
	"abc"

-- 17. try expr with block codes
do
	success, ... = try
		a = 1
		print a + nil
		1, 2, 3
	print select '#', ... if success

-- 18. while expr
i = 1
func while cond
	i += 1
	i

-- 19. switch expr
func switch value
	when 1
		'a'
	when 2
		'b'

GameEngine\onUpdate (deltaTime) ->
	-- 1. if expr
	func if cond
		998
	else
		"abc"
	
	-- 2. nil coalesed expr
	func valueA + valueB ?? 123
	
	-- 3. vararg passing
	do
		ok, ... = func 1, 2, 3
		print select 3, ...
	
	-- 4. chain with existential operator
	if tb?.abc?\call? 123
		print "OK"
	
	-- 5. colon chain with metamethod accessing with string or expr
	func(
		tb\<"fn"> 123
		tb\<[1 + 1]> "abc"
	)
	
	-- 6. colon chain with Lua keyword or unicode name
	func tb\end()\🤣 123
	
	-- 7. in expr with short check
	itemA = 1
	listA = []
	if itemA in listA
		print "item in list"
	
	-- 8. in expr without short check
	if itemB? and itemB in listB
		print "item in list"
	
	-- 9. spread table
	func [...listA, ...listB]
	
	-- 10. comprehension
	func [i for i = 1, 10], [k for k in pairs tb]
	
	-- 11. for expr
	func for i = 1, 10
		i + 1
	
	-- 12. for each expr
	func for k, v in pairs tb
		[k, v]
	
	-- 13. class declaration expr
	func class
		new: => @value = 1
	
	-- 14. with expr
	func with tb
		.field = 1
		\func "a"
	
	-- 15. table comprehension expr
	func {"#{k}-post-fix", v * 2 for k, v in pairs tb}
	
	-- 16. do expr
	func do
		print 123
		"abc"
	
	-- 17. try expr with block codes
	do
		success, ... = try
			a = 1
			print a + nil
			1, 2, 3
		print select '#', ... if success
	
	-- 18. while expr
	i = 1
	func while cond
		i += 1
		i
	
	-- 19. switch expr
	func switch value
		when 1
			'a'
		when 2
			'b'

GameEngine\onEvent "SomeEvent", ->
	func value + (if cond
		998
	else
		"abc") + (valueB ?? 123) > tb?.abc?\call?(123) + tb\end()\🤣 123 and
	itemA in listA

GameEngine\schedule (deltaTime) ->
	value = 123
	func if value > 200
		UpdateScoreText "Win: #{value}"
		"done"
	else
		UpdateScoreText "Score: #{value}"
		"continue"

GameEngine\schedule (deltaTime) -> -- closure 1
	value = 123
	func if value > 200
		UpdateScoreText "Win: #{value}"
		"done"
	else
		GameEngine\schedule (deltaTime) -> -- closure 2
			UpdateScoreText "Score: #{value}" -- value is captured by closure 2
		"continue"

-- test cases from issue
do
	buff_strength = (char, item) ->
		item.buffer.strength? char.stats.strength?::ref!

	local debug_env_before, debug_env_after

	exe_func = (func, env) ->
		ok, ... = try
			debug_env_before env
			func env
			debug_env_after env
		catch ex
			-- accessing ex and error
			error ex
			-- implicit return
			ex
		if ok
			return ...
		else
			os.exit 1