diff options
Diffstat (limited to 'spec/inputs')
-rw-r--r-- | spec/inputs/backcall.yue | 4 | ||||
-rw-r--r-- | spec/inputs/destructure.yue | 40 | ||||
-rw-r--r-- | spec/inputs/global.yue | 6 | ||||
-rw-r--r-- | spec/inputs/import.yue | 10 | ||||
-rw-r--r-- | spec/inputs/lists.yue | 47 | ||||
-rw-r--r-- | spec/inputs/literals.yue | 9 | ||||
-rw-r--r-- | spec/inputs/loops.yue | 52 | ||||
-rw-r--r-- | spec/inputs/macro.yue | 54 | ||||
-rw-r--r-- | spec/inputs/macro_export.yue | 31 | ||||
-rw-r--r-- | spec/inputs/macro_teal.yue | 13 | ||||
-rw-r--r-- | spec/inputs/macro_todo.yue | 7 | ||||
-rw-r--r-- | spec/inputs/props.yue | 61 | ||||
-rw-r--r-- | spec/inputs/string.yue | 73 | ||||
-rw-r--r-- | spec/inputs/switch.yue | 125 | ||||
-rw-r--r-- | spec/inputs/tables.yue | 18 | ||||
-rw-r--r-- | spec/inputs/try_catch.yue | 122 | ||||
-rw-r--r-- | spec/inputs/unicode/destructure.yue | 2 | ||||
-rw-r--r-- | spec/inputs/unicode/macro_export.yue | 4 | ||||
-rw-r--r-- | spec/inputs/unicode/with.yue | 24 | ||||
-rw-r--r-- | spec/inputs/with.yue | 42 |
20 files changed, 674 insertions, 70 deletions
diff --git a/spec/inputs/backcall.yue b/spec/inputs/backcall.yue index 8aadc71..e6b8c21 100644 --- a/spec/inputs/backcall.yue +++ b/spec/inputs/backcall.yue | |||
@@ -13,9 +13,9 @@ do | |||
13 | x > 2 | 13 | x > 2 |
14 | 14 | ||
15 | do | 15 | do |
16 | (data) <- http?.get "ajaxtest" | 16 | data <- http?.get "ajaxtest" |
17 | body[".result"]\html data | 17 | body[".result"]\html data |
18 | (processed) <- http.post "ajaxprocess", data | 18 | processed <- http.post "ajaxprocess", data |
19 | body[".result"]\append processed | 19 | body[".result"]\append processed |
20 | <- setTimeout 1000 | 20 | <- setTimeout 1000 |
21 | print "done" | 21 | print "done" |
diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue index 674dfe4..b6250d0 100644 --- a/spec/inputs/destructure.yue +++ b/spec/inputs/destructure.yue | |||
@@ -94,7 +94,7 @@ do | |||
94 | -- | 94 | -- |
95 | 95 | ||
96 | do | 96 | do |
97 | with {a,b} = thing | 97 | with {a,b} := thing |
98 | print a, b | 98 | print a, b |
99 | 99 | ||
100 | 100 | ||
@@ -240,5 +240,43 @@ do | |||
240 | switch tb | 240 | switch tb |
241 | when {c: {<"abc">: meta_field = "def"}, <[[any string]]>: {d: abc = 123}, <'str'>: {e: def = {}}} | 241 | when {c: {<"abc">: meta_field = "def"}, <[[any string]]>: {d: abc = 123}, <'str'>: {e: def = {}}} |
242 | print meta_field, abc, def | 242 | print meta_field, abc, def |
243 | |||
244 | do | ||
245 | clients = ["VIP_Alice", "User_Bob", "User_Clara", "VIP_Eva"] | ||
246 | [vipStart, ...regulars, vipEnd] = clients | ||
247 | print vipStart -- "VIP_Alice" | ||
248 | print regulars -- {"User_Bob", "User_Clara"} | ||
249 | print vipEnd -- "VIP_Eva" | ||
250 | |||
251 | do | ||
252 | setupMeeting = (participants) -> | ||
253 | [chair, ..._, secretary] = participants | ||
254 | print chair, secretary | ||
255 | |||
256 | setupMeeting ["Alice", "Bob", "Charlie", "David"] | ||
257 | -- Output: Alice David | ||
258 | |||
259 | do | ||
260 | getTransactions = -> | ||
261 | { | ||
262 | {id: "T1", amount: 100} | ||
263 | {id: "T2", amount: 200} | ||
264 | {id: "T3", amount: 300} | ||
265 | } | ||
266 | |||
267 | :id, :amount = getTransactions![#] | ||
268 | assert id == "T3" | ||
269 | assert amount == 300 | ||
270 | |||
271 | do | ||
272 | [ | ||
273 | _ | ||
274 | ...middle | ||
275 | _ | ||
276 | ] = tb | ||
277 | |||
278 | do | ||
279 | {a, :abc, b, :def, ...sub, d, e} = tb | ||
280 | |||
243 | nil | 281 | nil |
244 | 282 | ||
diff --git a/spec/inputs/global.yue b/spec/inputs/global.yue index ce1cc15..4e3b8aa 100644 --- a/spec/inputs/global.yue +++ b/spec/inputs/global.yue | |||
@@ -82,3 +82,9 @@ do | |||
82 | FooBar = "pascal case" | 82 | FooBar = "pascal case" |
83 | FOOBAR = "all uppercase" | 83 | FOOBAR = "all uppercase" |
84 | 84 | ||
85 | do | ||
86 | global const class A | ||
87 | global const Flag = 1 | ||
88 | global const const, x, y = "const", 1, 2 | ||
89 | global const math, table | ||
90 | |||
diff --git a/spec/inputs/import.yue b/spec/inputs/import.yue index b8ffc24..8982e6c 100644 --- a/spec/inputs/import.yue +++ b/spec/inputs/import.yue | |||
@@ -139,3 +139,13 @@ do | |||
139 | import "m" as {c: d} | 139 | import "m" as {c: d} |
140 | import "m" as {g, {<close>: i}} | 140 | import "m" as {g, {<close>: i}} |
141 | 141 | ||
142 | do | ||
143 | import require | ||
144 | import string as stringlib | ||
145 | import string.format | ||
146 | import io.read as io_read | ||
147 | |||
148 | type = -> | ||
149 | import type as tp | ||
150 | import 月 as yue | ||
151 | |||
diff --git a/spec/inputs/lists.yue b/spec/inputs/lists.yue index 921cae0..c493b68 100644 --- a/spec/inputs/lists.yue +++ b/spec/inputs/lists.yue | |||
@@ -87,4 +87,51 @@ do | |||
87 | [a, b] = hello | 87 | [a, b] = hello |
88 | [name = "nameless", job = "jobless"] = person | 88 | [name = "nameless", job = "jobless"] = person |
89 | 89 | ||
90 | do | ||
91 | transactions = ["T001", "T002", "T003", "T004", "T005"] | ||
92 | middleTransactions = transactions[2, -2] | ||
93 | print middleTransactions -- => {"T002", "T003", "T004"} | ||
94 | |||
95 | do | ||
96 | logs = | ||
97 | - start: 0, end: 100 | ||
98 | - start: 100, end: 200 | ||
99 | - start: 200, end: 123 | ||
100 | print logs[#].end -- => 123 | ||
101 | |||
102 | do | ||
103 | pendingOrders = ["O001", "O002", "O003", "O004"] | ||
104 | print pendingOrders[# - 1] -- => "O003" | ||
105 | |||
106 | do | ||
107 | getOrders = -> | ||
108 | { | ||
109 | { id: "O1001", status: "pending" } | ||
110 | { id: "O1002", status: "processing" } | ||
111 | { id: "O1003", status: "done" } | ||
112 | } | ||
113 | |||
114 | lastStatus = getOrders()[#].status | ||
115 | assert lastStatus == "done" | ||
116 | |||
117 | do | ||
118 | cloneList1 = (list) -> list[,] | ||
119 | cloneList2 = (list) -> [...list,] | ||
120 | cloneTable = (tb) -> {...tb} | ||
121 | |||
122 | do | ||
123 | print( | ||
124 | globalTB[#] | ||
125 | a.b.c[# - 2] | ||
126 | x?\y?!.z?[# - 3] | ||
127 | ) | ||
128 | |||
129 | do | ||
130 | f = -> | ||
131 | print( | ||
132 | globalTB[#]\end 123 | ||
133 | a.b.c[5,-5][# - 2] | ||
134 | x?\y?!.z?[# - 3]?[, -3] | ||
135 | ) | ||
136 | |||
90 | nil | 137 | nil |
diff --git a/spec/inputs/literals.yue b/spec/inputs/literals.yue index 6b666f0..32cf7e7 100644 --- a/spec/inputs/literals.yue +++ b/spec/inputs/literals.yue | |||
@@ -10,10 +10,19 @@ _ = { | |||
10 | 0xfF2323 | 10 | 0xfF2323 |
11 | 0xabcdef | 11 | 0xabcdef |
12 | 0xABCDEF | 12 | 0xABCDEF |
13 | 0XFB_C4_00 | ||
13 | 0x123p-123 | 14 | 0x123p-123 |
14 | 0xABCP+321 | 15 | 0xABCP+321 |
15 | 0x.1p-111 | 16 | 0x.1p-111 |
16 | 0xA_B_CP-3_2_1 | 17 | 0xA_B_CP-3_2_1 |
18 | 0x0.1E | ||
19 | 0xA23p-4 | ||
20 | 0X1.921FB54442D18P+1 | ||
21 | |||
22 | 0b01 | ||
23 | 0b00_00_10_00 | ||
24 | 0B1111 | ||
25 | 0B00_11_00_10_01 | ||
17 | 26 | ||
18 | .2323 | 27 | .2323 |
19 | .2323e-1 | 28 | .2323e-1 |
diff --git a/spec/inputs/loops.yue b/spec/inputs/loops.yue index c5b28b3..5df10ca 100644 --- a/spec/inputs/loops.yue +++ b/spec/inputs/loops.yue | |||
@@ -213,3 +213,55 @@ do | |||
213 | do | 213 | do |
214 | until x := func 'a', b do | 214 | until x := func 'a', b do |
215 | print "false expected" | 215 | print "false expected" |
216 | |||
217 | do | ||
218 | index = for i = 1, #tb | ||
219 | break i if tb[i] | ||
220 | |||
221 | f for i = 1, #tb | ||
222 | break i if tb[i] | ||
223 | |||
224 | f for i = 1, #tb | ||
225 | i if tb[i] | ||
226 | |||
227 | i = 1 | ||
228 | ids = while tb[i] | ||
229 | i += 1 | ||
230 | i - 1 | ||
231 | |||
232 | i = 1 | ||
233 | idx = while tb[i] | ||
234 | i += 1 | ||
235 | break i - 1 | ||
236 | |||
237 | f1 = -> | ||
238 | i = 1 | ||
239 | f while tb[i] | ||
240 | i += 1 | ||
241 | i - 1 | ||
242 | |||
243 | i = 1 | ||
244 | f while tb[i] | ||
245 | i += 1 | ||
246 | break i - 1 | ||
247 | |||
248 | list = for item in *items | ||
249 | switch item | ||
250 | when type: "A", :value | ||
251 | if value > 5 | ||
252 | item | ||
253 | |||
254 | do | ||
255 | repeat print 1 until true | ||
256 | |||
257 | x = repeat | ||
258 | a = func! | ||
259 | break a.x | ||
260 | until a.v | ||
261 | |||
262 | items = repeat | ||
263 | item = getItem! | ||
264 | break unless item | ||
265 | item if item.value > 0 | ||
266 | until false | ||
267 | |||
diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue index 5d5f1a9..191f09f 100644 --- a/spec/inputs/macro.yue +++ b/spec/inputs/macro.yue | |||
@@ -60,6 +60,11 @@ macro NumAndStr = (num, str) -> | |||
60 | 60 | ||
61 | print $NumAndStr 123, 'xyz' | 61 | print $NumAndStr 123, 'xyz' |
62 | 62 | ||
63 | macro NumAndStr2 = (num`Num, str`SingleString) -> | | ||
64 | [#{num}, #{str}] | ||
65 | |||
66 | print $NumAndStr2 456, 'abc' | ||
67 | |||
63 | $asserts item == nil | 68 | $asserts item == nil |
64 | 69 | ||
65 | $myconfig false | 70 | $myconfig false |
@@ -100,13 +105,14 @@ macro filter = (items, action)-> | |||
100 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" | 105 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" |
101 | 106 | ||
102 | macro reduce = (items, def, action)-> | 107 | macro reduce = (items, def, action)-> |
103 | $showMacro "reduce", "if ##{items} == 0 | 108 | $showMacro "reduce", | |
104 | #{def} | 109 | if ##{items} == 0 |
105 | else | 110 | #{def} |
106 | _1 = #{def} | 111 | else |
107 | for _2 in *#{items} | 112 | _1 = #{def} |
108 | _1 = #{action} | 113 | for _2 in *#{items} |
109 | _1" | 114 | _1 = #{action} |
115 | _1 | ||
110 | 116 | ||
111 | macro foreach = (items, action)-> | 117 | macro foreach = (items, action)-> |
112 | $showMacro "foreach", "for _ in *#{items} | 118 | $showMacro "foreach", "for _ in *#{items} |
@@ -154,13 +160,15 @@ macro curry = (...)-> | |||
154 | f = $curry x,y,z,do | 160 | f = $curry x,y,z,do |
155 | print x,y,z | 161 | print x,y,z |
156 | 162 | ||
157 | macro get_inner = (var)-> "do | 163 | macro get_inner = (var)-> | |
158 | a = 1 | 164 | do |
159 | a + 1" | 165 | a = 1 |
166 | a + 1 | ||
160 | 167 | ||
161 | macro get_inner_hygienic = (var)-> "(-> | 168 | macro get_inner_hygienic = (var)-> | |
162 | local a = 1 | 169 | (-> |
163 | a + 1)!" | 170 | local a = 1 |
171 | a + 1)! | ||
164 | 172 | ||
165 | do | 173 | do |
166 | a = 8 | 174 | a = 8 |
@@ -196,6 +204,18 @@ end | |||
196 | 204 | ||
197 | print x | 205 | print x |
198 | 206 | ||
207 | import "yue" | ||
208 | macro lua = (code`YAMLMultiline) -> { | ||
209 | code: yue.loadstring(code)! | ||
210 | type: "lua" | ||
211 | } | ||
212 | |||
213 | $lua | | ||
214 | local function f2(a) | ||
215 | return a + 1 | ||
216 | end | ||
217 | x = x + f2(3) | ||
218 | |||
199 | macro def = (fname, ...)-> | 219 | macro def = (fname, ...)-> |
200 | args = {...} | 220 | args = {...} |
201 | last = table.remove args | 221 | last = table.remove args |
@@ -317,7 +337,13 @@ $chainC( | |||
317 | Destroy! | 337 | Destroy! |
318 | ) | 338 | ) |
319 | 339 | ||
320 | macro tb = -> "{'abc', a:123, <call>:=> 998}" | 340 | macro tb = -> | |
341 | { | ||
342 | 'abc' | ||
343 | a: 123 | ||
344 | <call>: => 998 | ||
345 | } | ||
346 | |||
321 | print $tb[1], $tb.a, ($tb)!, $tb! | 347 | print $tb[1], $tb.a, ($tb)!, $tb! |
322 | 348 | ||
323 | print "current line: #{ $LINE }" | 349 | print "current line: #{ $LINE }" |
diff --git a/spec/inputs/macro_export.yue b/spec/inputs/macro_export.yue index cc7d459..22905b5 100644 --- a/spec/inputs/macro_export.yue +++ b/spec/inputs/macro_export.yue | |||
@@ -8,13 +8,12 @@ export macro config = (debugging = true)-> | |||
8 | "" | 8 | "" |
9 | 9 | ||
10 | export macro showMacro = (name, res)-> | 10 | export macro showMacro = (name, res)-> |
11 | if debugMacro then " | 11 | if debugMacro then | |
12 | do | 12 | do |
13 | txt = #{res} | 13 | txt = #{res} |
14 | print '[macro ' .. #{name} .. ']' | 14 | print '[macro #{name}]' |
15 | print txt | 15 | print txt |
16 | txt | 16 | txt |
17 | " | ||
18 | else | 17 | else |
19 | res | 18 | res |
20 | 19 | ||
@@ -35,14 +34,16 @@ export macro copy = (src, dst, ...)-> | |||
35 | src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_" | 34 | src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_" |
36 | "copy targets can not be _src_ or _dst_" | 35 | "copy targets can not be _src_ or _dst_" |
37 | ) | 36 | ) |
38 | " | 37 | copyFields = table.concat( |
39 | do | 38 | ["_dst_.#{field} = _src_.#{field}" for field in *{...}] |
40 | local _src_, _dst_ | 39 | "\n\t\t\t" |
41 | with _dst_ = #{dst} | 40 | ) |
42 | with _src_ = #{src} | 41 | | |
43 | #{table.concat for field in *{...} do " | 42 | do |
44 | _dst_.#{field} = _src_.#{field} | 43 | local _src_, _dst_ |
45 | "}" | 44 | with _dst_ := #{dst} |
45 | with _src_ := #{src} | ||
46 | #{copyFields} | ||
46 | 47 | ||
47 | export macro enum = (...) -> | 48 | export macro enum = (...) -> |
48 | items = {...} | 49 | items = {...} |
diff --git a/spec/inputs/macro_teal.yue b/spec/inputs/macro_teal.yue index 0cfd862..e51bcd7 100644 --- a/spec/inputs/macro_teal.yue +++ b/spec/inputs/macro_teal.yue | |||
@@ -4,11 +4,16 @@ $ -> | |||
4 | options.target_extension = "tl" | 4 | options.target_extension = "tl" |
5 | package.path ..= ";./spec/lib/?.lua" | 5 | package.path ..= ";./spec/lib/?.lua" |
6 | 6 | ||
7 | macro to_lua = (code)-> | 7 | macro to_lua = (code)-> | |
8 | "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)" | 8 | require('yue').to_lua #{code}, |
9 | reserve_line_number: false | ||
10 | same_module: true | ||
9 | 11 | ||
10 | macro trim = (name)-> | 12 | macro trim = (name)-> | |
11 | "if result := #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" | 13 | if result := #{name}\match '[\'"](.*)[\'"]' |
14 | result | ||
15 | else | ||
16 | #{name} | ||
12 | 17 | ||
13 | export macro local = (decl, value = nil)-> | 18 | export macro local = (decl, value = nil)-> |
14 | import "yue" as {options:{:tl_enabled}} | 19 | import "yue" as {options:{:tl_enabled}} |
diff --git a/spec/inputs/macro_todo.yue b/spec/inputs/macro_todo.yue index 752c9cb..c9c8f77 100644 --- a/spec/inputs/macro_todo.yue +++ b/spec/inputs/macro_todo.yue | |||
@@ -5,9 +5,6 @@ export macro todoInner = (module, line, msg)-> | |||
5 | type: "lua" | 5 | type: "lua" |
6 | } | 6 | } |
7 | 7 | ||
8 | export macro todo = (msg)-> | 8 | export macro todo = (msg)-> | |
9 | if msg | 9 | $todoInner $FILE, $LINE#{msg and ", #{msg}" or ""} |
10 | "$todoInner $FILE, $LINE, #{msg}" | ||
11 | else | ||
12 | "$todoInner $FILE, $LINE" | ||
13 | 10 | ||
diff --git a/spec/inputs/props.yue b/spec/inputs/props.yue new file mode 100644 index 0000000..bbb7aae --- /dev/null +++ b/spec/inputs/props.yue | |||
@@ -0,0 +1,61 @@ | |||
1 | class Props | ||
2 | __index: (name): nil => | ||
3 | cls = @.<> | ||
4 | if item := cls.__getter?[name] -- access properties | ||
5 | return item @ | ||
6 | elseif item := rawget cls, name -- access member functions | ||
7 | return item | ||
8 | else | ||
9 | c = cls | ||
10 | while c := c.<> -- recursive to access base classes | ||
11 | if item := c.__getter?[name] | ||
12 | cls.__getter ??= {} | ||
13 | cls.__getter[name] = item -- cache base properties to class | ||
14 | return item @ | ||
15 | elseif item := rawget c, name | ||
16 | rawset cls, name, item -- cache base member to class | ||
17 | return item | ||
18 | |||
19 | __newindex: (name, value) => | ||
20 | cls = @.<> | ||
21 | if item := cls.__setter?[name] -- access properties | ||
22 | item @, value | ||
23 | else | ||
24 | c = cls | ||
25 | while c := c.<> -- recursive to access base classes | ||
26 | if item := c.__setter?[name] | ||
27 | cls.__setter ??= {} | ||
28 | cls.__setter[name] = item -- cache base property to class | ||
29 | item @, value | ||
30 | return | ||
31 | rawset @, name, value -- assign field to self | ||
32 | |||
33 | assignReadOnly = -> error "assigning a readonly property" | ||
34 | |||
35 | prop: (name, props) => | ||
36 | { | ||
37 | :get | ||
38 | :set = assignReadOnly | ||
39 | } = props | ||
40 | if getter := rawget @__base, "__getter" | ||
41 | getter[name] = get | ||
42 | else | ||
43 | rawset @__base, "__getter", [name]: get | ||
44 | if setter := rawget @__base, "__setter" | ||
45 | setter[name] = set | ||
46 | else | ||
47 | rawset @__base, "__setter", [name]: set | ||
48 | |||
49 | class A extends Props | ||
50 | @prop 'x' | ||
51 | get: => @_x + 1000 | ||
52 | set: (v) => @_x = v | ||
53 | new: => | ||
54 | @_x = 0 | ||
55 | |||
56 | class B extends A | ||
57 | @prop 'abc', get: => "hello" | ||
58 | |||
59 | b = B! | ||
60 | b.x = 999 | ||
61 | print b.x, b.abc | ||
diff --git a/spec/inputs/string.yue b/spec/inputs/string.yue index f91383e..1f0fba8 100644 --- a/spec/inputs/string.yue +++ b/spec/inputs/string.yue | |||
@@ -74,3 +74,76 @@ _ = "hello" | |||
74 | something"hello"\world! | 74 | something"hello"\world! |
75 | something "hello"\world! | 75 | something "hello"\world! |
76 | 76 | ||
77 | do | ||
78 | str = | | ||
79 | key: value | ||
80 | str = | | ||
81 | config: | ||
82 | enabled: true | ||
83 | level: 5 | ||
84 | str = | | ||
85 | header: start | ||
86 | |||
87 | footer: end | ||
88 | str = | | ||
89 | name: #{username} | ||
90 | str = | | ||
91 | count: #{total} items | ||
92 | str = | | ||
93 | user: #{name} | ||
94 | id: #{id} | ||
95 | str = | | ||
96 | path: "C:\\Program Files\\App" | ||
97 | desc: 'single "quote" test' | ||
98 | str = | | ||
99 | key: value | ||
100 | next: 123 | ||
101 | str = | | ||
102 | list: | ||
103 | - "one" | ||
104 | - "two" | ||
105 | str = | | ||
106 | -- comment | ||
107 | content text | ||
108 | -- comment | ||
109 | str = | | ||
110 | #{1 + 2} | ||
111 | #{2 + 3} | ||
112 | #{"a" .. "b"} | ||
113 | obj = | ||
114 | settings: | | ||
115 | mode: #{mode} | ||
116 | flags: | ||
117 | - #{flag1} | ||
118 | - default | ||
119 | fn = -> | | ||
120 | Hello | ||
121 | name: #{userName} | ||
122 | str = | | ||
123 | result: | ||
124 | status: #{if ok then "pass" else "fail"} | ||
125 | code: #{code} | ||
126 | summary = | | ||
127 | date: #{os.date()} | ||
128 | values: | ||
129 | - | ||
130 | a: #{aVal} | ||
131 | b: #{bVal or defaultB} | ||
132 | msg = send | | ||
133 | Hello, #{user}! | ||
134 | Today is #{os.date("%A")}. | ||
135 | desc = do | ||
136 | prefix = "Result" | ||
137 | | | ||
138 | #{prefix}: | ||
139 | value: #{compute!} | ||
140 | (| | ||
141 | 1 | ||
142 | 2 | ||
143 | 3 | ||
144 | |||
145 | |||
146 | export yaml = | | ||
147 | version: #{ver} | ||
148 | ok: true | ||
149 | |||
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue index 49d47f3..2b0669c 100644 --- a/spec/inputs/switch.yue +++ b/spec/inputs/switch.yue | |||
@@ -165,5 +165,128 @@ do | |||
165 | print item | 165 | print item |
166 | when [a = 1, b = "abc"] | 166 | when [a = 1, b = "abc"] |
167 | print a, b | 167 | print a, b |
168 | nil | ||
169 | 168 | ||
169 | do | ||
170 | switch tb | ||
171 | when [1, 2, 3] | ||
172 | print "1, 2, 3" | ||
173 | when [1, b, 3] | ||
174 | print "1, #{b}, 3" | ||
175 | when [1, 2, b = 3] | ||
176 | print "1, 2, #{b}" | ||
177 | |||
178 | do | ||
179 | switch tb | ||
180 | when success: true, :result | ||
181 | print "success", result | ||
182 | when success: false | ||
183 | print "failed", result | ||
184 | else | ||
185 | print "invalid" | ||
186 | |||
187 | do | ||
188 | switch tb | ||
189 | when {type: "success", :content} | ||
190 | print "success", content | ||
191 | when {type: "error", :content} | ||
192 | print "failed", content | ||
193 | else | ||
194 | print "invalid" | ||
195 | |||
196 | do | ||
197 | switch tb | ||
198 | when [ | ||
199 | {a: 1, b: 2} | ||
200 | {a: 3, b: 4} | ||
201 | {a: 5, b: 6} | ||
202 | fourth | ||
203 | ] | ||
204 | print "matched", fourth | ||
205 | |||
206 | switch tb | ||
207 | when [ | ||
208 | {c: 1, d: 2} | ||
209 | {c: 3, d: 4} | ||
210 | {c: 5, d: 6} | ||
211 | ] | ||
212 | print "OK" | ||
213 | when [ | ||
214 | _ | ||
215 | _ | ||
216 | {a: 1, b: 2} | ||
217 | {a: 3, b: 4} | ||
218 | {a: 5, b: 6} | ||
219 | sixth | ||
220 | ] | ||
221 | print "matched", sixth | ||
222 | |||
223 | do | ||
224 | switch v := "hello" | ||
225 | when "hello" | ||
226 | print "matched hello" | ||
227 | else | ||
228 | print "not matched" | ||
229 | -- output: matched hello | ||
230 | |||
231 | do | ||
232 | f = -> "ok" | ||
233 | switch val := f! | ||
234 | when "ok" | ||
235 | print "it's ok" | ||
236 | -- output: it's ok | ||
237 | |||
238 | |||
239 | do | ||
240 | g = -> 42 | ||
241 | switch result := g! | ||
242 | when 1, 2 | ||
243 | print "small" | ||
244 | when 42 | ||
245 | print "life universe everything" | ||
246 | else | ||
247 | print "other #{result}" | ||
248 | -- output: life universe everything | ||
249 | |||
250 | do | ||
251 | check = -> | ||
252 | if true | ||
253 | "yes" | ||
254 | else | ||
255 | "no" | ||
256 | |||
257 | switch x := check! | ||
258 | when "yes" | ||
259 | print "affirmative" | ||
260 | else | ||
261 | print "negative" | ||
262 | -- output: affirmative | ||
263 | |||
264 | do | ||
265 | t = (): tb -> | ||
266 | tb = {a: 1} | ||
267 | tb.a = 2 | ||
268 | |||
269 | switch data := t! | ||
270 | when {a: 2} | ||
271 | print "matched" | ||
272 | else | ||
273 | print "not matched" | ||
274 | |||
275 | do | ||
276 | clientData = ["Meta", "CUST_1001", "CHK123"] | ||
277 | switch clientData | ||
278 | when [...metadata, customerId, checksum] | ||
279 | print metadata -- {"Meta"} | ||
280 | print customerId -- "CUST_1001" | ||
281 | print checksum -- "CHK123" | ||
282 | |||
283 | do | ||
284 | handlePath = (segments) -> | ||
285 | switch segments | ||
286 | when [..._, resource, action] | ||
287 | print "Resource:", resource | ||
288 | print "Action:", action | ||
289 | |||
290 | handlePath ["admin", "logs", "view"] | ||
291 | |||
292 | nil | ||
diff --git a/spec/inputs/tables.yue b/spec/inputs/tables.yue index 0de8a8c..702e04a 100644 --- a/spec/inputs/tables.yue +++ b/spec/inputs/tables.yue | |||
@@ -245,6 +245,24 @@ menus = | |||
245 | click: -> | 245 | click: -> |
246 | } | 246 | } |
247 | 247 | ||
248 | _ = | ||
249 | boolean: | ||
250 | - true | ||
251 | - false | ||
252 | float: | ||
253 | - 3.14 | ||
254 | - -6.8523015e+5 | ||
255 | int: | ||
256 | - 123 | ||
257 | - -0b1010_0111_0100_1010_1110 | ||
258 | null: | ||
259 | nodeName: 'node' | ||
260 | parent: nil | ||
261 | string: | ||
262 | - 'Hello world' | ||
263 | - "newline | ||
264 | newline2" | ||
265 | |||
248 | tb = {...other} | 266 | tb = {...other} |
249 | 267 | ||
250 | tbMix = { | 268 | tbMix = { |
diff --git a/spec/inputs/try_catch.yue b/spec/inputs/try_catch.yue index 4e05bc6..6c29a52 100644 --- a/spec/inputs/try_catch.yue +++ b/spec/inputs/try_catch.yue | |||
@@ -62,6 +62,126 @@ f = -> | |||
62 | do | 62 | do |
63 | <- x | 63 | <- x |
64 | local tb, a, b, c | 64 | local tb, a, b, c |
65 | f = -> try tb.f a, b, c | 65 | f1 = -> try tb.f a, b, c |
66 | |||
67 | do | ||
68 | f1 = -> do | ||
69 | ok, ... = try func! | ||
70 | ... | ||
71 | |||
72 | do | ||
73 | local func | ||
74 | a, b, c = try? func! | ||
75 | |||
76 | do | ||
77 | a, b, c = try? func! | ||
78 | |||
79 | do | ||
80 | a = (try? func!) ?? "default" | ||
81 | |||
82 | do | ||
83 | f try? func! | ||
84 | |||
85 | do | ||
86 | f try? | ||
87 | print 123 | ||
88 | func! | ||
89 | catch e | ||
90 | print e | ||
91 | e | ||
66 | 92 | ||
67 | nil | 93 | nil |
94 | |||
95 | do | ||
96 | try | ||
97 | func 1, 2, 3 | ||
98 | catch err | ||
99 | print err | ||
100 | |||
101 | try func 1, 2, 3 | ||
102 | catch err | ||
103 | print err | ||
104 | |||
105 | try | ||
106 | print "trying" | ||
107 | func 1, 2, 3 | ||
108 | |||
109 | do | ||
110 | success, result = try | ||
111 | func 1, 2, 3 | ||
112 | catch err | ||
113 | print err | ||
114 | |||
115 | success, result = try func 1, 2, 3 | ||
116 | |||
117 | tb = {} | ||
118 | |||
119 | try tb.func | ||
120 | try tb.func! | ||
121 | try tb.func() | ||
122 | try (tb.func!) | ||
123 | try (tb\func(1, 2, 3)) | ||
124 | |||
125 | try tb.func 1 | ||
126 | try tb.func(1) | ||
127 | |||
128 | if (try func 1 | ||
129 | catch err | ||
130 | print err) | ||
131 | print "OK" | ||
132 | |||
133 | if try (func 1) | ||
134 | catch err | ||
135 | print err | ||
136 | print "OK" | ||
137 | |||
138 | do | ||
139 | if success, result := try func "abc", 123 | ||
140 | print result | ||
141 | |||
142 | success, result = try func "abc", 123 | ||
143 | catch err | ||
144 | print err | ||
145 | |||
146 | print result if success, result := try func "abc", 123 | ||
147 | catch err | ||
148 | print err | ||
149 | |||
150 | do | ||
151 | try | ||
152 | func 1, 2, 3 | ||
153 | |||
154 | try func 1, 2, 3 | ||
155 | |||
156 | do | ||
157 | <- x | ||
158 | local tb, a, b, c | ||
159 | f1 = -> try tb.f a, b, c | ||
160 | |||
161 | do | ||
162 | f1 = -> do | ||
163 | ok, ... = try func! | ||
164 | ... | ||
165 | |||
166 | do | ||
167 | local func | ||
168 | a, b, c = try? func! | ||
169 | |||
170 | do | ||
171 | a, b, c = try? func! | ||
172 | |||
173 | do | ||
174 | a = (try? func!) ?? "default" | ||
175 | |||
176 | do | ||
177 | f try? func! | ||
178 | |||
179 | do | ||
180 | f try? | ||
181 | print 123 | ||
182 | func! | ||
183 | catch e | ||
184 | print e | ||
185 | e | ||
186 | |||
187 | nil | ||
diff --git a/spec/inputs/unicode/destructure.yue b/spec/inputs/unicode/destructure.yue index 3c3a369..a5ffd5d 100644 --- a/spec/inputs/unicode/destructure.yue +++ b/spec/inputs/unicode/destructure.yue | |||
@@ -84,7 +84,7 @@ do | |||
84 | -- | 84 | -- |
85 | 85 | ||
86 | do | 86 | do |
87 | with {元素a,元素b} = 东西 | 87 | with {元素a,元素b} := 东西 |
88 | 打印 元素a, 元素b | 88 | 打印 元素a, 元素b |
89 | 89 | ||
90 | 90 | ||
diff --git a/spec/inputs/unicode/macro_export.yue b/spec/inputs/unicode/macro_export.yue index 3c9a942..56571cd 100644 --- a/spec/inputs/unicode/macro_export.yue +++ b/spec/inputs/unicode/macro_export.yue | |||
@@ -37,8 +37,8 @@ export macro 复制 = (源, 目标, ...)-> | |||
37 | " | 37 | " |
38 | do | 38 | do |
39 | local _源_, _目标_ | 39 | local _源_, _目标_ |
40 | with _目标_ = #{目标} | 40 | with _目标_ := #{目标} |
41 | with _源_ = #{源} | 41 | with _源_ := #{源} |
42 | #{table.concat for 字段 in *{...} do " | 42 | #{table.concat for 字段 in *{...} do " |
43 | _目标_.#{字段} = _源_.#{字段} | 43 | _目标_.#{字段} = _源_.#{字段} |
44 | "}" | 44 | "}" |
diff --git a/spec/inputs/unicode/with.yue b/spec/inputs/unicode/with.yue index ecbfdab..3c15add 100644 --- a/spec/inputs/unicode/with.yue +++ b/spec/inputs/unicode/with.yue | |||
@@ -45,19 +45,19 @@ do | |||
45 | with 变量a | 45 | with 变量a |
46 | 打印 .世界 | 46 | 打印 .世界 |
47 | 47 | ||
48 | 模块 = with _模块 = {} | 48 | 模块 = with _模块 := {} |
49 | .事物 = "你好" | 49 | .事物 = "你好" |
50 | 50 | ||
51 | with 变量a, 变量b = 东西, 布 | 51 | with 变量a, 变量b := 东西, 布 |
52 | 打印 .世界 | 52 | 打印 .世界 |
53 | 53 | ||
54 | 变量x = with 变量a, 变量b = 1, 2 | 54 | 变量x = with 变量a, 变量b := 1, 2 |
55 | 打印 变量a + 变量b | 55 | 打印 变量a + 变量b |
56 | 56 | ||
57 | 打印 with 变量a, 变量b = 1, 2 | 57 | 打印 with 变量a, 变量b := 1, 2 |
58 | 打印 变量a + 变量b | 58 | 打印 变量a + 变量b |
59 | 59 | ||
60 | p = with 你好!.字段x, 世界!.字段y = 1, 2 | 60 | p = with 你好!.字段x, 世界!.字段y := 1, 2 |
61 | 打印 变量a + 变量b | 61 | 打印 变量a + 变量b |
62 | 62 | ||
63 | -- | 63 | -- |
@@ -68,16 +68,16 @@ do | |||
68 | 变量x\大写! | 68 | 变量x\大写! |
69 | 69 | ||
70 | do | 70 | do |
71 | with 变量k = "乔" | 71 | with 变量k := "乔" |
72 | 打印 \大写! | 72 | 打印 \大写! |
73 | 73 | ||
74 | do | 74 | do |
75 | with 变量a,变量b,变量c = "", "", "" | 75 | with 变量a,变量b,变量c := "", "", "" |
76 | 打印 \大写! | 76 | 打印 \大写! |
77 | 77 | ||
78 | do | 78 | do |
79 | 变量a = "床铺" | 79 | 变量a = "床铺" |
80 | with 变量a,变量b,变量c = "", "", "" | 80 | with 变量a,变量b,变量c := "", "", "" |
81 | 打印 \大写! | 81 | 打印 \大写! |
82 | 82 | ||
83 | do | 83 | do |
@@ -85,7 +85,7 @@ do | |||
85 | 打印 \大写! | 85 | 打印 \大写! |
86 | 86 | ||
87 | do | 87 | do |
88 | with 变量k.变量j = "乔" | 88 | with 变量k.变量j := "乔" |
89 | 打印 \大写! | 89 | 打印 \大写! |
90 | 90 | ||
91 | do | 91 | do |
@@ -96,7 +96,7 @@ do | |||
96 | 96 | ||
97 | do | 97 | do |
98 | with 变量a | 98 | with 变量a |
99 | with .b = 2 | 99 | with .b := 2 |
100 | 打印 .c | 100 | 打印 .c |
101 | 101 | ||
102 | do | 102 | do |
@@ -131,12 +131,12 @@ do | |||
131 | 131 | ||
132 | do | 132 | do |
133 | global 掩码 | 133 | global 掩码 |
134 | with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 | 134 | with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000 |
135 | .触摸启用 = true | 135 | .触摸启用 = true |
136 | .吞噬触摸 = true | 136 | .吞噬触摸 = true |
137 | 137 | ||
138 | do | 138 | do |
139 | with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 | 139 | with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000 |
140 | .触摸启用 = true | 140 | .触摸启用 = true |
141 | .吞噬触摸 = true | 141 | .吞噬触摸 = true |
142 | 142 | ||
diff --git a/spec/inputs/with.yue b/spec/inputs/with.yue index 19b7be1..2256833 100644 --- a/spec/inputs/with.yue +++ b/spec/inputs/with.yue | |||
@@ -48,21 +48,21 @@ do | |||
48 | with a -- only one value allowed | 48 | with a -- only one value allowed |
49 | print .world | 49 | print .world |
50 | 50 | ||
51 | mod = with _M = {} | 51 | mod = with _M := {} |
52 | .Thing = "hi" | 52 | .Thing = "hi" |
53 | 53 | ||
54 | -- operate on a only | 54 | -- operate on a only |
55 | with a, b = something, pooh | 55 | with a, b := something, pooh |
56 | print .world | 56 | print .world |
57 | 57 | ||
58 | x = with a, b = 1, 2 | 58 | x = with a, b := 1, 2 |
59 | print a + b | 59 | print a + b |
60 | 60 | ||
61 | print with a, b = 1, 2 | 61 | print with a, b := 1, 2 |
62 | print a + b | 62 | print a + b |
63 | 63 | ||
64 | -- assignment lhs must be evaluated in the order they appear | 64 | -- assignment lhs must be evaluated in the order they appear |
65 | p = with hello!.x, world!.y = 1, 2 | 65 | p = with hello!.x, world!.y := 1, 2 |
66 | print a + b | 66 | print a + b |
67 | 67 | ||
68 | -- | 68 | -- |
@@ -73,16 +73,16 @@ do | |||
73 | x\upper! | 73 | x\upper! |
74 | 74 | ||
75 | do | 75 | do |
76 | with k = "jo" | 76 | with k := "jo" |
77 | print \upper! | 77 | print \upper! |
78 | 78 | ||
79 | do | 79 | do |
80 | with a,b,c = "", "", "" | 80 | with a,b,c := "", "", "" |
81 | print \upper! | 81 | print \upper! |
82 | 82 | ||
83 | do | 83 | do |
84 | a = "bunk" | 84 | a = "bunk" |
85 | with a,b,c = "", "", "" | 85 | with a,b,c := "", "", "" |
86 | print \upper! | 86 | print \upper! |
87 | 87 | ||
88 | do | 88 | do |
@@ -90,7 +90,7 @@ do | |||
90 | print \upper! | 90 | print \upper! |
91 | 91 | ||
92 | do | 92 | do |
93 | with k.j = "jo" | 93 | with k.j := "jo" |
94 | print \upper! | 94 | print \upper! |
95 | 95 | ||
96 | do | 96 | do |
@@ -103,7 +103,7 @@ do | |||
103 | do | 103 | do |
104 | with a | 104 | with a |
105 | -- nested `with`s with assignments should change the scope correctly | 105 | -- nested `with`s with assignments should change the scope correctly |
106 | with .b = 2 | 106 | with .b := 2 |
107 | print .c | 107 | print .c |
108 | 108 | ||
109 | do | 109 | do |
@@ -138,12 +138,12 @@ do | |||
138 | 138 | ||
139 | do | 139 | do |
140 | global mask | 140 | global mask |
141 | with? mask = SolidRect width: w, height: h, color: 0x66000000 | 141 | with? mask := SolidRect width: w, height: h, color: 0x66000000 |
142 | .touchEnabled = true | 142 | .touchEnabled = true |
143 | .swallowTouches = true | 143 | .swallowTouches = true |
144 | 144 | ||
145 | do | 145 | do |
146 | with? mask = SolidRect width: w, height: h, color: 0x66000000 | 146 | with? mask := SolidRect width: w, height: h, color: 0x66000000 |
147 | .touchEnabled = true | 147 | .touchEnabled = true |
148 | .swallowTouches = true | 148 | .swallowTouches = true |
149 | 149 | ||
@@ -152,4 +152,22 @@ do | |||
152 | return with {} | 152 | return with {} |
153 | return [123] | 153 | return [123] |
154 | 154 | ||
155 | do | ||
156 | f with item | ||
157 | if .id > 0 | ||
158 | break .content | ||
159 | |||
160 | a = with tb | ||
161 | if .v | ||
162 | break .a | ||
163 | |||
164 | a = while true | ||
165 | break with? tb | ||
166 | break 1 | ||
167 | |||
168 | do | ||
169 | a = for i = 1, 100 | ||
170 | with? x := tb[i] | ||
171 | break x if .id := 1 | ||
172 | |||
155 | nil | 173 | nil |