Skip to content

Commit

Permalink
给match也添加设置返回句柄的支持
Browse files Browse the repository at this point in the history
- 移除不应该的数字常量评估中Binder的错误实现
- 给标签类错误添加建议使用编译选项的提示
  • Loading branch information
A4-Tacks committed Jan 22, 2025
1 parent 29df89d commit 05a53e9
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 35 deletions.
10 changes: 5 additions & 5 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.15"
version = "0.17.16"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
4 changes: 3 additions & 1 deletion examples/learn.md
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,9 @@ op add c a b
其中`A:`部分可省略
- `@`: 在这个位置匹配任意个数的值, 并将匹配到的值进行`# setArgs`,
每个分支只能存在一个`@`
- 其它匹配前面加上`$`可以方便的将这个匹配到的值进行 setres

match 里面由零至多个匹配后面跟上一个花括号组成, 称作一个分支, 可以有多个分支

而除了普通 match, 还存在一种 const-match,
区别是 match 会将所有参数进行求值后拿其句柄进行匹配,
Expand All @@ -1159,7 +1162,6 @@ op add c a b
而 const-match 还多出一些可用的匹配:

- 其它匹配前面加上`*`则使用 take 而不是 const 这个值到要绑定到的常量
- 其它匹配前面加上`$`可以方便的将这个匹配到的值进行 setres
- 在方括号匹配里面方括号头部加上`*`则尝试先将待匹配的值进行求值后匹配其句柄
- 在方括号匹配里面方括号头部加上`?`则方括号内输入 op-expr,
来通过返回0还是1来确定是否匹配
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn logic_to_tagcode<'a>(lines: ParseLines<'a>, src: &str) -> TagCodes {
Err(e) => {
let (line, column) = e.location(src);
let prefix = format!("ParseTagCode {line}:{column}");
err!("{prefix} {e}");
err!("{prefix} {e}\n或许你可以使用`Li`选项编译来详细查看");
exit(10)
},
};
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.25"
version = "0.3.26"
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/display_source/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,9 @@ impl DisplaySource for MatchPatAtom {
fn display_source(&self, meta: &mut DisplaySourceMeta) {
let show_name = !self.name().is_empty();
let show_list = !self.pattern().is_empty();
if self.set_res() {
meta.push("$");
}
if show_name {
meta.push(self.name());
if show_list { meta.push(":") }
Expand All @@ -578,6 +581,9 @@ impl DisplaySource for MatchPatAtom {
meta.display_source_iter_by_space(self.pattern());
meta.push("]");
}
if !show_name && !show_list {
meta.push("_");
}
}
}
impl DisplaySource for MatchPat {
Expand Down Expand Up @@ -1066,6 +1072,46 @@ fn display_source_test() {
.display_source_and_get(&mut meta),
"match {}"
);
assert_eq!(
parse!(line_parser, r#"
match { $X {} }
"#)
.unwrap()
.display_source_and_get(&mut meta),
"match {\n $X {}\n}"
);
assert_eq!(
parse!(line_parser, r#"
match { $X:[1] {} }
"#)
.unwrap()
.display_source_and_get(&mut meta),
"match {\n $X:[1] {}\n}"
);
assert_eq!(
parse!(line_parser, r#"
match { $[1 2] {} }
"#)
.unwrap()
.display_source_and_get(&mut meta),
"match {\n $[1 2] {}\n}"
);
assert_eq!(
parse!(line_parser, r#"
match { _ {} }
"#)
.unwrap()
.display_source_and_get(&mut meta),
"match {\n _ {}\n}"
);
assert_eq!(
parse!(line_parser, r#"
match { $_ {} }
"#)
.unwrap()
.display_source_and_get(&mut meta),
"match {\n $_ {}\n}"
);
assert_eq!(
parse!(line_parser, r#"
foo 'match';
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser"
version = "0.3.24"
version = "0.3.25"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
9 changes: 5 additions & 4 deletions tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,10 @@ Match: Match = {
}

MatchPat: MatchPatAtom = {
MList<Args1> => MatchPatAtom::new_unamed(<>),
Var => MatchPatAtom::new(<>, vec![]),
<name:Var> ":" <pat:MList<Args1>> => MatchPatAtom::new(name, pat),
<sr:Opt<"$">> <pat:MList<Args1>> => MatchPatAtom::new_unamed(pat, sr),
<sr:Opt<"$">> "_" => MatchPatAtom::new_unamed(vec![], sr),
<sr:Opt<"$">> <v:Var> => MatchPatAtom::new(v, vec![], sr),
<sr:Opt<"$">> <name:Var> ":" <pat:MList<Args1>> => MatchPatAtom::new(name, pat, sr),
}

ConstMatch: ConstMatch = {
Expand Down Expand Up @@ -454,7 +455,7 @@ ConstMatchPatAtom: ConstMatchPatAtom = {
Args::GLOB_ONLY,
vec![
(
vec![MatchPatAtom::new_unamed(pat)].into(),
vec![MatchPatAtom::new_unamed(pat, false)].into(),
vec![
LogicLine::SetResultHandle(
Value::ReprVar("1".into())
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.41"
version = "0.1.42"
edition = "2021"

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

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Foo = (match @ {
{ print 0; }
_ { print 1; }
_ _ { print 2; }
});
take Foo[] Foo[3] Foo[3 3];
"#).unwrap()).compile().unwrap(),
vec![
"print 0",
"print 1",
"print 2",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Foo = (match @ {
$_ { print 1; }
});
print Foo[6];
"#).unwrap()).compile().unwrap(),
vec![
"print 1",
"print 6",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Foo = (match @ {
$X { print 8 X 8; }
});
print Foo[6];
"#).unwrap()).compile().unwrap(),
vec![
"print 8",
"print 6",
"print 8",
"print 6",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Foo = (match @ {
$X $Y { print 8 X 8; }
});
print Foo[6 9];
"#).unwrap()).compile().unwrap(),
vec![
"print 8",
"print 6",
"print 8",
"print 9",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Foo = (match @ {
X $Y { print 8 X 8; }
});
print Foo[6 9];
"#).unwrap()).compile().unwrap(),
vec![
"print 8",
"print 6",
"print 8",
"print 9",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Foo = (match @ {
$X Y { print 8 X 8; }
});
print Foo[6 9];
"#).unwrap()).compile().unwrap(),
vec![
"print 8",
"print 6",
"print 8",
"print 6",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Foo = (match @ {
X Y { print 8 X 8; }
});
print Foo[6 9];
"#).unwrap()).compile().unwrap(),
vec![
"print 8",
"print 6",
"print 8",
"print __2",
],
);
}

#[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.45"
version = "0.2.46"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Loading

0 comments on commit 05a53e9

Please sign in to comment.