diff --git a/Cargo.lock b/Cargo.lock index c9c831f..cc33967 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,7 +100,7 @@ dependencies = [ [[package]] name = "display_source" -version = "0.3.16" +version = "0.3.17" dependencies = [ "parser", "syntax", @@ -301,7 +301,7 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "mindustry_logic_bang_lang" -version = "0.16.8" +version = "0.16.9" dependencies = [ "display_source", "logic_lint", @@ -535,7 +535,7 @@ dependencies = [ [[package]] name = "syntax" -version = "0.2.27" +version = "0.2.28" dependencies = [ "either", "tag_code", diff --git a/Cargo.toml b/Cargo.toml index c9f58bf..d302646 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.16.8" +version = "0.16.9" edition = "2021" authors = ["A4-Tacks "] diff --git a/examples/builtin_functions.mdtlbl b/examples/builtin_functions.mdtlbl index 9a511a0..745c3be 100644 --- a/examples/builtin_functions.mdtlbl +++ b/examples/builtin_functions.mdtlbl @@ -29,6 +29,7 @@ * * `IsString[str]`: 返回给定量是否是一个字符串 * * `RefArg`: 返回指定下标的上层参数句柄, 一般再用Const解开 * * `MissesMatch[enable]`: 当enable非0时, 将match失配打印日志功能开启 +* * `MissesBind[enable]`: 当enable非0时, 将绑定量失配打印日志功能开启 * * `SetNoOp[line]`: 根据给定的字串设置noop语句生成的行, * 包含一层简单的反斜杠解释规则: * `\\` => `\` diff --git a/tools/display_source/Cargo.toml b/tools/display_source/Cargo.toml index ca24460..3e0f512 100644 --- a/tools/display_source/Cargo.toml +++ b/tools/display_source/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "display_source" -version = "0.3.16" +version = "0.3.17" 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 384393e..c8c0942 100644 --- a/tools/display_source/src/impls.rs +++ b/tools/display_source/src/impls.rs @@ -47,21 +47,54 @@ impl DisplaySource for ClosuredValueMethod { } impl DisplaySource for ClosuredValue { fn display_source(&self, meta: &mut DisplaySourceMeta) { - let Self::Uninit { - catch_values, - value, - labels, - } = self else { - panic!("failed builded value: {:?}", self) - }; - meta.push("(["); - meta.display_source_iter_by_splitter(|meta| { - meta.add_space(); - }, catch_values); - meta.push("]"); - value.display_source(meta); - inline_labs(labels, meta); - meta.push(")"); + match self { + ClosuredValue::Uninit { + catch_values, + value, + labels, + } => { + meta.push("(["); + meta.display_source_iter_by_splitter( + |meta| { + meta.add_space(); + }, + catch_values, + ); + meta.push("]"); + value.display_source(meta); + inline_labs(labels, meta); + meta.push(")"); + }, + ClosuredValue::Inited { + bind_handle, + vars, + } => { + struct CatchedVar<'a>(&'a Var); + impl DisplaySource for CatchedVar<'_> { + fn display_source( + &self, + meta: &mut DisplaySourceMeta, + ) { + self.0.display_source(meta); + meta.push(":__catched"); + } + } + let catcheds = vars.iter() + .map(|var| CatchedVar(var)) + .collect::>(); + meta.push("(["); + meta.display_source_iter_by_splitter( + |meta| { + meta.add_space(); + }, + &catcheds, + ); + meta.push("]"); + bind_handle.display_source(meta); + meta.push(")"); + }, + ClosuredValue::Empty => unreachable!(), + } } } impl DisplaySource for Value { diff --git a/tools/syntax/Cargo.toml b/tools/syntax/Cargo.toml index 249e927..28bc295 100644 --- a/tools/syntax/Cargo.toml +++ b/tools/syntax/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "syntax" -version = "0.2.27" +version = "0.2.28" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/tools/syntax/src/builtins.rs b/tools/syntax/src/builtins.rs index d43d2c9..ce30f62 100644 --- a/tools/syntax/src/builtins.rs +++ b/tools/syntax/src/builtins.rs @@ -386,6 +386,14 @@ pub fn build_builtins() -> Vec { Ok("__".into()) }) } + + fn misses_bind:MissesBind(meta) [v:enable] { + check_type!("var" Value::Var(enable) = enable.value() => { + meta.enable_misses_bind_info = enable != "0"; + Ok("__".into()) + }) + } + fn set_noop:SetNoOp(meta) [l:line] { check_type!("var" Value::Var(line) = line.value() => { let line = if Value::is_string(line) { diff --git a/tools/syntax/src/lib.rs b/tools/syntax/src/lib.rs index beefd95..f5af39a 100644 --- a/tools/syntax/src/lib.rs +++ b/tools/syntax/src/lib.rs @@ -104,12 +104,19 @@ macro_rules! do_return { } macro_rules! csi { (@ignore($($i:tt)*) $($t:tt)*) => ($($t)*); - ($fcode:expr $(, $code:expr)*; $($arg:tt)*) => { + ($fcode:expr $(, $code:expr)*; $efcode:expr $(, $ecode:expr)*; $($arg:tt)*) => { format_args!( - concat!("\x1b[{}", $(csi!(@ignore($code) ";{}"), )* "m{}\x1b[0m"), + concat!( + "\x1b[{}", + $(csi!(@ignore($code) ";{}"), )* + "m{}\x1b[{}", $(csi!(@ignore($ecode) ";{}"), )* + "m", + ), $fcode, $($code,)* format_args!($($arg)*), + $efcode, + $($ecode,)* ) }; } @@ -3782,6 +3789,7 @@ pub struct CompileMeta { value_bind_global_consts: HashMap, last_builtin_exit_code: u8, enable_misses_match_log_info: bool, + enable_misses_bind_info: bool, noop_line: String, } impl Debug for CompileMeta { @@ -3843,6 +3851,7 @@ impl CompileMeta { value_bind_global_consts: HashMap::new(), last_builtin_exit_code: 0, enable_misses_match_log_info: false, + enable_misses_bind_info: false, noop_line: "noop".into(), }; let builtin = String::from(Self::BUILTIN_FUNCS_BINDER); @@ -3896,20 +3905,26 @@ impl CompileMeta { /// 获取绑定值, 如果绑定关系不存在则自动插入 pub fn get_value_binded(&mut self, value: Var, bind: Var) -> Var { let mut warn_builtin = (value == Self::BUILTIN_FUNCS_BINDER) - .then_some((false, bind.clone())); - let key = (value, bind); + .then_some(false); + let key = (value.clone(), bind.clone()); let binded = self.value_binds.entry(key) .or_insert_with(|| { - if let Some((ref mut warn, _)) = warn_builtin { + if let Some(ref mut warn) = warn_builtin { *warn = true; } self.tmp_var_count.get() }).clone(); - if let Some((true, bind)) = warn_builtin { + if warn_builtin == Some(true) { self.log_info(format!( "Missed Builtin Call: {}", bind.display_src(self), )); + } else if self.enable_misses_bind_info { + self.log_info(format!( + "Missed Bind Get: {}.{}", + value.display_src(self), + bind.display_src(self), + )); } binded } @@ -4324,12 +4339,12 @@ impl CompileMeta { } pub fn log_info(&mut self, s: impl std::fmt::Display) { - eprintln!("{}", csi!(1; "[I] {}", + eprintln!("{}", csi!(1; 22; "[I] {}", s.to_string().trim_end().replace('\n', "\n "))) } pub fn log_err(&mut self, s: impl std::fmt::Display) { - eprintln!("{}", csi!(1, 91; "[E] {}", + eprintln!("{}", csi!(1, 91; 22, 39; "[E] {}", s.to_string().trim_end().replace('\n', "\n "))) }