Skip to content

Commit

Permalink
将switch-append改为允许多行
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Dec 17, 2023
1 parent fe6815e commit 6da0e4f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mindustry_logic_bang_lang"
version = "0.13.7"
version = "0.13.8"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
6 changes: 3 additions & 3 deletions examples/switch_append.mdtlbl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#* 这是v0.4.1添加的新语法, 是switch语法扩展
* 被写在switch所有case前
* 作用是写在这里的一条语句会被追加到每个case后方
* 你可以写一个块来写多条语句
* 作用是写在这里的每条语句会被追加到每个case后方
*
* 当然, 这是简单复制, 并没有进行标签的处理,
* 所以你往里面写跳转目标会有重复的标记
* 解决也很简单, 你可以写到一个const-DExp, 那条语句去take这个const
* 解决也很简单, 你可以写到一个const-DExp, 然后去take这个const
*#

switch 1 {
Expand Down
19 changes: 13 additions & 6 deletions src/syntax/def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ pub Control: LogicLine = {
"switch" <value:Value>
CtrlStart
<cases:MBlock<(
<LogicLine?> // append line
<LogicLine*> // append line
<( // catch 拦截
"case"
<SwitchCatchFlag+> // 捕获模式, 至少有一个
Expand All @@ -766,7 +766,7 @@ pub Control: LogicLine = {
)>>
<ctrl:CtrlStop>
=> {
let (append, catchs, cases) = cases;
let (mut append, catchs, cases) = cases;
let catchs_is_empty = catchs.is_empty();

let mut next_case_num = 0;
Expand All @@ -787,10 +787,17 @@ pub Control: LogicLine = {
.unwrap();

// 用于填充填充case的行, 如果有追加在末尾的行则将其封装并替换填充
let mut fill_line = append
.as_ref()
.map(|line| Expand(vec![line.clone()]).into())
.unwrap_or(LogicLine::Ignore);
let (mut fill_line, append) = match &append[..] {
[] => (LogicLine::Ignore, None),
[_] => (
Expand(vec![append.last().unwrap().clone()]).into(),
append.pop().unwrap().into(),
),
[..] => (
Expand(append.clone()).into(),
Some(Expand(append).into()),
),
};

// 用于添加到头部的捕获块
let mut catch_lines = Vec::new();
Expand Down
23 changes: 23 additions & 0 deletions src/syntax/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,29 @@ fn switch_test() {
])
).into()
);

assert_eq!(
parse!(parser, r#"
switch 1 {
print end;
print end1;
case 0: print 0;
}
"#).unwrap(),
Select(
"1".into(),
Expand(vec![
Expand(vec![
LogicLine::Other(vec![Value::ReprVar("print".into()), "0".into()]),
Expand(vec![
LogicLine::Other(vec![Value::ReprVar("print".into()), "end".into()]),
LogicLine::Other(vec![Value::ReprVar("print".into()), "end1".into()]),
]).into(),
]).into(),
])
).into()
);

}

#[test]
Expand Down

0 comments on commit 6da0e4f

Please sign in to comment.