From 072735355241f629a7fab0900e9cdc54c058047e Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Tue, 28 Jan 2025 06:17:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=E5=9C=A8const-match=E4=B8=AD?= =?UTF-8?q?,=20=E4=B8=BA`@`=E4=B9=9F=E4=BD=BF=E7=94=A8`*`=E8=BF=9B?= =?UTF-8?q?=E8=A1=8Ctake?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 10 ++--- Cargo.toml | 2 +- examples/const_match.mdtlbl | 7 ++++ examples/function.mdtlbl | 4 +- tools/display_source/Cargo.toml | 2 +- tools/display_source/src/impls.rs | 5 ++- tools/parser/Cargo.toml | 2 +- tools/parser/src/parser.lalrpop | 2 +- tools/parser/tests/Cargo.toml | 2 +- tools/parser/tests/src/lib.rs | 68 +++++++++++++++++++++++++++++++ tools/syntax/Cargo.toml | 2 +- tools/syntax/src/lib.rs | 21 +++++++--- 12 files changed, 106 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2e95222..624cc34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -88,7 +88,7 @@ dependencies = [ [[package]] name = "display_source" -version = "0.3.26" +version = "0.3.27" dependencies = [ "either", "parser", @@ -285,7 +285,7 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "mindustry_logic_bang_lang" -version = "0.17.20" +version = "0.17.21" dependencies = [ "display_source", "logic_lint", @@ -331,7 +331,7 @@ dependencies = [ [[package]] name = "parser" -version = "0.3.27" +version = "0.3.28" dependencies = [ "lalrpop", "lalrpop-util", @@ -342,7 +342,7 @@ dependencies = [ [[package]] name = "parser-tests" -version = "0.1.46" +version = "0.1.47" dependencies = [ "either", "parser", @@ -531,7 +531,7 @@ dependencies = [ [[package]] name = "syntax" -version = "0.2.50" +version = "0.2.51" dependencies = [ "either", "itermaps", diff --git a/Cargo.toml b/Cargo.toml index f260a0d..72a1116 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.17.20" +version = "0.17.21" edition = "2021" authors = ["A4-Tacks "] diff --git a/examples/const_match.mdtlbl b/examples/const_match.mdtlbl index 72aca76..6bb1d1c 100644 --- a/examples/const_match.mdtlbl +++ b/examples/const_match.mdtlbl @@ -84,3 +84,10 @@ const match @ { } } # 在只有一个分支时可以减少一层括号, 用于快速接收参数等情况非常舒适 + + +# 在0.17.21版本对@匹配也允许加*对其进行take了 +const match (foo;) => *@ { } +#* >>> +foo +*# diff --git a/examples/function.mdtlbl b/examples/function.mdtlbl index 5da91ec..998efd0 100644 --- a/examples/function.mdtlbl +++ b/examples/function.mdtlbl @@ -10,9 +10,7 @@ Builtin.BindSep! '.'; Builtin.MissesMatch! 1; -const Function = (const match @ => @ F { - match @ => @ { } - +const Function = (const match @ => *@ F { take B = $; skip { diff --git a/tools/display_source/Cargo.toml b/tools/display_source/Cargo.toml index a2a5c1f..76a1aa1 100644 --- a/tools/display_source/Cargo.toml +++ b/tools/display_source/Cargo.toml @@ -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 diff --git a/tools/display_source/src/impls.rs b/tools/display_source/src/impls.rs index d186633..3d8c770 100644 --- a/tools/display_source/src/impls.rs +++ b/tools/display_source/src/impls.rs @@ -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(); diff --git a/tools/parser/Cargo.toml b/tools/parser/Cargo.toml index cddc5c9..c9378a2 100644 --- a/tools/parser/Cargo.toml +++ b/tools/parser/Cargo.toml @@ -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 diff --git a/tools/parser/src/parser.lalrpop b/tools/parser/src/parser.lalrpop index c3daaaa..11eb587 100644 --- a/tools/parser/src/parser.lalrpop +++ b/tools/parser/src/parser.lalrpop @@ -438,7 +438,7 @@ ConstMatch: ConstMatch = { ConstMatchPat: ConstMatchPat = { ConstMatchPatAtom* => ConstMatchPat::Normal(<>), - "@" + > "@" => ConstMatchPat::Expanded(<>), } diff --git a/tools/parser/tests/Cargo.toml b/tools/parser/tests/Cargo.toml index edefea2..67b0405 100644 --- a/tools/parser/tests/Cargo.toml +++ b/tools/parser/tests/Cargo.toml @@ -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 diff --git a/tools/parser/tests/src/lib.rs b/tools/parser/tests/src/lib.rs index eb14805..012fc63 100644 --- a/tools/parser/tests/src/lib.rs +++ b/tools/parser/tests/src/lib.rs @@ -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] @@ -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] diff --git a/tools/syntax/Cargo.toml b/tools/syntax/Cargo.toml index 7f5e4c7..b732ca3 100644 --- a/tools/syntax/Cargo.toml +++ b/tools/syntax/Cargo.toml @@ -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 diff --git a/tools/syntax/src/lib.rs b/tools/syntax/src/lib.rs index 7c935ec..42ab025 100644 --- a/tools/syntax/src/lib.rs +++ b/tools/syntax/src/lib.rs @@ -3114,13 +3114,13 @@ impl Compile for ConstMatch { #[derive(Debug, PartialEq, Clone)] pub enum ConstMatchPat { Normal(Vec), - Expanded(Vec, Vec), + Expanded(Vec, /**take*/bool, Vec), } 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 }, } @@ -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) @@ -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::>(); LogicLine::SetArgs(expand_args.into()).compile(meta); + + for ele in right_datas { + make(ele, meta); + } code.compile(meta); Ok(()) },