Skip to content

Commit

Permalink
添加展开层数限制与相关内置函数
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jan 18, 2024
1 parent 0fdd685 commit 003d7ec
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 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.14.12"
version = "0.14.13"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
3 changes: 3 additions & 0 deletions examples/builtin_functions.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* * `SliceArgs[start end]`: 将参数切分, 但不take
* * `ArgsHandle[idx]`: 拿到指定下标的参数的const句柄, 配合Const内置函数转移其值
* * `MetaDebug[]`: 以调试形式输出编译时元数据
* * `MaxExpandDepth[]`: 获取最大展开层数限制
* * `SetMaxExpandDepth[depth]`: 设置最大展开层数限制
* * `ExpandStack[]`: 调试输出当前展开栈情况
*#

print Builtin.Type[x];
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.9"
version = "0.2.10"
edition = "2021"

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

fn max_expand_depth:MaxExpandDepth(meta) [] {
Ok(meta.const_expand_max_depth().to_string())
}

fn set_max_expand_depth:SetMaxExpandDepth(meta) [d:depth] {
check_type!("var" Value::Var(depth) = depth.value() => {
let depth: usize = match depth.parse() {
Ok(n) => n,
Err(e) => {
return Err((2, e.to_string()))
},
};
meta.set_const_expand_max_depth(depth);
Ok("__".into())
})
}

fn expand_stack:ExpandStack(meta) [] {
meta.log_expand_stack();
Ok("__".into())
}
}
}
50 changes: 49 additions & 1 deletion tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2692,6 +2692,8 @@ pub struct CompileMeta {
/// 一个标签从尾部上寻, 寻到就返回找到的, 没找到就返回原本的
/// 所以它支持在宏A内部展开的宏B跳转到宏A内部的标记
const_expand_tag_name_map: Vec<HashMap<Var, Var>>,
const_expand_names: Vec<Var>,
const_expand_max_depth: usize,
value_binds: HashMap<(Var, Var), Var>,
/// 值绑定全局常量表, 只有值绑定在使用它
value_bind_global_consts: HashMap<Var, ConstData>,
Expand Down Expand Up @@ -2746,6 +2748,8 @@ impl CompileMeta {
dexp_expand_binders: Vec::new(),
tmp_tag_count: Counter::new(Self::tmp_tag_getter),
const_expand_tag_name_map: Vec::new(),
const_expand_names: Vec::new(),
const_expand_max_depth: 500,
value_binds: HashMap::new(),
value_bind_global_consts: HashMap::new(),
last_builtin_exit_code: 0,
Expand Down Expand Up @@ -3057,6 +3061,19 @@ impl CompileMeta {
/// 如果不是一个宏则直接返回None, 也不会进入无需清理
pub fn const_expand_enter(&mut self, name: &Var) -> Option<Value> {
let label_count = self.get_const_value(name)?.labels().len();
if self.const_expand_names.len() >= self.const_expand_max_depth {
self.log_err(format!(
"Stack Expand:\n{}",
self.debug_expand_stack()
.collect::<Vec<_>>()
.join(", "),
));
err!(
"Maximum recursion depth exceeded ({})",
self.const_expand_max_depth,
);
exit(6)
}
let mut tmp_tags = Vec::with_capacity(label_count);
tmp_tags.extend(repeat_with(|| self.get_tmp_tag())
.take(label_count));
Expand All @@ -3077,11 +3094,14 @@ impl CompileMeta {
let res = value.clone();
self.dexp_expand_binders.push(binder.clone());
self.const_expand_tag_name_map.push(labels_map);
self.const_expand_names.push(name.clone());
res.into()
}

pub fn const_expand_exit(&mut self) -> (HashMap<Var, Var>, Option<Var>) {
pub fn const_expand_exit(&mut self)
-> (Var, HashMap<Var, Var>, Option<Var>) {
(
self.const_expand_names.pop().unwrap(),
self.const_expand_tag_name_map.pop().unwrap(),
self.dexp_expand_binders.pop().unwrap(),
)
Expand Down Expand Up @@ -3190,13 +3210,41 @@ impl CompileMeta {
.trim_end().replace('\n', "\n "))
}

pub fn debug_expand_stack(&self) -> impl Iterator<Item = String> + '_ {
self.const_expand_names().iter()
.zip(&self.dexp_expand_binders)
.map(|x| match x {
(name, Some(binder)) => format!("{name} ..{binder}"),
(name, None) => name.into(),
})
}

pub fn log_expand_stack(&mut self) {
let names = self.debug_expand_stack()
.collect::<Vec<_>>()
.join("\n");
self.log_info(format!("Expand Stack:\n{names}"))
}

pub fn last_builtin_exit_code(&self) -> u8 {
self.last_builtin_exit_code
}

pub fn set_last_builtin_exit_code(&mut self, new_code: u8) -> u8 {
replace(&mut self.last_builtin_exit_code, new_code)
}

pub fn const_expand_names(&self) -> &[String] {
self.const_expand_names.as_ref()
}

pub fn const_expand_max_depth(&self) -> usize {
self.const_expand_max_depth
}

pub fn set_const_expand_max_depth(&mut self, const_expand_max_depth: usize) {
self.const_expand_max_depth = const_expand_max_depth;
}
}

pub fn line_first_add(lines: &mut Vec<String>, insert: &str) {
Expand Down

0 comments on commit 003d7ec

Please sign in to comment.