Skip to content

Commit

Permalink
添加mul-takes和mul-consts语法, 用于简化一些小型场景
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Nov 26, 2023
1 parent 669a195 commit 161621c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 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.0"
version = "0.13.1"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
> [`shell_sort.mdtlbl`](./shell_sort.mdtlbl)<br/>
> [`switch_catch.mdtlbl`](./switch_catch.mdtlbl)<br/>
> [`take2.mdtlbl`](./take2.mdtlbl)<br/>
> [`mul_takes_and_consts.mdtlbl`](./mul_takes_and_consts.mdtlbl)<br/>
> [`cmper.mdtlbl`](./cmper.mdtlbl)<br/>
> [`setres.mdtlbl`](./setres.mdtlbl)<br/>
> [`consted_dexp.mdtlbl`](./consted_dexp.mdtlbl)<br/>
Expand Down
17 changes: 17 additions & 0 deletions examples/mul_takes_and_consts.mdtlbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#**
* 这是0.13.1加入的功能, 可以在一条语句中方便的进行多个take和const
* 当然这不能在take语句本身进行传参了, 不然显得太过混乱.
*
* 语法应该也很熟悉, 直接看示例
*#

const A=1 B=2 C=3;
take X=A Y=B Z=C;
print X Y Z;
printflush message1;
#* >>>
print 1
print 2
print 3
printflush message1
*#
32 changes: 30 additions & 2 deletions src/syntax/def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,23 @@ OpExprAtom: OpExprInfo = {

// 开始一个const, 开启了必须负责清理
ConstStart: () = () => meta.add_label_scope();
ConstStop: Vec<Var> = () => meta.pop_label_scope();

pub BuiltinCommand: LogicLine = {
"const" <var:Var> "=" ConstStart <value:Value> LEnd
=> Const(var, value, meta.pop_label_scope()).into(),
"const" <mut values:(<Var> "=" ConstStart <Value> <ConstStop>)+> LEnd
=> {
if values.len() == 1 {
let (var, value, labels) = values.pop().unwrap();
Const(var, value, labels).into()
} else {
let mut res = Vec::with_capacity(values.len());
for (var, value, labels) in values {
let r#const = Const(var, value, labels);
res.push(r#const.into());
}
InlineBlock(res).into()
}
},

// 如果后方是一个Var则直接将常量映射到后方的值
// 如果后方是一个DExp则将其计算然后将常量映射到计算出的句柄
Expand All @@ -592,6 +605,21 @@ pub BuiltinCommand: LogicLine = {
)
},

// 两个以上take
"take"
<first:(<(<Var> "=")?> <Value>)>
<nexts:(<(<Var> "=")?> <Value>)+>
LEnd => {
let mut lines = Vec::with_capacity(1 + nexts.len());
let iter = [first].into_iter().chain(nexts);
for (res, value) in iter {
let res = res.unwrap_or_else(|| "__".into());
let take = Take(res, value);
lines.push(take.into())
}
InlineBlock(lines).into()
},

"setres" <Value> LEnd => LogicLine::SetResultHandle(<>),
}

Expand Down
50 changes: 50 additions & 0 deletions src/syntax/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3544,3 +3544,53 @@ fn cmper_test() {
);

}

#[test]
fn mul_takes_test() {
let parser = TopLevelParser::new();

assert_eq!(
parse!(parser, r#"
take A=B C=D E=F G=I;
"#).unwrap(),
parse!(parser, r#"
inline {
take A = B;
take C = D;
take E = F;
take G = I;
}
"#).unwrap(),
);
}

#[test]
fn mul_consts_test() {
let parser = TopLevelParser::new();

assert_eq!(
parse!(parser, r#"
const A=B C=D E=F G=I;
"#).unwrap(),
parse!(parser, r#"
inline {
const A = B;
const C = D;
const E = F;
const G = I;
}
"#).unwrap(),
);

assert_eq!( // label
parse!(parser, r#"
const A=(:m goto :m;) C=(if x {});
"#).unwrap(),
parse!(parser, r#"
inline {
const A = (:m goto :m;);
const C = (if x {});
}
"#).unwrap(),
);
}

0 comments on commit 161621c

Please sign in to comment.