Skip to content

Commit

Permalink
优化了select在0行和1行下的行为
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Sep 3, 2023
1 parent 124d814 commit 3939486
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 21 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.11.3"
version = "0.11.4"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
100 changes: 82 additions & 18 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,21 +1472,39 @@ impl Compile for Select {
)
.count()
}).collect();
let max_len = lens.iter().copied().max().unwrap();
let max_len = lens.iter().copied().max().unwrap_or_default();

let counter = Value::ReprVar(COUNTER.into());

// build head
let tmp_var = meta.get_tmp_var();
let mut head = Op::Mul(
tmp_var.clone().into(),
self.0,
max_len.to_string().into()
).compile_take(meta);
let head_1 = Op::Add(
COUNTER.into(),
COUNTER.into(),
tmp_var.into()
).compile_take(meta);
head.extend(head_1);
let head = match max_len {
0 => { // no op
Take("__".into(), self.0).compile_take(meta)
},
1 => { // no mul
Op::Add(
counter.clone(),
counter,
self.0
).compile_take(meta)
},
// normal
_ => {
let tmp_var = meta.get_tmp_var();
let mut head = Op::Mul(
tmp_var.clone().into(),
self.0,
Value::ReprVar(max_len.to_string())
).compile_take(meta);
let head_1 = Op::Add(
counter.clone(),
counter,
tmp_var.into()
).compile_take(meta);
head.extend(head_1);
head
}
};

// 填补不够长的`case`
for (len, case) in zip(lens, &mut cases) {
Expand Down Expand Up @@ -2710,13 +2728,13 @@ mod tests {
Select(
"2".into(),
Expand(vec![
LogicLine::NoOp,
LogicLine::Ignore,
Expand(vec![LogicLine::Other(vec![Value::ReprVar("print".into()), "1".into()])]).into(),
Expand(vec![
LogicLine::Other(vec![Value::ReprVar("print".into()), "2".into()]),
LogicLine::Other(vec![Value::ReprVar("print".into()), "4".into()]),
]).into(),
LogicLine::NoOp,
LogicLine::Ignore,
Expand(vec![
LogicLine::Other(vec![Value::ReprVar("print".into()), "2".into()]),
LogicLine::Other(vec![Value::ReprVar("print".into()), "4".into()]),
Expand Down Expand Up @@ -3861,9 +3879,7 @@ mod tests {
let logic_lines = CompileMeta::new().compile(parse!(parser, r#"
select 1 {
print 0;
{
print 1 " is one!";
}
print 1 " is one!";
print 2;
}
"#).unwrap()).compile().unwrap();
Expand All @@ -3878,6 +3894,32 @@ mod tests {
"noop",
]);

let logic_lines = CompileMeta::new().compile(parse!(parser, r#"
select x {
print 0;
print 1;
print 2;
}
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"op add @counter @counter x",
"print 0",
"print 1",
"print 2",
]);

let logic_lines = CompileMeta::new().compile(parse!(parser, r#"
select (y: op $ x + 2;) {}
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"op add y x 2",
]);

let logic_lines = CompileMeta::new().compile(parse!(parser, r#"
select x {}
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, Vec::<&str>::new());

}

#[test]
Expand Down Expand Up @@ -4174,6 +4216,28 @@ mod tests {
}
}
"#).unwrap());

let logic_lines = CompileMeta::new().compile(parse!(parser, r#"
switch (op $ x + 2;) {
case !:
stop;
case 1:
case 3:
}
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, CompileMeta::new().compile(parse!(parser, r#"
take tmp = (op $ x + 2;);
skip _ {
:mis
stop;
}
select tmp {
goto :mis _;
noop;
goto :mis _;
noop;
}
"#).unwrap()).compile().unwrap());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/syntax_def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ pub Control: LogicLine = {
let mut fill_line = append
.as_ref()
.map(|line| Expand(vec![line.clone()]).into())
.unwrap_or(LogicLine::NoOp);
.unwrap_or(LogicLine::Ignore);

// 用于添加到头部的捕获块
let mut catch_lines = Vec::new();
Expand Down

0 comments on commit 3939486

Please sign in to comment.