From a91135ce512f907ed085d9aac147d8fcad356406 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 26 May 2025 11:07:38 +0800 Subject: Added assignment expression for switch syntax. --- spec/inputs/destructure.yue | 2 +- spec/inputs/macro_export.yue | 4 +-- spec/inputs/switch.yue | 54 +++++++++++++++++++++++++++++++++++- spec/inputs/unicode/destructure.yue | 2 +- spec/inputs/unicode/macro_export.yue | 4 +-- spec/inputs/unicode/with.yue | 24 ++++++++-------- spec/inputs/with.yue | 24 ++++++++-------- 7 files changed, 83 insertions(+), 31 deletions(-) (limited to 'spec/inputs') 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 -- do - with {a,b} = thing + with {a,b} := thing print a, b 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, ...)-> " do local _src_, _dst_ - with _dst_ = #{dst} - with _src_ = #{src} + with _dst_ := #{dst} + with _src_ := #{src} #{table.concat for field in *{...} do " _dst_.#{field} = _src_.#{field} "}" 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 ] print "matched", sixth -nil \ No newline at end of file +do + switch v := "hello" + when "hello" + print "matched hello" + else + print "not matched" + -- output: matched hello + +do + f = -> "ok" + switch val := f! + when "ok" + print "it's ok" + -- output: it's ok + + +do + g = -> 42 + switch result := g! + when 1, 2 + print "small" + when 42 + print "life universe everything" + else + print "other #{result}" + -- output: life universe everything + +do + check = -> + if true + "yes" + else + "no" + + switch x := check! + when "yes" + print "affirmative" + else + print "negative" + -- output: affirmative + +do + t = (): tb -> + tb = {a: 1} + tb.a = 2 + + switch data := t! + when {a: 2} + print "matched" + else + print "not matched" + +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 -- do - with {元素a,元素b} = 东西 + with {元素a,元素b} := 东西 打印 元素a, 元素b 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 复制 = (源, 目标, ...)-> " do local _源_, _目标_ - with _目标_ = #{目标} - with _源_ = #{源} + with _目标_ := #{目标} + with _源_ := #{源} #{table.concat for 字段 in *{...} do " _目标_.#{字段} = _源_.#{字段} "}" 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 with 变量a 打印 .世界 - 模块 = with _模块 = {} + 模块 = with _模块 := {} .事物 = "你好" - with 变量a, 变量b = 东西, 布 + with 变量a, 变量b := 东西, 布 打印 .世界 - 变量x = with 变量a, 变量b = 1, 2 + 变量x = with 变量a, 变量b := 1, 2 打印 变量a + 变量b - 打印 with 变量a, 变量b = 1, 2 + 打印 with 变量a, 变量b := 1, 2 打印 变量a + 变量b - p = with 你好!.字段x, 世界!.字段y = 1, 2 + p = with 你好!.字段x, 世界!.字段y := 1, 2 打印 变量a + 变量b -- @@ -68,16 +68,16 @@ do 变量x\大写! do - with 变量k = "乔" + with 变量k := "乔" 打印 \大写! do - with 变量a,变量b,变量c = "", "", "" + with 变量a,变量b,变量c := "", "", "" 打印 \大写! do 变量a = "床铺" - with 变量a,变量b,变量c = "", "", "" + with 变量a,变量b,变量c := "", "", "" 打印 \大写! do @@ -85,7 +85,7 @@ do 打印 \大写! do - with 变量k.变量j = "乔" + with 变量k.变量j := "乔" 打印 \大写! do @@ -96,7 +96,7 @@ do do with 变量a - with .b = 2 + with .b := 2 打印 .c do @@ -131,12 +131,12 @@ do do global 掩码 - with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 + with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000 .触摸启用 = true .吞噬触摸 = true do - with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 + with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000 .触摸启用 = true .吞噬触摸 = true 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 with a -- only one value allowed print .world - mod = with _M = {} + mod = with _M := {} .Thing = "hi" -- operate on a only - with a, b = something, pooh + with a, b := something, pooh print .world - x = with a, b = 1, 2 + x = with a, b := 1, 2 print a + b - print with a, b = 1, 2 + print with a, b := 1, 2 print a + b -- assignment lhs must be evaluated in the order they appear - p = with hello!.x, world!.y = 1, 2 + p = with hello!.x, world!.y := 1, 2 print a + b -- @@ -73,16 +73,16 @@ do x\upper! do - with k = "jo" + with k := "jo" print \upper! do - with a,b,c = "", "", "" + with a,b,c := "", "", "" print \upper! do a = "bunk" - with a,b,c = "", "", "" + with a,b,c := "", "", "" print \upper! do @@ -90,7 +90,7 @@ do print \upper! do - with k.j = "jo" + with k.j := "jo" print \upper! do @@ -103,7 +103,7 @@ do do with a -- nested `with`s with assignments should change the scope correctly - with .b = 2 + with .b := 2 print .c do @@ -138,12 +138,12 @@ do do global mask - with? mask = SolidRect width: w, height: h, color: 0x66000000 + with? mask := SolidRect width: w, height: h, color: 0x66000000 .touchEnabled = true .swallowTouches = true do - with? mask = SolidRect width: w, height: h, color: 0x66000000 + with? mask := SolidRect width: w, height: h, color: 0x66000000 .touchEnabled = true .swallowTouches = true -- cgit v1.2.3-55-g6feb