Skip to content

Commit

Permalink
添加内联块, 更新Vim的高亮与代码片段
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Aug 24, 2023
1 parent eb4f85c commit 6914d77
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 13 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.11.0"
version = "0.11.1"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
21 changes: 21 additions & 0 deletions examples/inline_block.mdtlbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#**
* 这是0.11.1新加入的语法, 它可以在不创建一个块作用域的情况下来编写行单元.
* 虽然对于使用者来说大多情况用不上, 但是在一些特殊场景还是有些用的.
*
* 当然, 这个语法的目的是直接解决语法分析器中只能展开为一个行.
* 有了这个后, 无需为了展开为多行添加一个块作用域, 直接使用内联块即可
*#

print A;
inline {
const A = 2;
print A;
}
print A;
#* >>>
print A
print 2
print 2
*#
# 可以看到, 内联块并没有创建一个块作用域, 相当于这个块直接去掉了
# 所以内联块内常量在内联块外依旧可以使用
66 changes: 66 additions & 0 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,27 @@ impl TryFrom<&TagCodes> for Expand {
}
impl_derefs!(impl for Expand => (self: self.0): Vec<LogicLine>);

#[derive(Debug, PartialEq, Clone)]
pub struct InlineBlock(pub Vec<LogicLine>);
impl DisplaySource for InlineBlock {
fn display_source(&self, meta: &mut DisplaySourceMeta) {
self.0
.iter()
.for_each(|line| {
line.display_source(meta);
meta.add_lf();
})
}
}
impl Compile for InlineBlock {
fn compile(self, meta: &mut CompileMeta) {
for line in self.0 {
line.compile(meta)
}
}
}
impl_derefs!(impl for InlineBlock => (self: self.0): Vec<LogicLine>);

/// 用于`switch`的`select`结构
/// 编译最后一步会将其填充至每个语句定长
/// 然后将`self.0`乘以每个语句的长并让`@counter += _`来跳转到目标
Expand Down Expand Up @@ -1637,6 +1658,7 @@ pub enum LogicLine {
Goto(Goto),
Other(Vec<Value>),
Expand(Expand),
InlineBlock(InlineBlock),
Select(Select),
NoOp,
/// 空语句, 什么也不生成
Expand Down Expand Up @@ -1670,6 +1692,7 @@ impl Compile for LogicLine {
},
Self::Select(select) => select.compile(meta),
Self::Expand(expand) => expand.compile(meta),
Self::InlineBlock(block) => block.compile(meta),
Self::Goto(goto) => goto.compile(meta),
Self::Op(op) => op.compile(meta),
Self::Const(r#const) => r#const.compile(meta),
Expand Down Expand Up @@ -1783,6 +1806,7 @@ impl_enum_froms!(impl From for LogicLine {
Op => Op;
Goto => Goto;
Expand => Expand;
InlineBlock => InlineBlock;
Select => Select;
Const => Const;
Take => Take;
Expand All @@ -1798,6 +1822,16 @@ impl DisplaySource for LogicLine {
});
meta.push("}");
},
Self::InlineBlock(block) => {
meta.push("inline");
meta.add_space();
meta.push("{");
meta.add_lf();
meta.do_block(|meta| {
block.display_source(meta);
});
meta.push("}");
},
Self::Ignore => meta.push("# ignore line"),
Self::NoOp => meta.push("noop;"),
Self::Label(lab) => {
Expand Down Expand Up @@ -4448,4 +4482,36 @@ mod tests {
);

}

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

assert_eq!(
parse!(parser, r#"
inline {
foo;
}
"#).unwrap(),
Expand(vec![
InlineBlock(vec![
LogicLine::Other(vec!["foo".into()])
]).into()
]).into()
);

let logic_lines = CompileMeta::new().compile(parse!(parser, r#"
print A;
inline {
const A = 2;
print A;
}
print A;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"print A",
"print 2",
"print 2",
]);
}
}
19 changes: 11 additions & 8 deletions src/syntax_def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::syntax::{
CmpTree,
Goto,
Expand,
InlineBlock,
SwitchCatch,
Select,
Const,
Expand Down Expand Up @@ -220,15 +221,21 @@ pub LogicLine: LogicLine = {
Control,
BuiltinCommand,
MBlock<Expand> => <>.into(),
"inline" <MBlock<LogicLine*>> => InlineBlock(<>).into(),
Label => LogicLine::new_label(<>, meta),
"op" <Op> LEnd => <>.into(),
"noop" LEnd => LogicLine::NoOp,
"set" <Value> <Value> LEnd => Meta::build_set(<>),
<l:@L> <vars:Args2> "=" <values:Args2> LEnd <r:@R>
=>? Meta::build_sets([l, r], vars, values)
.map_err(|e| e.into()),
=>? Meta::build_sets([l, r], vars, values).map_err(|e| e.into()),
OpExpr,
"print" <Args0> LEnd => {
Print,
<args:Args1> LEnd => {
LogicLine::Other(args)
},
}

Print: LogicLine = "print" <Args0> LEnd => {
if <>.is_empty() {
// 无参数, 啥也不做
return LogicLine::Ignore
Expand All @@ -248,11 +255,7 @@ pub LogicLine: LogicLine = {
]))
.collect()
).into()
},
<args:Args1> LEnd => {
LogicLine::Other(args)
},
}
};

