Skip to content

Commit

Permalink
修复比较依赖没有封闭参数, 统一match设置原始参数形式
Browse files Browse the repository at this point in the history
- 设置原始参数不包括重复块
  • Loading branch information
A4-Tacks committed Oct 28, 2024
1 parent 24d095c commit fb0fd28
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 19 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.8"
version = "0.17.9"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
3 changes: 2 additions & 1 deletion examples/builtin_functions.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* * `Info[var]`: 以info级日志形式将传入Var输出
* * `Err[var]`: 以err级日志形式将传入Var输出
* * `Unbind[var]`: 传入一个值绑定, 返回绑定者
* * `Const[name value]`: 动态目标的进行一个const, 并泄露到上层
* * `Const[name value]`: 动态目标的进行一个const, 并泄露到上层,
* 调用者需要保证被泄漏的expand不会展开次数不同, 比如直接位于重复块中操作重复块外
* * `Binder[name value]`: 传入一个值绑定, 将其被绑定值const给给定名称
* * `BindHandle[value]`: 直接获取值绑定的句柄, 但是不进行最后一层全局常量域追踪
* * `BindHandle2[value name]`: 直接获取值绑定的句柄, 但是不进行最后一层全局常量域追踪.
Expand Down
8 changes: 6 additions & 2 deletions examples/match.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
*
* 修改了参数方式, 由构建期展开为编号参数改为编译期展开特定语句
*
* 重复块在重复时会将匹配到的参数设置环境参数
* HINT: 只会设置`@`这种形式的使用, 并不包括_0这种
*
* ---
*
* 内部语句SetArgs, 它将设置参数, 其作用范围为Expand和重复块
* 内部语句SetArgs, 它将设置参数(包括_0 _1 这种旧形式),
* 其作用范围为Expand
*
* 扩展Other, Match和Print的参数,
* 增加展开符`@`, 展开符将会展开为全部参数, 参数来源于SetArgs
* 增加展开符`@`, 展开符将会展开为全部参数, 参数来源于SetArgs设置的环境参数
*
* 可以使用重复块, 它将在一个内联块中重复给定块, 并且每次重复迭代指定个参数,
* 此数字未指定则为1, 它不能为0, 它是构建期被指定的
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.37"
version = "0.1.38"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
62 changes: 62 additions & 0 deletions tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4171,6 +4171,28 @@ fn cmper_test() {
print __;
"#).unwrap()).compile().unwrap(),
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
break ({match 1 2 3 => @ {}} => _);
print _0 @;
"#).unwrap()).compile().unwrap(),
vec![
"jump 0 always 0 0",
"print _0",
]
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
break (=>[1 2 3] _);
print _0 @;
"#).unwrap()).compile().unwrap(),
vec![
"jump 0 always 0 0",
"print _0",
]
);
}

#[test]
Expand Down Expand Up @@ -5234,6 +5256,44 @@ fn match_test() {
}
"#).unwrap(),
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
match 1 2 => @ {
print _0 _1 @;
}
print _0 _1 @;
"#).unwrap()).compile().unwrap(),
vec![
"print 1",
"print 2",
"print 1",
"print 2",
"print 1",
"print 2",
"print 1",
"print 2",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const match 1 2 => @ {
print _0 _1 @;
}
print _0 _1 @;
"#).unwrap()).compile().unwrap(),
vec![
"print 1",
"print 2",
"print 1",
"print 2",
"print 1",
"print 2",
"print 1",
"print 2",
],
);
}

#[test]
Expand Down Expand Up @@ -6099,13 +6159,15 @@ fn builtin_func_test() {
const Value = (i:);
match Name Value { @ {} }
take Builtin.Const;
print Y;
}
print Y;
}
"#).unwrap()).compile().unwrap(),
vec![
"print h",
"print i",
"print i",
],
);

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.42"
version = "0.2.43"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
37 changes: 27 additions & 10 deletions tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ impl CmpTree {

match self {
Deps(deps, cmp) => {
meta.with_block(|meta| {
meta.with_block_and_env_args(|meta| {
deps.compile(meta);
cmp.build(meta, do_tag_expanded);
});
Expand Down Expand Up @@ -2162,12 +2162,10 @@ impl Compile for Goto {
pub struct Expand(pub Vec<LogicLine>);
impl Compile for Expand {
fn compile(self, meta: &mut CompileMeta) {
meta.with_block(|this| {
this.with_env_args_block(|this| {
for line in self.0 {
line.compile(this)
}
});
meta.with_block_and_env_args(|meta| {
for line in self.0 {
line.compile(meta)
}
});
}
}
Expand Down Expand Up @@ -2908,7 +2906,10 @@ impl MatchPat {
for ((name, _), arg) in a.chain(b) {
binds(name, arg, meta)
}
meta.set_env_args(extracted)
let extracted = extracted.iter()
.map_into()
.collect::<Vec<_>>();
LogicLine::SetArgs(extracted.into()).compile(meta);
}).is_some()
},
_ => false,
Expand Down Expand Up @@ -3102,7 +3103,10 @@ impl ConstMatchPat {
for ele in right_datas {
make(ele, meta);
}
meta.set_env_args(&handles[mid_rng]);
let expand_args = handles[mid_rng].iter()
.map_into()
.collect::<Vec<_>>();
LogicLine::SetArgs(expand_args.into()).compile(meta);
code.compile(meta);
Ok(())
},
Expand Down Expand Up @@ -3305,7 +3309,7 @@ impl Compile for GSwitch {
fn compile(self, meta: &mut CompileMeta) {
use GSwitchCase as GSC;

meta.with_block(|meta| {
meta.with_block_and_env_args(|meta| {
let mut missed_lab = self
.case_lab_with_cond(GSwitchCase::is_missed, meta);
let underflow_lab = self
Expand Down Expand Up @@ -4045,6 +4049,19 @@ impl CompileMeta {
block_exit(self)
}

/// like `with_block(|meta| meta.with_env_args_block(f))`
pub fn with_block_and_env_args<F>(&mut self, f: F)
-> (HashMap<Var, ConstData>, Option<Vec<Var>>)
where F: FnOnce(&mut Self)
{
let mut inner_res = None;
let block_res = self.with_block(|meta| {
inner_res = Some(meta.with_env_args_block(f))
});

(block_res, inner_res.unwrap())
}

/// 添加一个需泄露的const
pub fn add_const_value_leak(&mut self, name: Var) {
self.expand_env
Expand Down

0 comments on commit fb0fd28

Please sign in to comment.