aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-05-26 11:07:38 +0800
committerLi Jin <dragon-fly@qq.com>2025-05-26 11:07:38 +0800
commita91135ce512f907ed085d9aac147d8fcad356406 (patch)
treee53408fe0a88ef71ea33d14bcb0b6eeb3a344810 /spec
parent4ba4c90e711c6204aa40e38347c5a5a076d9370e (diff)
downloadyuescript-a91135ce512f907ed085d9aac147d8fcad356406.tar.gz
yuescript-a91135ce512f907ed085d9aac147d8fcad356406.tar.bz2
yuescript-a91135ce512f907ed085d9aac147d8fcad356406.zip
Added assignment expression for switch syntax.
Diffstat (limited to 'spec')
-rw-r--r--spec/inputs/destructure.yue2
-rw-r--r--spec/inputs/macro_export.yue4
-rw-r--r--spec/inputs/switch.yue54
-rw-r--r--spec/inputs/unicode/destructure.yue2
-rw-r--r--spec/inputs/unicode/macro_export.yue4
-rw-r--r--spec/inputs/unicode/with.yue24
-rw-r--r--spec/inputs/with.yue24
-rw-r--r--spec/outputs/codes_from_doc.lua4
-rw-r--r--spec/outputs/codes_from_doc_zh.lua4
-rw-r--r--spec/outputs/switch.lua71
10 files changed, 158 insertions, 35 deletions
diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue
index 674dfe4..9f01a20 100644
--- a/spec/inputs/destructure.yue
+++ b/spec/inputs/destructure.yue
@@ -94,7 +94,7 @@ do
94-- 94--
95 95
96do 96do
97 with {a,b} = thing 97 with {a,b} := thing
98 print a, b 98 print a, b
99 99
100 100
diff --git a/spec/inputs/macro_export.yue b/spec/inputs/macro_export.yue
index cc7d459..75fd813 100644
--- a/spec/inputs/macro_export.yue
+++ b/spec/inputs/macro_export.yue
@@ -38,8 +38,8 @@ export macro copy = (src, dst, ...)->
38 " 38 "
39do 39do
40 local _src_, _dst_ 40 local _src_, _dst_
41 with _dst_ = #{dst} 41 with _dst_ := #{dst}
42 with _src_ = #{src} 42 with _src_ := #{src}
43#{table.concat for field in *{...} do " 43#{table.concat for field in *{...} do "
44 _dst_.#{field} = _src_.#{field} 44 _dst_.#{field} = _src_.#{field}
45"}" 45"}"
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue
index 7ff3118..5097db3 100644
--- a/spec/inputs/switch.yue
+++ b/spec/inputs/switch.yue
@@ -220,4 +220,56 @@ do
220 ] 220 ]
221 print "matched", sixth 221 print "matched", sixth
222 222
223nil \ No newline at end of file 223do
224 switch v := "hello"
225 when "hello"
226 print "matched hello"
227 else
228 print "not matched"
229 -- output: matched hello
230
231do
232 f = -> "ok"
233 switch val := f!
234 when "ok"
235 print "it's ok"
236 -- output: it's ok
237
238
239do
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
250do
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
264do
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
275nil
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
86do 86do
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 "
38do 38do
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
70do 70do
71 with 变量k = "乔" 71 with 变量k := "乔"
72 打印 \大写! 72 打印 \大写!
73 73
74do 74do
75 with 变量a,变量b,变量c = "", "", "" 75 with 变量a,变量b,变量c := "", "", ""
76 打印 \大写! 76 打印 \大写!
77 77
78do 78do
79 变量a = "床铺" 79 变量a = "床铺"
80 with 变量a,变量b,变量c = "", "", "" 80 with 变量a,变量b,变量c := "", "", ""
81 打印 \大写! 81 打印 \大写!
82 82
83do 83do
@@ -85,7 +85,7 @@ do
85 打印 \大写! 85 打印 \大写!
86 86
87do 87do
88 with 变量k.变量j = "乔" 88 with 变量k.变量j := "乔"
89 打印 \大写! 89 打印 \大写!
90 90
91do 91do
@@ -96,7 +96,7 @@ do
96 96
97do 97do
98 with 变量a 98 with 变量a
99 with .b = 2 99 with .b := 2
100 打印 .c 100 打印 .c
101 101
102do 102do
@@ -131,12 +131,12 @@ do
131 131
132do 132do
133 global 掩码 133 global 掩码
134 with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 134 with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000
135 .触摸启用 = true 135 .触摸启用 = true
136 .吞噬触摸 = true 136 .吞噬触摸 = true
137 137
138do 138do
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 dcd4053..c1cf3e3 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
75do 75do
76 with k = "jo" 76 with k := "jo"
77 print \upper! 77 print \upper!
78 78
79do 79do
80 with a,b,c = "", "", "" 80 with a,b,c := "", "", ""
81 print \upper! 81 print \upper!
82 82
83do 83do
84 a = "bunk" 84 a = "bunk"
85 with a,b,c = "", "", "" 85 with a,b,c := "", "", ""
86 print \upper! 86 print \upper!
87 87
88do 88do
@@ -90,7 +90,7 @@ do
90 print \upper! 90 print \upper!
91 91
92do 92do
93 with k.j = "jo" 93 with k.j := "jo"
94 print \upper! 94 print \upper!
95 95
96do 96do
@@ -103,7 +103,7 @@ do
103do 103do
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
109do 109do
@@ -138,12 +138,12 @@ do
138 138
139do 139do
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
145do 145do
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
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua
index c7a2d50..d857dec 100644
--- a/spec/outputs/codes_from_doc.lua
+++ b/spec/outputs/codes_from_doc.lua
@@ -1234,7 +1234,7 @@ if "Robert" == name then
1234elseif "Dan" == name or "Daniel" == name then 1234elseif "Dan" == name or "Daniel" == name then
1235 print("Your name, it's Dan") 1235 print("Your name, it's Dan")
1236else 1236else
1237 print("I don't know about your name") 1237 print("I don't know about you with name " .. tostring(name))
1238end 1238end
1239local b = 1 1239local b = 1
1240local next_number 1240local next_number
@@ -3450,7 +3450,7 @@ if "Robert" == name then
3450elseif "Dan" == name or "Daniel" == name then 3450elseif "Dan" == name or "Daniel" == name then
3451 print("Your name, it's Dan") 3451 print("Your name, it's Dan")
3452else 3452else
3453 print("I don't know about your name") 3453 print("I don't know about you with name " .. tostring(name))
3454end 3454end
3455local b = 1 3455local b = 1
3456local next_number 3456local next_number
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua
index fcde41f..ecab077 100644
--- a/spec/outputs/codes_from_doc_zh.lua
+++ b/spec/outputs/codes_from_doc_zh.lua
@@ -1228,7 +1228,7 @@ if "Robert" == name then
1228elseif "Dan" == name or "Daniel" == name then 1228elseif "Dan" == name or "Daniel" == name then
1229 print("你的名字是Dan") 1229 print("你的名字是Dan")
1230else 1230else
1231 print("我不知道你的名字") 1231 print("我不认识,你的名字" .. tostring(name))
1232end 1232end
1233local b = 1 1233local b = 1
1234local next_number 1234local next_number
@@ -3438,7 +3438,7 @@ if "Robert" == name then
3438elseif "Dan" == name or "Daniel" == name then 3438elseif "Dan" == name or "Daniel" == name then
3439 print("你的名字是Dan") 3439 print("你的名字是Dan")
3440else 3440else
3441 print("我不知道你的名字") 3441 print("我不认识,你的名字" .. tostring(name))
3442end 3442end
3443local b = 1 3443local b = 1
3444local next_number 3444local next_number
diff --git a/spec/outputs/switch.lua b/spec/outputs/switch.lua
index 0f8bba2..204b816 100644
--- a/spec/outputs/switch.lua
+++ b/spec/outputs/switch.lua
@@ -656,4 +656,75 @@ do
656 end 656 end
657 end 657 end
658end 658end
659do
660 local v = "hello"
661 if "hello" == v then
662 print("matched hello")
663 else
664 print("not matched")
665 end
666end
667do
668 local f
669 f = function()
670 return "ok"
671 end
672 local val = f()
673 if "ok" == val then
674 print("it's ok")
675 end
676end
677do
678 local g
679 g = function()
680 return 42
681 end
682 local result = g()
683 if 1 == result or 2 == result then
684 print("small")
685 elseif 42 == result then
686 print("life universe everything")
687 else
688 print("other " .. tostring(result))
689 end
690end
691do
692 local check
693 check = function()
694 if true then
695 return "yes"
696 else
697 return "no"
698 end
699 end
700 local x = check()
701 if "yes" == x then
702 print("affirmative")
703 else
704 print("negative")
705 end
706end
707do
708 local t
709 t = function()
710 local tb = {
711 a = 1
712 }
713 tb.a = 2
714 return tb
715 end
716 local data = t()
717 local _type_0 = type(data)
718 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
719 local _match_0 = false
720 if _tab_0 then
721 if 2 == data.a then
722 _match_0 = true
723 print("matched")
724 end
725 end
726 if not _match_0 then
727 print("not matched")
728 end
729end
659return nil 730return nil