Skip to content

Commit

Permalink
给const-match做一点小优化, 尝试减缓匿名量增长速度
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed May 4, 2024
1 parent 54f45e5 commit e30ec92
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 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.16.0"
version = "0.16.1"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
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.20"
version = "0.2.21"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
30 changes: 30 additions & 0 deletions tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Error> {
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 {
Expand Down Expand Up @@ -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<I> {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit e30ec92

Please sign in to comment.