Skip to content

Commit

Permalink
为switch添加了append语法, 这可以使一些场景下使用switch更轻松, 例如常量表
Browse files Browse the repository at this point in the history
移除了被枚举语句end, 因为它没啥用且容易占用一个常用跳转标记
  • Loading branch information
A4-Tacks committed Jul 27, 2023
1 parent 99c9658 commit 25da64c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 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.4.0"
version = "0.4.1"
edition = "2021"

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

switch 1 {
goto :end _;
case 0: print 0;
case 1: print 1;
case 2: print 2;
} :end
end;

#* 以上代码会被编译为
op mul __0 1 2
op add @counter @counter __0
print 0
jump 8 always 0 0
print 1
jump 8 always 0 0
print 2
jump 8 always 0 0
end
*#

# 从以上代码可以看到, goto到end呗加入到了每一行的后面
# 这是一个实用的功能
32 changes: 25 additions & 7 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@ mod tests {
#[test]
fn switch_test() {
let parser = LogicLineParser::new();

let ast = parse!(parser, r#"
switch 2 {
case 1:
Expand Down Expand Up @@ -1296,7 +1297,30 @@ mod tests {
"print 5",
"noop",
]);
//println!("{}", lines.join("\n"));

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

#[test]
Expand Down Expand Up @@ -1344,16 +1368,10 @@ mod tests {
}
print (op $ y + 3;);
"#;
//dbg!(&src);
let ast = parse!(parser, src).unwrap();
//dbg!(&ast);
let meta = CompileMeta::new();
//dbg!(&meta);
let mut tag_codes = meta.compile(ast);
//dbg!(&tag_codes);
let logic_lines = tag_codes.compile().unwrap();
//dbg!(&logic_lines);
//println!("{}", logic_lines.join("\n"));
assert_eq!(logic_lines, [
r#"op add x 1 2"#,
r#"op add __0 x 3"#,
Expand Down
13 changes: 10 additions & 3 deletions src/syntax_def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ pub LogicLine: LogicLine = {
Label => LogicLine::new_label(<>, meta),
"op" <Op> LEnd => <>.into(),
"noop" LEnd => LogicLine::NoOp,
"end" LEnd => LogicLine::End,
"set" <var:Value> <value:Value> LEnd => LogicLine::Other(vec!["set".into(), var.into(), value]),
<var:Value> "=" <value:Value> LEnd => LogicLine::Other(vec!["set".into(), var.into(), value]),
"read" <var:Value> <from:Value> <index:Value> LEnd
Expand Down Expand Up @@ -315,8 +314,13 @@ pub Control: LogicLine = {
},

"switch" <value:Value>
<cases:MBlock<("case" <LiteralUInt+> ":" <Expand>)+>>
<cases:MBlock<(
<LogicLine?> // append line
<("case" <LiteralUInt+> ":" <Expand>)+>
)>>
=> {
let (append, cases) = cases;

let case_num_max = cases
.iter()
.map(
Expand All @@ -328,7 +332,10 @@ pub Control: LogicLine = {
.max()
.unwrap();
let mut cases_res = Vec::with_capacity(case_num_max);
for (nums, expand) in cases {
for (nums, mut expand) in cases {
if let Some(append) = &append {
expand.push(append.clone())
}
for num in nums {
for _ in cases_res.len()..=num {
cases_res.push(LogicLine::NoOp)
Expand Down

0 comments on commit 25da64c

Please sign in to comment.