Skip to content

Commit

Permalink
允许在const-match中, 为@也使用*进行take
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jan 27, 2025
1 parent 6fdabb4 commit 0727353
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 21 deletions.
10 changes: 5 additions & 5 deletions 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.17.20"
version = "0.17.21"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
7 changes: 7 additions & 0 deletions examples/const_match.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ const match @ {
}
}
# 在只有一个分支时可以减少一层括号, 用于快速接收参数等情况非常舒适


# 在0.17.21版本对@匹配也允许加*对其进行take了
const match (foo;) => *@ { }
#* >>>
foo
*#
4 changes: 1 addition & 3 deletions examples/function.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
Builtin.BindSep! '.';
Builtin.MissesMatch! 1;

const Function = (const match @ => @ F {
match @ => @ { }

const Function = (const match @ => *@ F {
take B = $;

skip {
Expand Down
2 changes: 1 addition & 1 deletion tools/display_source/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "display_source"
version = "0.3.26"
version = "0.3.27"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
5 changes: 4 additions & 1 deletion tools/display_source/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,14 @@ impl DisplaySource for ConstMatchPat {
ConstMatchPat::Normal(args) => {
meta.display_source_iter_by_space(args)
},
ConstMatchPat::Expanded(prefix, suffix) => {
ConstMatchPat::Expanded(prefix, do_take, suffix) => {
for s in prefix {
s.display_source(meta);
meta.add_space();
}
if *do_take {
meta.push("*")
}
meta.push("@");
for s in suffix {
meta.add_space();
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser"
version = "0.3.27"
version = "0.3.28"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ ConstMatch: ConstMatch = {

ConstMatchPat: ConstMatchPat = {
ConstMatchPatAtom* => ConstMatchPat::Normal(<>),
<ConstMatchPatAtom*> "@" <ConstMatchPatAtom*>
<ConstMatchPatAtom*> <Opt<"*">> "@" <ConstMatchPatAtom*>
=> ConstMatchPat::Expanded(<>),
}

Expand Down
2 changes: 1 addition & 1 deletion tools/parser/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser-tests"
version = "0.1.46"
version = "0.1.47"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
68 changes: 68 additions & 0 deletions tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5521,6 +5521,18 @@ fn match_test() {
"print __2",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const match (arg;) => @ {}
match (a;) @ (b;) => @ {}
"#).unwrap()).compile().unwrap(),
vec![
"a",
"arg",
"b",
],
);
}

#[test]
Expand Down Expand Up @@ -5993,6 +6005,62 @@ fn const_match_test() {
"foo h",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const match (arg;) => @ {}
const match (a;) @ (b;) => A @ *B {}
"#).unwrap()).compile().unwrap(),
vec![
"b",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const match (arg;) => @ {}
const match (a;) @ (b;) => *A @ *B {}
"#).unwrap()).compile().unwrap(),
vec![
"a",
"b",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const match (arg;) => @ {}
const match (a;) @ (b;) => *A *@ *B {}
"#).unwrap()).compile().unwrap(),
vec![
"a",
"arg",
"b",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const match (arg;) => @ {}
const match (a;) @ (b;) => A *@ *B {}
"#).unwrap()).compile().unwrap(),
vec![
"arg",
"b",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const match (arg;) => @ {}
const match (a;) @ (b;) => A *@ *B { end; }
"#).unwrap()).compile().unwrap(),
vec![
"arg",
"b",
"end",
],
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tools/syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "syntax"
version = "0.2.50"
version = "0.2.51"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
21 changes: 15 additions & 6 deletions tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3114,13 +3114,13 @@ impl Compile for ConstMatch {
#[derive(Debug, PartialEq, Clone)]
pub enum ConstMatchPat {
Normal(Vec<ConstMatchPatAtom>),
Expanded(Vec<ConstMatchPatAtom>, Vec<ConstMatchPatAtom>),
Expanded(Vec<ConstMatchPatAtom>, /**take*/bool, Vec<ConstMatchPatAtom>),
}
impl ConstMatchPat {
pub fn check_len(&self, value_len: usize) -> bool {
match self {
ConstMatchPat::Normal(norm) => norm.len() == value_len,
ConstMatchPat::Expanded(left, right) => {
ConstMatchPat::Expanded(left, _, right) => {
left.len() + right.len() <= value_len
},
}
Expand Down Expand Up @@ -3199,7 +3199,7 @@ impl ConstMatchPat {
code.compile(meta);
Ok(())
},
Self::Expanded(left, right) => {
Self::Expanded(left, do_take, right) => {
let mid_rng = left.len()..handles.len()-right.len();
let Some(left_datas)
= do_iter(left, meta, handles)
Expand All @@ -3214,13 +3214,22 @@ impl ConstMatchPat {
for ele in left_datas {
make(ele, meta);
}
for ele in right_datas {
make(ele, meta);
}

let packer = if !do_take { identity } else {
|arg: Value| ValueBindRef::new(
arg.into(),
ValueBindRefTarget::ResultHandle,
).into()
};
let expand_args = handles[mid_rng].iter()
.map_into()
.map(packer)
.collect::<Vec<_>>();
LogicLine::SetArgs(expand_args.into()).compile(meta);

for ele in right_datas {
make(ele, meta);
}
code.compile(meta);
Ok(())
},
Expand Down

0 comments on commit 0727353

Please sign in to comment.