diff options
| author | Li Jin <dragon-fly@qq.com> | 2025-05-26 11:07:38 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2025-05-26 11:07:38 +0800 |
| commit | a91135ce512f907ed085d9aac147d8fcad356406 (patch) | |
| tree | e53408fe0a88ef71ea33d14bcb0b6eeb3a344810 /spec | |
| parent | 4ba4c90e711c6204aa40e38347c5a5a076d9370e (diff) | |
| download | yuescript-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.yue | 2 | ||||
| -rw-r--r-- | spec/inputs/macro_export.yue | 4 | ||||
| -rw-r--r-- | spec/inputs/switch.yue | 54 | ||||
| -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 | 24 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc.lua | 4 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc_zh.lua | 4 | ||||
| -rw-r--r-- | spec/outputs/switch.lua | 71 |
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 | ||
| 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 | ||
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 | " |
| 39 | do | 39 | do |
| 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 | ||
| 223 | nil \ No newline at end of file | 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 | 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 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 | ||
| 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 | ||
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 | |||
| 1234 | elseif "Dan" == name or "Daniel" == name then | 1234 | elseif "Dan" == name or "Daniel" == name then |
| 1235 | print("Your name, it's Dan") | 1235 | print("Your name, it's Dan") |
| 1236 | else | 1236 | else |
| 1237 | print("I don't know about your name") | 1237 | print("I don't know about you with name " .. tostring(name)) |
| 1238 | end | 1238 | end |
| 1239 | local b = 1 | 1239 | local b = 1 |
| 1240 | local next_number | 1240 | local next_number |
| @@ -3450,7 +3450,7 @@ if "Robert" == name then | |||
| 3450 | elseif "Dan" == name or "Daniel" == name then | 3450 | elseif "Dan" == name or "Daniel" == name then |
| 3451 | print("Your name, it's Dan") | 3451 | print("Your name, it's Dan") |
| 3452 | else | 3452 | else |
| 3453 | print("I don't know about your name") | 3453 | print("I don't know about you with name " .. tostring(name)) |
| 3454 | end | 3454 | end |
| 3455 | local b = 1 | 3455 | local b = 1 |
| 3456 | local next_number | 3456 | local 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 | |||
| 1228 | elseif "Dan" == name or "Daniel" == name then | 1228 | elseif "Dan" == name or "Daniel" == name then |
| 1229 | print("你的名字是Dan") | 1229 | print("你的名字是Dan") |
| 1230 | else | 1230 | else |
| 1231 | print("我不知道你的名字") | 1231 | print("我不认识你,你的名字是" .. tostring(name)) |
| 1232 | end | 1232 | end |
| 1233 | local b = 1 | 1233 | local b = 1 |
| 1234 | local next_number | 1234 | local next_number |
| @@ -3438,7 +3438,7 @@ if "Robert" == name then | |||
| 3438 | elseif "Dan" == name or "Daniel" == name then | 3438 | elseif "Dan" == name or "Daniel" == name then |
| 3439 | print("你的名字是Dan") | 3439 | print("你的名字是Dan") |
| 3440 | else | 3440 | else |
| 3441 | print("我不知道你的名字") | 3441 | print("我不认识你,你的名字是" .. tostring(name)) |
| 3442 | end | 3442 | end |
| 3443 | local b = 1 | 3443 | local b = 1 |
| 3444 | local next_number | 3444 | local 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 |
| 658 | end | 658 | end |
| 659 | do | ||
| 660 | local v = "hello" | ||
| 661 | if "hello" == v then | ||
| 662 | print("matched hello") | ||
| 663 | else | ||
| 664 | print("not matched") | ||
| 665 | end | ||
| 666 | end | ||
| 667 | do | ||
| 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 | ||
| 676 | end | ||
| 677 | do | ||
| 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 | ||
| 690 | end | ||
| 691 | do | ||
| 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 | ||
| 706 | end | ||
| 707 | do | ||
| 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 | ||
| 729 | end | ||
| 659 | return nil | 730 | return nil |
