Skip to content

Commit

Permalink
添加便于底层调试的可使用自定义绑定分隔符
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Nov 27, 2024
1 parent 3407690 commit 3bb8142
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 7 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.17.10"
version = "0.17.11"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
4 changes: 4 additions & 0 deletions examples/builtin_functions.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
* * `RefArg`: 返回指定下标的上层参数句柄, 一般再用Const解开
* * `MissesMatch[enable]`: 当enable非0时, 将match失配打印日志功能开启
* * `NewBind[enable]`: 当enable非0时, 新建绑定量时将会打印日志
* * `BindSep[sep]`: 当sep非空字符串时, 绑定量将以指定分隔的形式输出
* 例如: take BindSep["%"]; a.b = 2;的a.b在take后的句柄会变成'a%b'
* NOTE: 注意, 如果选择的分隔符分隔后结果和现有var冲突可能会产生奇怪的问题,
* 或者在字符串上绑定等
* * `SetNoOp[line]`: 根据给定的字串设置noop语句生成的行,
* 包含一层简单的反斜杠解释规则:
* `\\` => `\`
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser-tests"
version = "0.1.39"
version = "0.1.40"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
27 changes: 27 additions & 0 deletions tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6374,6 +6374,33 @@ fn builtin_func_test() {
r#"print 5"#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
take Builtin.BindSep[x];
print a.b;
const a.b = 2;
print a.b;
print axb;
take Builtin.BindSep[""];
print axb;
print c.d;
const c.d = 3;
print c.d;
print cxd;
print __2;
"#).unwrap()).compile().unwrap(),
vec![
r#"print axb"#,
r#"print 2"#,
r#"print 2"#,
r#"print 2"#,
r#"print __2"#,
r#"print 3"#,
r#"print cxd"#,
r#"print 3"#,
],
);
}

#[test]
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.44"
version = "0.2.45"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
18 changes: 18 additions & 0 deletions tools/syntax/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,5 +427,23 @@ pub fn build_builtins() -> Vec<BuiltinFunc> {
Ok("__".into())
})
}

fn bind_sep:BindSep(meta) [v:sep] {
check_type!("var" Value::Var(sep) = sep.value() => {
match sep.as_var_type().as_string() {
Some(&"") => {
meta.bind_custom_sep = None;
},
Some(s) => {
return Err((2, format!(
"expected empty string, found: {s:?}")));
},
None => {
meta.bind_custom_sep = sep.to_owned().into();
},
}
Ok("__".into())
})
}
}
}
10 changes: 9 additions & 1 deletion tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3871,6 +3871,8 @@ pub struct CompileMeta {
enable_misses_match_log_info: bool,
enable_misses_bind_info: bool,
noop_line: String,
/// 保证不会是字符串
bind_custom_sep: Option<Var>,
}
impl Debug for CompileMeta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -3927,6 +3929,7 @@ impl CompileMeta {
enable_misses_match_log_info: false,
enable_misses_bind_info: false,
noop_line: "noop".into(),
bind_custom_sep: None,
};
let builtin = Var::from(Self::BUILTIN_FUNCS_BINDER);
for builtin_func in build_builtins() {
Expand Down Expand Up @@ -3982,7 +3985,12 @@ impl CompileMeta {
*warn = true;
}
show_new_bind = self.enable_misses_bind_info;
self.tmp_var_count.get()
if let Some(ref sep) = self.bind_custom_sep {
debug_assert!(! Value::is_string(sep), "{sep:?}");
format!("{handle}{sep}{bind}").into()
} else {
self.tmp_var_count.get()
}
}).clone();
if warn_builtin == Some(true) {
self.log_info(format!(
Expand Down

0 comments on commit 3bb8142

Please sign in to comment.