// 0..
Args0: Vec<Value> = {
Expand Down
5 changes: 5 additions & 0 deletions syntax/vim/mdtlbl.snippets
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ snippet elsel "else line" w
else
$0
endsnippet
snippet inline_block "inline { ... }" w
inline {
$0
}
endsnippet
snippet const "const value" w
const ${1:NAME} = ${2:value};$0
endsnippet
Expand Down
24 changes: 21 additions & 3 deletions syntax/vim/mdtlbl.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
" Vim syntax file
" Language: mindustry_logic_bang_lang (mdtlbl)
" Maintainer: A4-Tacks <[email protected]>
" Last Change: 2023-8-11
" Last Change: 2023-8-24
" URL: https://github.com/A4-Tacks/mindustry_logic_bang_lang

" 已加载高亮时就退出
Expand All @@ -22,13 +22,25 @@ endif
" 大小写敏感 {{{1
syn case match


" 控制语句 {{{1
" 一些关键字 {{{1
syn keyword mdtlblKeyword
\ while do skip goto if elif else
\ switch case const take gwhile setres select
\ inline
\ op set noop print
hi link mdtlblKeyword Keyword

syn keyword mdtlblOpFunKeyword
\ add sub mul div idiv mod pow
\ equal notEqual land lessThan lessThanEq greaterThan greaterThanEq
\ strictEqual shl shr or and xor max
\ min angle len noise not abs log
\ floor ceil sqrt rand sin cos tan
\ asin acos atan lnot
hi link mdtlblOpFunKeyword Operator

syn match mdtlblCmpTreeOper /&&\|||\|!/
hi link mdtlblCmpTreeOper Operator

" 注释 {{{1
syn region mdtlblComment start=/#/ end=/$/
Expand Down Expand Up @@ -57,6 +69,12 @@ hi link mdtlblOtherValue Identifier
syn match mdtlblNumber /\v<(0(x\-?[0-9a-fA-F][0-9a-fA-F_]*|b\-?[01][_01]*)|\-?[0-9][0-9_]*(\.[0-9][0-9_]*)?)>/
hi link mdtlblNumber Number

syn match mdtlblBoolean /\v<true|false>/
hi link mdtlblBoolean Boolean

syn match mdtlblNull /\<null\>/
hi link mdtlblNull Boolean

syn match mdtlblResultHandle /\$/
hi link mdtlblResultHandle Identifier

Expand Down

0 comments on commit 6914d77

Please sign in to comment.