Skip to content

Commit a91135c

Browse files
committed
Added assignment expression for switch syntax.
1 parent 4ba4c90 commit a91135c

17 files changed

Lines changed: 218 additions & 78 deletions

doc/docs/doc/README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,28 +2696,26 @@ reader\parse_line! until reader\eof!
26962696

26972697
## Switch
26982698

2699-
The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator.
2699+
The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator. In switch statement, you can also use assignment expression to store temporary variable value.
27002700

27012701
```moonscript
2702-
name = "Dan"
2703-
switch name
2702+
switch name := "Dan"
27042703
when "Robert"
27052704
print "You are Robert"
27062705
when "Dan", "Daniel"
27072706
print "Your name, it's Dan"
27082707
else
2709-
print "I don't know about your name"
2708+
print "I don't know about you with name #{name}"
27102709
```
27112710
<YueDisplay>
27122711
<pre>
2713-
name = "Dan"
2714-
switch name
2712+
switch name := "Dan"
27152713
when "Robert"
27162714
print "You are Robert"
27172715
when "Dan", "Daniel"
27182716
print "Your name, it's Dan"
27192717
else
2720-
print "I don't know about your name"
2718+
print "I don't know about you with name #{name}"
27212719
</pre>
27222720
</YueDisplay>
27232721

@@ -3528,13 +3526,13 @@ In this usage, with can be seen as a special form of the K combinator.
35283526
The expression in the with statement can also be an assignment, if you want to give a name to the expression.
35293527

35303528
```moonscript
3531-
with str = "Hello"
3529+
with str := "Hello"
35323530
print "original:", str
35333531
print "upper:", \upper!
35343532
```
35353533
<YueDisplay>
35363534
<pre>
3537-
with str = "Hello"
3535+
with str := "Hello"
35383536
print "original:", str
35393537
print "upper:", \upper!
35403538
</pre>

doc/docs/zh/doc/README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,28 +2655,26 @@ reader\parse_line! until reader\eof!
26552655

26562656
## switch 语句
26572657

2658-
switch语句是为了简化检查一系列相同值的if语句而提供的简写语法。要注意用于比较检查的目标值只会计算一次。和if语句一样,switch语句在最后可以接一个else代码块来处理没有匹配的情况。在生成的Lua代码中,进行比较是使用==操作符完成的。
2658+
switch语句是为了简化检查一系列相同值的if语句而提供的简写语法。要注意用于比较检查的目标值只会计算一次。和if语句一样,switch语句在最后可以接一个else代码块来处理没有匹配的情况。在生成的Lua代码中,进行比较是使用==操作符完成的。switch语句中也可以使用赋值表达式来储存临时变量值。
26592659

26602660
```moonscript
2661-
name = "Dan"
2662-
switch name
2661+
switch name := "Dan"
26632662
when "Robert"
26642663
print "你是Robert"
26652664
when "Dan", "Daniel"
26662665
print "你的名字是Dan"
26672666
else
2668-
print "我不知道你的名字"
2667+
print "我不认识你,你的名字是#{name}"
26692668
```
26702669
<YueDisplay>
26712670
<pre>
2672-
name = "Dan"
2673-
switch name
2671+
switch name := "Dan"
26742672
when "Robert"
26752673
print "你是Robert"
26762674
when "Dan", "Daniel"
26772675
print "你的名字是Dan"
26782676
else
2679-
print "我不知道你的名字"
2677+
print "我不认识你,你的名字是#{name}"
26802678
</pre>
26812679
</YueDisplay>
26822680

@@ -3484,13 +3482,13 @@ me = create_person "Leaf", [dad, mother, sister]
34843482
如果你想给表达式另外起一个名称的话,with语句中的表达式也可以是一个赋值语句。
34853483

34863484
```moonscript
3487-
with str = "你好"
3485+
with str := "你好"
34883486
print "原始:", str
34893487
print "大写:", \upper!
34903488
```
34913489
<YueDisplay>
34923490
<pre>
3493-
with str = "你好"
3491+
with str := "你好"
34943492
print "原始:", str
34953493
print "大写:", \upper!
34963494
</pre>

spec/inputs/destructure.yue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ do
9494
--
9595

9696
do
97-
with {a,b} = thing
97+
with {a,b} := thing
9898
print a, b
9999

100100

spec/inputs/macro_export.yue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export macro copy = (src, dst, ...)->
3838
"
3939
do
4040
local _src_, _dst_
41-
with _dst_ = #{dst}
42-
with _src_ = #{src}
41+
with _dst_ := #{dst}
42+
with _src_ := #{src}
4343
#{table.concat for field in *{...} do "
4444
_dst_.#{field} = _src_.#{field}
4545
"}"

spec/inputs/switch.yue

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,56 @@ do
220220
]
221221
print "matched", sixth
222222

223-
nil
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

spec/inputs/unicode/destructure.yue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ do
8484
--
8585

8686
do
87-
with {元素a,元素b} = 东西
87+
with {元素a,元素b} := 东西
8888
打印 元素a, 元素b
8989

9090

