aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2024-03-19 16:21:35 +0800
committerLi Jin <dragon-fly@qq.com>2024-03-19 16:21:35 +0800
commit2733fe565c405f7b382fb7c02c69f78fb65d2f20 (patch)
tree41a91a52c7ee9e9360831693d107e7da897c7a3b /spec/inputs
parent62ddb888d67d047324aa6c411d47aaeac9b658fd (diff)
downloadyuescript-2733fe565c405f7b382fb7c02c69f78fb65d2f20.tar.gz
yuescript-2733fe565c405f7b382fb7c02c69f78fb65d2f20.tar.bz2
yuescript-2733fe565c405f7b382fb7c02c69f78fb65d2f20.zip
fix nil coalescing anonymous function moving issue. add test cases.
Diffstat (limited to 'spec/inputs')
-rw-r--r--spec/inputs/upvalue_func.yue207
1 files changed, 207 insertions, 0 deletions
diff --git a/spec/inputs/upvalue_func.yue b/spec/inputs/upvalue_func.yue
new file mode 100644
index 0000000..a4155da
--- /dev/null
+++ b/spec/inputs/upvalue_func.yue
@@ -0,0 +1,207 @@
1-- In module root scope the anonymous functions won't be moved to up-values because they just run once.
2
3-- 1. if expr
4func if cond
5 998
6else
7 "abc"
8
9-- 2. nil coalesed expr
10func valueA + valueB ?? 123
11
12-- 3. vararg passing
13do
14 ok, ... = func 1, 2, 3
15 print select 3, ...
16
17-- 4. chain with existential operator
18if tb?.abc?\call? 123
19 print "OK"
20
21-- 5. colon chain with metamethod accessing with string or expr
22func(
23 tb\<"fn"> 123
24 tb\<[1 + 1]> "abc"
25)
26
27-- 6. colon chain with Lua keyword or unicode name
28func tb\end()\🤣 123
29
30-- 7. in expr with short check
31itemA = 1
32listA = []
33if itemA in listA
34 print "itemA in listA"
35
36-- 8. in expr without short check
37if itemB? and itemB in listB
38 print "itemB in listB"
39
40-- 9. spread table
41func [...listA, ...listB]
42
43-- 10. comprehension
44func [i for i = 1, 10], [k for k in pairs tb]
45
46-- 11. for expr
47func for i = 1, 10
48 i + 1
49
50-- 12. for each expr
51func for k, v in pairs tb
52 [k, v]
53
54-- 13. class declaration expr
55func class
56 new: => @value = 1
57
58-- 14. with expr
59func with tb
60 .field = 1
61 \func "a"
62
63-- 15. table comprehension expr
64func {"#{k}-post-fix", v * 2 for k, v in pairs tb}
65
66-- 16. do expr
67func do
68 print 123
69 "abc"
70
71-- 17. try expr with block codes
72do
73 success, ... = try
74 a = 1
75 print a + nil
76 1, 2, 3
77 print select '#', ... if success
78
79-- 18. while expr
80i = 1
81func while cond
82 i += 1
83 i
84
85-- 19. switch expr
86func switch value
87 when 1
88 'a'
89 when 2
90 'b'
91
92GameEngine\onUpdate (deltaTime) ->
93 -- 1. if expr
94 func if cond
95 998
96 else
97 "abc"
98
99 -- 2. nil coalesed expr
100 func valueA + valueB ?? 123
101
102 -- 3. vararg passing
103 do
104 ok, ... = func 1, 2, 3
105 print select 3, ...
106
107 -- 4. chain with existential operator
108 if tb?.abc?\call? 123
109 print "OK"
110
111 -- 5. colon chain with metamethod accessing with string or expr
112 func(
113 tb\<"fn"> 123
114 tb\<[1 + 1]> "abc"
115 )
116
117 -- 6. colon chain with Lua keyword or unicode name
118 func tb\end()\🤣 123
119
120 -- 7. in expr with short check
121 itemA = 1
122 listA = []
123 if itemA in listA
124 print "item in list"
125
126 -- 8. in expr without short check
127 if itemB? and itemB in listB
128 print "item in list"
129
130 -- 9. spread table
131 func [...listA, ...listB]
132
133 -- 10. comprehension
134 func [i for i = 1, 10], [k for k in pairs tb]
135
136 -- 11. for expr
137 func for i = 1, 10
138 i + 1
139
140 -- 12. for each expr
141 func for k, v in pairs tb
142 [k, v]
143
144 -- 13. class declaration expr
145 func class
146 new: => @value = 1
147
148 -- 14. with expr
149 func with tb
150 .field = 1
151 \func "a"
152
153 -- 15. table comprehension expr
154 func {"#{k}-post-fix", v * 2 for k, v in pairs tb}
155
156 -- 16. do expr
157 func do
158 print 123
159 "abc"
160
161 -- 17. try expr with block codes
162 do
163 success, ... = try
164 a = 1
165 print a + nil
166 1, 2, 3
167 print select '#', ... if success
168
169 -- 18. while expr
170 i = 1
171 func while cond
172 i += 1
173 i
174
175 -- 19. switch expr
176 func switch value
177 when 1
178 'a'
179 when 2
180 'b'
181
182GameEngine\onEvent "SomeEvent", ->
183 func value + (if cond
184 998
185 else
186 "abc") + (valueB ?? 123) > tb?.abc?\call?(123) + tb\end()\🤣 123 and
187 itemA in listA
188
189GameEngine\schedule (deltaTime) ->
190 value = 123
191 func if value > 200
192 UpdateScoreText "Win: #{value}"
193 "done"
194 else
195 UpdateScoreText "Score: #{value}"
196 "continue"
197
198GameEngine\schedule (deltaTime) -> -- closure 1
199 value = 123
200 func if value > 200
201 UpdateScoreText "Win: #{value}"
202 "done"
203 else
204 GameEngine\schedule (deltaTime) -> -- closure 2
205 UpdateScoreText "Score: #{value}" -- value is captured by closure 2
206 "continue"
207