Skip to content

Commit

Permalink
添加cmp-deps关于参数设置的语法糖, 并且修整重复块的示例
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed May 4, 2024
1 parent 6f947f3 commit d677671
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 7 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.16.1"
version = "0.16.2"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
11 changes: 11 additions & 0 deletions examples/cmp_deps.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* 它的优先级是溢出的, 大部分情况需要在括号内使用, 其优先级高于`||`
*
* 它为自身左右运算成员构建了一层作用域
*
* 在0.16.2版本中, 添加了一个语法糖,
* 可在箭头后面添加方括号进行正常的参数设置, 并且此时可以省略前面的代码体
*#

const X = ($ = 1 + 2;);
Expand All @@ -11,3 +14,11 @@ while ({take N = X;} => N > 10 && N < 30) {
noop;
}
# 不用这个语法来解决这个问题的话, 总是不那么美观, 或者只能是看着X被take多次


# 利用参数设置语法糖
# 这里用到了cmper
const Cmper = goto(_0 > 0 && _0 < _1);
while (=>[a b] Cmper) {
noop;
}
5 changes: 5 additions & 0 deletions examples/match.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* 简单概述为凭借对句柄的模式匹配完成分支条件编译
*
* 同时还增加了重复块, 可以对参数进行重复
* 需要注意的是, 重复块中的被跳转标签并没有被处理, 这是需要注意的
* 也就是说很可能你在重复块中编写被跳转标签如控制语句就会引起错误
* 并且排查可能还需要一番功夫
* 所以你只应该在其中进行较简单的语句, 或者使用拥有子标签域的方式
* 例如 直接take一个consted-DExp
*
* 修改了参数方式, 由构建期展开为编号参数改为编译期展开特定语句
*
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ fn build_tag_down(meta: &mut CompileMeta) {
{}\n\
TagsMap:\n\
{}",
tag_str,
tag_codes.join("\n"),
tags_map.join("\n"),
tag_str,
);
exit(4)
})
Expand Down
25 changes: 24 additions & 1 deletion tools/display_source/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,12 @@ impl DisplaySource for LogicLine {
Self::SetArgs(args) => {
meta.do_insert_first("# ".into(), |meta| {
meta.push("setArgs");
meta.add_space();
if !args.as_normal()
.map(Vec::is_empty)
.unwrap_or_default()
{
meta.add_space();
}

args.display_source(meta);
meta.push(";");
Expand Down Expand Up @@ -742,6 +747,24 @@ fn display_source_test() {
.display_source_and_get(&mut meta),
"({\n take X = A;\n take Y = B;\n} => ((X > 10 && Y > 20) && X < Y))"
);
assert_eq!(
parse!(jumpcmp_parser, "(=>[A B] X == 2)")
.unwrap()
.display_source_and_get(&mut meta),
"({\n inline {}\n # setArgs A B;\n} => X == 2)"
);
assert_eq!(
parse!(jumpcmp_parser, "(=>[] X == 2)")
.unwrap()
.display_source_and_get(&mut meta),
"({\n inline {}\n # setArgs;\n} => X == 2)"
);
assert_eq!(
parse!(jumpcmp_parser, "(=>[@] X == 2)")
.unwrap()
.display_source_and_get(&mut meta),
"({\n inline {}\n # setArgs @;\n} => X == 2)"
);
assert_eq!(
parse!(line_parser, r#"set a "\n\\\[hi]\\n";"#)
.unwrap()
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.8"
version = "0.3.9"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
9 changes: 8 additions & 1 deletion tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,14 @@ pub JumpCmpOnce: CmpTree = CmpTree1;
AlwaysJumpCmp: CmpTree = JumpCmp? => <>.unwrap_or(JumpCmp::Always.into());

CmpTree1: CmpTree = {
<deps:MBlock<LogicLine*>> "=>" <cmp:CmpTree1> => CmpTree::Deps(deps.into(), cmp.into()),
<deps:MBlock<LogicLine*>> "=>" <cmp:CmpTree1>
=> CmpTree::Deps(deps.into(), cmp.into()),
<deps:MBlock<LogicLine*>?> "=>" <args:MList<Args?>> <cmp:CmpTree1> => {
CmpTree::Deps(vec![
InlineBlock::from(deps.unwrap_or_default()).into(),
LogicLine::SetArgs(args.unwrap_or_default()),
].into(), cmp.into())
},
CmpTree2,
}

Expand Down

0 comments on commit d677671

Please sign in to comment.