spec/inputs/unicode/macro_export.yue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export macro 复制 = (源, 目标, ...)->
3737
"
3838
do
3939
local _源_, _目标_
40-
with _目标_ = #{目标}
41-
with _源_ = #{源}
40+
with _目标_ := #{目标}
41+
with _源_ := #{源}
4242
#{table.concat for 字段 in *{...} do "
4343
_目标_.#{字段} = _源_.#{字段}
4444
"}"

spec/inputs/unicode/with.yue

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ do
4545
with 变量a
4646
打印 .世界
4747

48-
模块 = with _模块 = {}
48+
模块 = with _模块 := {}
4949
.事物 = "你好"
5050

51-
with 变量a, 变量b = 东西, 布
51+
with 变量a, 变量b := 东西, 布
5252
打印 .世界
5353

54-
变量x = with 变量a, 变量b = 1, 2
54+
变量x = with 变量a, 变量b := 1, 2
5555
打印 变量a + 变量b
5656

57-
打印 with 变量a, 变量b = 1, 2
57+
打印 with 变量a, 变量b := 1, 2
5858
打印 变量a + 变量b
5959

60-
p = with 你好!.字段x, 世界!.字段y = 1, 2
60+
p = with 你好!.字段x, 世界!.字段y := 1, 2
6161
打印 变量a + 变量b
6262

6363
--
@@ -68,24 +68,24 @@ do
6868
变量x\大写!
6969

7070
do
71-
with 变量k = "乔"
71+
with 变量k := "乔"
7272
打印 \大写!
7373

7474
do
75-
with 变量a,变量b,变量c = "", "", ""
75+
with 变量a,变量b,变量c := "", "", ""
7676
打印 \大写!
7777

7878
do
7979
变量a = "床铺"
80-
with 变量a,变量b,变量c = "", "", ""
80+
with 变量a,变量b,变量c := "", "", ""
8181
打印 \大写!
8282

8383
do
8484
with 变量j
8585
打印 \大写!
8686

8787
do
88-
with 变量k.变量j = "乔"
88+
with 变量k.变量j := "乔"
8989
打印 \大写!
9090

9191
do
@@ -96,7 +96,7 @@ do
9696

9797
do
9898
with 变量a
99-
with .b = 2
99+
with .b := 2
100100
打印 .c
101101

102102
do
@@ -131,12 +131,12 @@ do
131131

132132
do
133133
global 掩码
134-
with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000
134+
with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000
135135
.触摸启用 = true
136136
.吞噬触摸 = true
137137

138138
do
139-
with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000
139+
with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000
140140
.触摸启用 = true
141141
.吞噬触摸 = true
142142

spec/inputs/with.yue

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ do
4848
with a -- only one value allowed
4949
print .world
5050

51-
mod = with _M = {}
51+
mod = with _M := {}
5252
.Thing = "hi"
5353

5454
-- operate on a only
55-
with a, b = something, pooh
55+
with a, b := something, pooh
5656
print .world
5757

58-
x = with a, b = 1, 2
58+
x = with a, b := 1, 2
5959
print a + b
6060

61-
print with a, b = 1, 2
61+
print with a, b := 1, 2
6262
print a + b
6363

6464
-- 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
6666
print a + b
6767

6868
--
@@ -73,24 +73,24 @@ do
7373
x\upper!
7474

7575
do
76-
with k = "jo"
76+
with k := "jo"
7777
print \upper!
7878

7979
do
80-
with a,b,c = "", "", ""
80+
with a,b,c := "", "", ""
8181
print \upper!
8282

8383
do
8484
a = "bunk"
85-
with a,b,c = "", "", ""
85+
with a,b,c := "", "", ""
8686
print \upper!
8787

8888
do
8989
with j
9090
print \upper!
9191

9292
do
93-
with k.j = "jo"
93+
with k.j := "jo"
9494
print \upper!
9595

9696
do
@@ -103,7 +103,7 @@ do
103103
do
104104
with a
105105
-- nested `with`s with assignments should change the scope correctly
106-
with .b = 2
106+
with .b := 2
107107
print .c
108108

109109
do
@@ -138,12 +138,12 @@ do
138138

139139
do
140140
global mask
141-
with? mask = SolidRect width: w, height: h, color: 0x66000000
141+
with? mask := SolidRect width: w, height: h, color: 0x66000000
142142
.touchEnabled = true
143143
.swallowTouches = true
144144

145145
do
146-
with? mask = SolidRect width: w, height: h, color: 0x66000000
146+
with? mask := SolidRect width: w, height: h, color: 0x66000000
147147
.touchEnabled = true
148148
.swallowTouches = true
149149

spec/outputs/codes_from_doc.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ if "Robert" == name then
12341234
elseif "Dan" == name or "Daniel" == name then
12351235
print("Your name, it's Dan")
12361236
else
1237-
print("I don't know about your name")
1237+
print("I don't know about you with name " .. tostring(name))
12381238
end
12391239
local b = 1
12401240
local next_number
@@ -3450,7 +3450,7 @@ if "Robert" == name then
34503450
elseif "Dan" == name or "Daniel" == name then
34513451
print("Your name, it's Dan")
34523452
else
3453-
print("I don't know about your name")
3453+
print("I don't know about you with name " .. tostring(name))
34543454
end
34553455
local b = 1
34563456
local next_number

0 commit comments

Comments
 (0)