Skip to content

Commit

Permalink
添加了select语句, 可以看成丐版switch,
Browse files Browse the repository at this point in the history
它是较为原始的直接对应语法树中`Select`的语句,
有时相较`switch`也会更方便
  • Loading branch information
A4-Tacks committed Aug 8, 2023
1 parent e3ab937 commit b46a571
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion 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.7.7"
version = "0.7.8"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
20 changes: 20 additions & 0 deletions examples/control.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,23 @@ jump 2 lessThan i 10
*#
# 它的缺点是, 需要多一条跳转, 所以这导致while并没有采用这种形式
# 当然, 在手写逻辑时, 我们很喜欢这么写, 因为比较省力


select x {
print 0;
print 1 "is one!";
print 2;
}
#* >>>
op mul __0 x 2
op add @counter @counter __0
print 0
noop
print 1
print "is one!"
print 2
noop
*#
# select是原始版本, 没有switch那么多的功能,
# 但是相比switch的好处是不用编写case, 直接跳转到指定行
# 第0行, 第1行 以此类推
26 changes: 26 additions & 0 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2802,4 +2802,30 @@ mod tests {
"print a",
]);
}

#[test]
fn select_test() {
let parser = ExpandParser::new();

let logic_lines = CompileMeta::new().compile(parse!(parser, r#"
select 1 {
print 0;
{
print 1 " is one!";
}
print 2;
}
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"op mul __0 1 2",
"op add @counter @counter __0",
"print 0",
"noop",
"print 1",
"print \" is one!\"",
"print 2",
"noop",
]);

}
}
5 changes: 4 additions & 1 deletion src/syntax_def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ pub BuiltinCommand: LogicLine = {
"setres" <Value> LEnd => LogicLine::SetResultHandle(<>),
}

pub Block: LogicLine = MBlock<Expand> => <>.into();
BlockExpand: Expand = MBlock<Expand> => <>;
pub Block: LogicLine = BlockExpand => <>.into();

pub DExp: DExp = MTuple<(<(<Var> ":")?> <Expand>)>
=> DExp::new_optional_res(<>.0, <>.1);
Expand Down Expand Up @@ -369,6 +370,8 @@ pub Control: LogicLine = {
Select(value, Expand(cases_res)).into()
},

"select" <value:Value> <lines:BlockExpand> => Select(value, lines).into(),

"if" <cmp:JumpCmp> <body:Block>
<elifs:("elif" <JumpCmp> <Block>)*>
<else_body:("else" <LogicLine>)?> => {
Expand Down
11 changes: 8 additions & 3 deletions syntax/vim/mdtlbl.snippets
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ skip ${1:_} {
}
endsnippet
snippet if "if" w
if ${1:_} {
if $1 {
$0
}
endsnippet
snippet elif "elif" w
elif ${1:_} {
elif $1 {
$0
}
endsnippet
Expand All @@ -53,7 +53,7 @@ snippet iop "op self" w
op ${1:i} $1 ${2:+} ${3:1};$0
endsnippet
snippet switch "switch" w
switch ${1:x} {
switch $1 {
$0
}
endsnippet
Expand Down Expand Up @@ -87,3 +87,8 @@ endsnippet
snippet drawflush "drawflush message1" w
drawflush ${1:display1};$0
endsnippet
snippet select "select" w
select $1 {
$0
}
endsnippet
4 changes: 3 additions & 1 deletion syntax/vim/mdtlbl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ syn case match


" 控制语句 {{{1
syn keyword mdtlblKeyword while do skip goto if elif else switch case const take gwhile setres
syn keyword mdtlblKeyword
\ while do skip goto if elif else
\ switch case const take gwhile setres select
hi link mdtlblKeyword Keyword


Expand Down

0 comments on commit b46a571

Please sign in to comment.