Skip to content

Commit

Permalink
优化没有elif和else的if为反转if条件的skip
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Aug 1, 2023
1 parent 0876235 commit ce91838
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 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.7.3"
version = "0.7.4"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
13 changes: 13 additions & 0 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,19 @@ mod tests {
"#).unwrap(),
);

assert_eq!(
parse!(parser, r#"
if 2 < 3 { # 对于没有elif与else的if, 会将条件反转并构建为skip
print 1;
}
"#).unwrap(),
parse!(parser, r#"
skip ! 2 < 3 {
print 1;
}
"#).unwrap(),
);

assert_eq!(
parse!(parser, r#"
while a < b
Expand Down
10 changes: 10 additions & 0 deletions src/syntax_def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ pub Control: LogicLine = {
<elifs:("elif" <JumpCmp> <Block>)*>
<else_body:("else" <LogicLine>)?> => {

// 对于没有elif和else的直接优化成skip
if elifs.is_empty() && else_body.is_none() {
let lab = meta.get_tag();
return Expand(vec![
Goto(lab.clone(), cmp.reverse()).into(),
body,
LogicLine::new_label(lab, meta),
]).into()
}

let end = meta.get_tag();
let true_start = meta.get_tag();
let mut elif_tags = Vec::with_capacity(elifs.len());
Expand Down

0 comments on commit ce91838

Please sign in to comment.