Skip to content

Commit

Permalink
增加两个take相关的语法糖
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Aug 22, 2024
1 parent bea2326 commit 94d6a1d
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 20 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.16.21"
version = "0.16.22"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
6 changes: 6 additions & 0 deletions examples/op_expr.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,9 @@ i++; j--;
op ___1 ___1 - `1`;
}
*#


# 在0.16.22版本为了使take配合op-expr更加简洁, 添加了一个语法糖
take*A, B = x+y, i++;
# 相当于
take A=(*x+y) B=(*i++);
6 changes: 6 additions & 0 deletions examples/take.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ print __1

# 可以看到, 我们可以对常量的值计算一次但是多次使用结果
# 如果直接`print F;`, 将会每次都进行计算


# 可以直接take一个空的DExp, 利用其自动分配匿名返回句柄的行为声明一个匿名量
take X = ();
# 在0.16.22版本有一个语法糖, 可以更简短的完成这个例子
take+X;
3 changes: 2 additions & 1 deletion syntax/vim/mdtlbl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ syn case match
" 一些关键字 {{{1
syn keyword mdtlblKeyword
\ while gwhile do skip if elif else switch gswitch break continue
\ const take setres select match
\ const setres select match
\ inline
\ op noop print
syn keyword mdtlblKeyword goto nextgroup=mdtlblIdentLabelRest
syn keyword mdtlblKeyword case nextgroup=mdtlblStar skipwhite
syn keyword mdtlblKeyword take nextgroup=mdtlblStar skipwhite
syn match mdtlblStar /\*/ contained

syn keyword mdtlblOpFunKeyword
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.19"
version = "0.3.20"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
54 changes: 41 additions & 13 deletions tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -921,25 +921,53 @@ pub BuiltinCommand: LogicLine = {
)
},

"take"
<mut takes:(<(<ConstKey> "=")?> <Value>)+>
LEnd => {
if takes.len() == 1 {
let (res, value) = takes.pop().unwrap();
let res = res.unwrap_or_else(|| meta.unnamed_var().into());
return Take(res, value).into();
"take" <lines:TakeAtom+> LEnd => {
if lines.len() == 1 {
lines.into_iter().next().unwrap()
} else {
InlineBlock(lines).into()
}
let mut lines = Vec::with_capacity(takes.len());
for (res, value) in takes.into_iter() {
let res = res.unwrap_or_else(|| meta.unnamed_var().into());
let take = Take(res, value);
lines.push(take.into())
},

"take" "*" <inner:TakeOpExprMultInner> LEnd => {
let (keys, vals) = inner;
assert_eq!(keys.len(), vals.len());

let lines = keys.into_iter()
.rev()
.zip(vals)
.map(|(key, val)| {
Take(key, val).into()
})
.collect::<Vec<LogicLine>>();

if lines.len() == 1 {
lines.into_iter().next().unwrap()
} else {
InlineBlock(lines).into()
}
InlineBlock(lines).into()
},

"setres" <Value> LEnd => LogicLine::SetResultHandle(<>),
}
TakeOpExprMultInner: (Vec<ConstKey>, Vec<Value>) = {
<key:ConstKey> "=" <val:OpExprBodyToValue> => (vec![key], vec![val]),
<key:ConstKey> "," <mut inner:TakeOpExprMultInner> "," <val:OpExprBodyToValue> => {
inner.0.push(key);
inner.1.push(val);
inner
},
}
#[inline]
TakeAtom: LogicLine = {
"+" <key:ConstKey> => {
Take(key, DExp::new_nores(vec![].into()).into()).into()
},
<key:(<ConstKey> "=")?> <val:Value> => {
let key = key.unwrap_or_else(|| meta.unnamed_var().into());
Take(key, val).into()
},
}

BlockExpand: Expand = MBlock<Expand> => <>;
pub Block: LogicLine = BlockExpand => <>.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.33"
version = "0.1.34"
edition = "2021"

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

assert_eq!(
parse!(parser, r#"
take+A+B+C+D;
"#).unwrap(),
parse!(parser, r#"
inline {
take A = ();
take B = ();
take C = ();
take D = ();
}
"#).unwrap(),
);
}

#[test]
Expand Down Expand Up @@ -2647,6 +2661,17 @@ fn op_expr_test() {
print (__: setres i; $ = $ + `1`;);
"#).unwrap(),
);

assert_eq!(
parse!(parser, r#"
take*A, B = x+y, i++;
take*C = j--;
"#).unwrap(),
parse!(parser, r#"
take A = (*x+y) B = (*i++);
take C = (*j--);
"#).unwrap(),
);
}

#[test]
Expand Down

0 comments on commit 94d6a1d

Please sign in to comment.