From e30ec9221212e66b09c7118acb018744d6e862b3 Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Sat, 4 May 2024 10:31:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99const-match=E5=81=9A=E4=B8=80=E7=82=B9?= =?UTF-8?q?=E5=B0=8F=E4=BC=98=E5=8C=96,=20=E5=B0=9D=E8=AF=95=E5=87=8F?= =?UTF-8?q?=E7=BC=93=E5=8C=BF=E5=90=8D=E9=87=8F=E5=A2=9E=E9=95=BF=E9=80=9F?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- tools/syntax/Cargo.toml | 2 +- tools/syntax/src/lib.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f12d1c..44abf0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,7 +301,7 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "mindustry_logic_bang_lang" -version = "0.16.0" +version = "0.16.1" dependencies = [ "display_source", "logic_lint", @@ -535,7 +535,7 @@ dependencies = [ [[package]] name = "syntax" -version = "0.2.20" +version = "0.2.21" dependencies = [ "either", "tag_code", diff --git a/Cargo.toml b/Cargo.toml index f02abe6..51af004 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.16.0" +version = "0.16.1" edition = "2021" authors = ["A4-Tacks "] diff --git a/tools/syntax/Cargo.toml b/tools/syntax/Cargo.toml index c3560a8..ad609d2 100644 --- a/tools/syntax/Cargo.toml +++ b/tools/syntax/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "syntax" -version = "0.2.20" +version = "0.2.21" 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 f3b1df4..a93eb6c 100644 --- a/tools/syntax/src/lib.rs +++ b/tools/syntax/src/lib.rs @@ -36,6 +36,25 @@ macro_rules! impl_enum_froms { } )* }; } +macro_rules! impl_enum_try_into { + (impl TryInto for $ty:ty {$( + $variant:ident => $target:ty ; + )*}) => { + $( + impl TryFrom<$ty> for $target { + type Error = $ty; + + fn try_from(value: $ty) -> Result { + use $ty::*; + match value { + $variant(v) => Ok(v), + this => Err(this), + } + } + } + )* + }; +} macro_rules! impl_derefs { (impl $([$($t:tt)*])? for $ty:ty => ($self_:ident : $expr:expr): $res_ty:ty) => { impl $(<$($t)*>)? ::std::ops::Deref for $ty { @@ -562,6 +581,12 @@ impl_enum_froms!(impl From for Value { BuiltinFunc => BuiltinFunc; ValueBindRef => ValueBindRef; }); +impl_enum_try_into!(impl TryInto for Value { + Var => Var; + DExp => DExp; + ValueBind => ValueBind; + ValueBindRef => ValueBindRef; +}); /// 一次性的迭代器格式化包装器 struct IterFmtter { @@ -2830,6 +2855,11 @@ impl Compile for ConstMatch { let args = self.args.into_value_args(meta) .into_iter() .map(|value| { + if let Some(var) = value.as_var() { + if meta.get_const_value(var).is_some() { + return value.try_into().unwrap(); + } + } let handle = meta.get_tmp_var(); Const( handle.clone().into(),