Skip to content

Commit

Permalink
对于硬无限重复块使其不在头部重设参数
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jan 31, 2025
1 parent f47c619 commit 6662bab
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 22 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.18.0"
version = "0.18.1"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
13 changes: 10 additions & 3 deletions examples/learn.md
Original file line number Diff line number Diff line change
Expand Up @@ -1263,13 +1263,20 @@ print 1
如果数量省略不写则默认为`1`

> [!WARNING]
> 重复块的重复次数可以为 0, 那么它将无限的重复,
> 重复块的重复数量可以为 0, 那么它将无限的重复,
> 此时需要使用内建函数 StopRepeat 来终止下一轮重复
>
> 如果重复数量为 0 且为硬重复的情况下, 也就是 `inline 0@{}` 时,
> 不仅无限重复, 还不会把参数置空, 可以在下一轮重复获得上一轮设置的参数
通常重复块也配合着 match 一起使用, 来取出参数内容, 或者仅关心参数足够时的情况

> [!TIP]
> 重复块的重复次数类似 gswitch, 也可以参与常量系统, 但是需要额外标注才能使用
> 使用了`*`这种标注的重复块如 `inline*0@{}` 被称作软重复,
> 如果没有使用则被称作硬重复
>
> 软重复将不再输入一个固定的数字, 而是输入一个值做重复数量
```
const C = 2;
match 1 2 3 4 5 6 7 { @ {} }
Expand All @@ -1286,7 +1293,7 @@ foo 7
```

> [!NOTE]
> 重复块从常量系统中获取的重复次数是使用常量评估而不是求值进行的,
> 重复块从常量系统中获取的重复次数是对值使用常量评估而不是求值进行的,
> 所以对于一些值你可能需要先求值再给重复块使用

Expand Down
2 changes: 2 additions & 0 deletions examples/match.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ foo c
# 在0.18.0版本的重复块添加了可以无限重复的功能, 只要在重复次数写0(以前会报错)
# 终止的方式则是使用内建函数 StopRepeat, 调用了它后将不会再次运行当前重复块
# 同时增加了重复次数上限, 防止卡死, 也可以使用内建函数调整上限
#
# 在0.18.1版本将其调整为硬编码0时也就是没标注参与常量时, 每次重复不会重设参数
take*I = 0;
inline 0@ {
# 考虑到 StopRepeat 不是立即跳出, 而是执行完当前重复不再重复
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.50"
version = "0.1.51"
edition = "2021"

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

// 不重设参数情况
assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
take*I = 0;
inline 0@ {
match I { [3] { Builtin.StopRepeat!; } _ {
match @ I => @ {}
foo @;
take*I = I + 1;
} }
}
bar @; # 都保留了参数作用域
baz I; # 并不具有常量作用域
"#).unwrap()).compile().unwrap(),
vec![
"foo 0",
"foo 0 1",
"foo 0 1 2",
"bar",
"baz 3",
],
);

// 重设参数情况
assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
take*I = 0;
inline*0@ {
match I { [3] { Builtin.StopRepeat!; } _ {
match @ I => @ {}
foo @;
take*I = I + 1;
} }
}
bar @; # 都保留了参数作用域
baz I; # 并不具有常量作用域
"#).unwrap()).compile().unwrap(),
vec![
"foo 0",
"foo 1",
"foo 2",
"bar",
"baz 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.54"
version = "0.2.55"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
29 changes: 16 additions & 13 deletions tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2839,7 +2839,9 @@ impl ArgsRepeat {
impl Compile for ArgsRepeat {
fn compile(self, meta: &mut CompileMeta) {
let loc = self.count.new_value(());
let mut set_args = true;
let count = match self.count.value {
Either::Left(0) => { set_args = false; 0 },
Either::Left(count) => count,
Either::Right(value) => {
let Some((n, _)) = value.try_eval_const_num(meta) else {
Expand Down Expand Up @@ -2887,20 +2889,21 @@ impl Compile for ArgsRepeat {
.into_iter())
}.enumerate();
meta.args_repeat_flags.push(true);
while let Some((i, args)) = chunks.next() {
meta.with_env_args_scope(|meta| {
meta.set_env_args(args);
self.block.clone().compile(meta)
});
if !meta.args_repeat_flags.last().unwrap() { break }
if i >= meta.args_repeat_limit {
err!(
"Maximum repeat depth exceeded ({})",
meta.args_repeat_limit,
);
exit(6)
meta.with_env_args_scope(|meta| {
while let Some((i, args)) = chunks.next() {
if set_args { meta.set_env_args(args); }
self.block.clone().compile(meta);

if !meta.args_repeat_flags.last().unwrap() { break }
if i >= meta.args_repeat_limit {
err!(
"Maximum repeat limit exceeded ({})",
meta.args_repeat_limit,
);
exit(6)
}
}
}
});
meta.args_repeat_flags.pop().unwrap();
}
}
Expand Down

0 comments on commit 6662bab

Please sign in to comment.