Skip to content

Commit 2ddae8d

Browse files
committed
添加switch-catch语法
1 parent b46a571 commit 2ddae8d

File tree

5 files changed

+538
-6
lines changed

5 files changed

+538
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mindustry_logic_bang_lang"
3-
version = "0.7.8"
3+
version = "0.8.0"
44
edition = "2021"
55

66
authors = ["A4-Tacks <wdsjxhno1001@163.com>"]

examples/switch_catch.mdtlbl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#**
2+
这是switch的一种特殊语法
3+
可以在计算出需要跳转的case目标后, 开始跳转前, 进行捕获
4+
捕获后可以对其进行处理, 比如可以捕获越界的跳转, 未命中的跳转等
5+
一共有几种捕获模式:
6+
## `<`
7+
对跳转目标小于0时捕获
8+
## `>`
9+
对跳转目标大于最大的case时捕获
10+
## `(...)`
11+
当括号内条件被满足时捕获
12+
## `!`
13+
当存在此捕获时, 填充case将会跳转至此捕获头部
14+
15+
捕获可重复, 捕获的判定顺序由声明顺序决定
16+
17+
语法:
18+
一个case,
19+
接下来是1至多个捕获模式,
20+
然后是可选的捕获值, 它将被const到实际要跳转到的case,
21+
然后是冒号
22+
示例: `case ! e: print e "未命中";`
23+
*#
24+
25+
printflush message1;
26+
switch (op $ x + 3;) {
27+
end;
28+
case ((op $ floor e;) != e) e:
29+
print e " 是一个偏跳转";
30+
end;
31+
case < e:
32+
print e " 向下越界了";
33+
end;
34+
case ! e:
35+
print e " 未命中";
36+
end;
37+
case > e:
38+
print e " 向上越界了";
39+
end;
40+
case 1:
41+
print 1;
42+
case 3:
43+
print 3;
44+
}
45+
# 以上代码首先会对switch后面的值进行take求值, 因为我们编写了捕获
46+
# 然后按声明顺序判断每一个捕获
47+
# 如果未命中捕获存在则永远跳过它, 然后填充case被改为跳进未命中捕获
48+
# 在每一个捕获中如果使用了捕获值例如`case ! x`这里将捕获值捕获到x
49+
# 那么在捕获头部会进行一个`const x = 之前take的句柄;`
50+
# 然后是捕获中的语句
51+
#
52+
# 接下来再看以上代码, 我们可以发现, 当未命中捕获后还会进行一次上越界捕获判定
53+
# 这在一些情况下很有用,
54+
# 例如我们在未命中捕获中可以中途改变需要跳转到的目标再进行跳转
55+
# 这在其它捕获中也适用, 我们可以捕获后不进行终止等操作 而是直接修改跳转目标
56+
# 当然, 这要求你输入的值并不是一个字面量, 否则可能出现`op 1 1 + x`这样的

0 commit comments

Comments
 (0)