Skip to content

Commit

Permalink
为以初始化的闭包添加显示, 增加可选的未命中绑定日志
Browse files Browse the repository at this point in the history
- 改进log系列的颜色显示, 不清除多余的
  • Loading branch information
A4-Tacks committed May 14, 2024
1 parent d6ee701 commit c77ce7c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 29 deletions.
6 changes: 3 additions & 3 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.8"
version = "0.16.9"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
1 change: 1 addition & 0 deletions examples/builtin_functions.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* * `IsString[str]`: 返回给定量是否是一个字符串
* * `RefArg`: 返回指定下标的上层参数句柄, 一般再用Const解开
* * `MissesMatch[enable]`: 当enable非0时, 将match失配打印日志功能开启
* * `MissesBind[enable]`: 当enable非0时, 将绑定量失配打印日志功能开启
* * `SetNoOp[line]`: 根据给定的字串设置noop语句生成的行,
* 包含一层简单的反斜杠解释规则:
* `\\` => `\`
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.16"
version = "0.3.17"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
63 changes: 48 additions & 15 deletions tools/display_source/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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 {
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.27"
version = "0.2.28"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
8 changes: 8 additions & 0 deletions tools/syntax/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ pub fn build_builtins() -> Vec<BuiltinFunc> {
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) {
Expand Down
31 changes: 23 additions & 8 deletions tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,)*
)
};
}
Expand Down Expand Up @@ -3782,6 +3789,7 @@ pub struct CompileMeta {
value_bind_global_consts: HashMap<Var, ConstData>,
last_builtin_exit_code: u8,
enable_misses_match_log_info: bool,
enable_misses_bind_info: bool,
noop_line: String,
}
impl Debug for CompileMeta {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 ")))
}

Expand Down

0 comments on commit c77ce7c

Please sign in to comment.