Skip to content

Commit

Permalink
移除set关键字, DExp增加可选的不进行追溯的返回句柄定义
Browse files Browse the repository at this point in the history
- 增加一个快速DExp和引用互动的语法糖
- 整理vim语法文件
  • Loading branch information
A4-Tacks committed May 28, 2024
1 parent 33b083d commit aacb57c
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 151 deletions.
12 changes: 6 additions & 6 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.13"
version = "0.16.14"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
2 changes: 2 additions & 0 deletions examples/const.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 内联常量的值是一个Value, 也就是说你可以往里面塞一个DExp, 这可以有效的提高代码复用
* Value有一个特殊的值 `$`, 这个值为包裹这个Value的DExp的返回句柄
* 例如 `(x: read $ cell1 0;)`, 其中的`$`代表了这个DExp的返回句柄`x`
*
* 需要注意, DExp定义返回句柄的位置是会尝试对定义的Var进行追溯的, 并且只允许Var
*#

const N = 2;
Expand Down
2 changes: 1 addition & 1 deletion examples/dexp.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* 需要注意的是, 对于Var这些, 它们的句柄即为它们自己本身
*
* 语法方面的困惑可以查看源码中的`syntax_def.lalrpop`文件
* 语法方面的困惑可以查看源码中的`parser.lalrpop`文件
* `...: ... = ... => ...;`这种就是一个类型定义, 而`=>`前面的就是语法定义, 后方的不用管那是逻辑处理
* 例如`pub Foo: FooStruct = "!" <r"\d+"> => { ... };`
* pub是可见性, 不用管
Expand Down
1 change: 1 addition & 0 deletions examples/value_bind_ref.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 语法大致为`Value->Name`, 并且Name的位置还可以有两个特殊值
* 如果是`..`, 那么将返回其绑定者, 这只会对箭头左边的值进行常量追溯而不是take
* 如果是`$`, 那么将其take并返回其句柄, 这在一些const时想要take很好用
* 还有一个语法糖, `Value->[...]` 应等价 `Value[...]->$`
*
* 返回绑定者的值只会进行常量追溯而不是进行take
*#
Expand Down
101 changes: 52 additions & 49 deletions syntax/vim/mdtlbl.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
" Vim syntax file
" Language: mindustry_logic_bang_lang (mdtlbl)
" Maintainer: A4-Tacks <[email protected]>
" Last Change: 2023-12-24
" URL: https://github.com/A4-Tacks/mindustry_logic_bang_lang
" Last Change: 2024-05-27
" URL: https://github.com/A4-Tacks/mindustry_logic_bang_lang
scriptencoding utf-8

" 已加载高亮时就退出
if exists("b:current_syntax")
if exists('b:current_syntax')
finish
endif

Expand All @@ -28,7 +29,7 @@ syn keyword mdtlblKeyword
\ while gwhile do skip if elif else switch gswitch break continue
\ const take setres select match
\ inline
\ op set noop print
\ op noop print
syn keyword mdtlblKeyword goto nextgroup=mdtlblIdentLabelRest
syn keyword mdtlblKeyword case nextgroup=mdtlblStar skipwhite
syn match mdtlblStar /\*/ contained
Expand All @@ -46,20 +47,20 @@ syn match mdtlblCmpTreeOper /&&\|||\|!\|=>/
syn match mdtlblArgsExpand /@/

" 注释 {{{1
syn region mdtlblComment start=/#\%([^*]\|$\)/ end=/$/ oneline contains=mdtlblCommentMeta
syn region mdtlblLongComment start=/#\*/ end=/\*#/ contains=mdtlblCommentMeta fold
syn region mdtlblComment start=/#\%([^*]\|$\)/ end=/$/ contains=mdtlblCommentMeta oneline
syn region mdtlblLongComment start=/#\*/ end=/\*#/ contains=mdtlblCommentMeta fold
syn keyword mdtlblCommentMeta Todo TODO Note NOTE Hint HINT

setlocal comments=s:#*,mb:*,ex:*#,:#
setlocal commentstring=#%s
setlocal formatoptions+=rq

" 值(Var) {{{1
syn match mdtlblStringFailedEscape /\\\%("\@=\|.\)/ contained
syn match mdtlblStringColor /\[\v%(#\x{6,8}|%(c%(lear|yan|oral)|b%(l%(ack|ue)|r%(own|ick))|white|li%(ghtgray|me)|g%(r%(ay|een)|old%(enrod)?)|darkgray|navy|r%(oyal|ed)|s%(late|ky|carlet|almon)|t%(eal|an)|acid|forest|o%(live|range)|yellow|p%(ink|urple)|ma%(genta|roon)|violet))=\]/ contained
syn match mdtlblSpecialChar /^ *\\ \|\\\%([n\\[]\|$\)/ contained
syn cluster mdtlblStringContains add=mdtlblSpecialChar,mdtlblStringFailedEscape,mdtlblStringColor
syn region mdtlblString start=/"/ end=/"/ contains=@mdtlblStringContains
syn match mdtlblStringFailedEscape /\\\%("\@=\|.\)/ contained
syn match mdtlblStringColor contained /\[\v%(#\x{6,8}|%(c%(lear|yan|oral)|b%(l%(ack|ue)|r%(own|ick))|white|li%(ghtgray|me)|g%(r%(ay|een)|old%(enrod)?)|darkgray|navy|r%(oyal|ed)|s%(late|ky|carlet|almon)|t%(eal|an)|acid|forest|o%(live|range)|yellow|p%(ink|urple)|ma%(genta|roon)|violet))=\]/
syn match mdtlblSpecialChar /^ *\\ \|\\\%([n\\[]\|$\)/ contained
syn cluster mdtlblStringContains contains=mdtlblSpecialChar,mdtlblStringFailedEscape,mdtlblStringColor
syn region mdtlblString start=/"/ end=/"/ contains=@mdtlblStringContains

syn match mdtlblOIdent /@\I\i*\%(-\i*\)*/
syn match mdtlblOtherVar /'[^' \t]\+'/
Expand All @@ -75,27 +76,28 @@ syn match mdtlblDefineResultHandle /\v%(\([%?]=)@2<=0%(x-=_@![0-9a-fA-F_]+|b-=_@
syn match mdtlblDefineResultHandle /\v%(\([%?]=)@2<=%(\I\i*|\@\I\i*%(-\i*)*|'[^' \t]+'):/
syn match mdtlblDefineResultHandle /\v%(\([%?]=)@2<="[^"]*":/ contains=@mdtlblStringContains

syn match mdtlblQuickDExpTakeIdent /\I\i*\%(\[\)\@=/
syn match mdtlblQuickDExpTakeIdent /'[^' \t]\+'\%(\[\)\@=/
syn match mdtlblQuickDExpTakeIdent /\v\@\I\i*%(-\i*)*%(%(-\>)=\[)@=/
syn match mdtlblQuickDExpTakeIdent /\v\I\i*%(%(-\>)=\[)@=/
syn match mdtlblQuickDExpTakeIdent /\v'[^' \t]+'%(%(-\>)=\[)@=/
syn match mdtlblQuickDExpTakeIdent /->/

syn match mdtlblIdentLabel /\v%(^|\W@1<=):%(\I\i*|\@\I\i*%(-\i*)*|'[^' \t]+')/ nextgroup=mdtlblIdentLabelRest
syn match mdtlblIdentLabel /\v%(^|\W@1<=):-=_@![0-9_]+%(\._@![0-9_]+|e[+-]=-=_@![0-9_]+)=>/ nextgroup=mdtlblIdentLabelRest
syn match mdtlblIdentLabel /\v%(^|\W@1<=):0%(x-=_@![0-9a-fA-F_]+|b-=_@![01_]+)>/ nextgroup=mdtlblIdentLabelRest
syn region mdtlblIdentLabel start=/\v%(^|\W@1<=):"/ end=/"/ contains=@mdtlblStringContains
syn match mdtlblIdentLabel /\v%(^|\W@1<=):%(\I\i*|\@\I\i*%(-\i*)*|'[^' \t]+')/ nextgroup=mdtlblIdentLabelRest
syn match mdtlblIdentLabel /\v%(^|\W@1<=):-=_@![0-9_]+%(\._@![0-9_]+|e[+-]=-=_@![0-9_]+)=>/ nextgroup=mdtlblIdentLabelRest
syn match mdtlblIdentLabel /\v%(^|\W@1<=):0%(x-=_@![0-9a-fA-F_]+|b-=_@![01_]+)>/ nextgroup=mdtlblIdentLabelRest
syn region mdtlblIdentLabel start=/\v%(^|\W@1<=):"/ end=/"/ contains=@mdtlblStringContains

syn match mdtlblIdentLabelRest /\v:%(\I\i*|\@\I\i*%(-\i*)*|'[^' \t]+')/ nextgroup=mdtlblIdentLabelRest contained
syn match mdtlblIdentLabelRest /\v:-=_@![0-9_]+%(\._@![0-9_]+|e[+-]=-=_@![0-9_]+)=>/ nextgroup=mdtlblIdentLabelRest contained
syn match mdtlblIdentLabelRest /\v:0%(x-=_@![0-9a-fA-F_]+|b-=_@![01_]+)>/ nextgroup=mdtlblIdentLabelRest contained
syn region mdtlblIdentLabelRest start=/:"/ end=/"/ contains=@mdtlblStringContains contained
syn match mdtlblIdentLabelRest /\v:%(\I\i*|\@\I\i*%(-\i*)*|'[^' \t]+')/ nextgroup=mdtlblIdentLabelRest contained
syn match mdtlblIdentLabelRest /\v:-=_@![0-9_]+%(\._@![0-9_]+|e[+-]=-=_@![0-9_]+)=>/ nextgroup=mdtlblIdentLabelRest contained
syn match mdtlblIdentLabelRest /\v:0%(x-=_@![0-9a-fA-F_]+|b-=_@![01_]+)>/ nextgroup=mdtlblIdentLabelRest contained
syn region mdtlblIdentLabelRest start=/:"/ end=/"/ contains=@mdtlblStringContains contained

" Fold {{{1
setlocal foldmethod=syntax
syn region mdtlblBlock start=/{/ end=/}/ transparent fold
syn region mdtlblDExp start=/(\[\@=/ end=/)/ transparent
syn region mdtlblDExp start=/(\[\@!/ end=/)/ transparent fold
syn region mdtlblArgs matchgroup=mdtlblArgsBracket start=/(\@<!\[/ end=/\]/ transparent fold
syn region mdtlblArgs start=/(\@<=\[/ end=/\]/ transparent fold
syn region mdtlblBlock start=/{/ end=/}/ transparent fold
syn region mdtlblDExp start=/(\[\@!/ end=/)/ transparent fold
syn region mdtlblArgs start=/(\@<!\[/ end=/]/ transparent fold matchgroup=mdtlblArgsBracket
syn region mdtlblClos start=/(\[\@=/ end=/)/ transparent
syn region mdtlblClos start=/(\@<=\[/ end=/]/ transparent fold

" Indent (缩进控制) {{{1
function! <SID>lineFilter(line)
Expand Down Expand Up @@ -153,27 +155,28 @@ setlocal indentkeys+==.
setlocal indentkeys+=0->

" END And Color Links {{{1
hi def link mdtlblKeyword Keyword
hi def link mdtlblStar Keyword
hi def link mdtlblOpFunKeyword Operator
hi def link mdtlblCmpTreeOper Operator
hi def link mdtlblComment Comment
hi def link mdtlblLongComment Comment
hi def link mdtlblCommentMeta Todo
hi def link mdtlblStringFailedEscape Error
hi def link mdtlblStringColor Include
hi def link mdtlblSpecialChar SpecialChar
hi def link mdtlblString String
hi def link mdtlblOIdent Identifier
hi def link mdtlblOtherVar Identifier
hi def link mdtlblNumber Number
hi def link mdtlblBoolean Boolean
hi def link mdtlblNull Boolean
hi def link mdtlblResultHandle Identifier
hi def link mdtlblDefineResultHandle Identifier
hi def link mdtlblIdentLabel Label
hi def link mdtlblIdentLabelRest mdtlblIdentLabel
hi def link mdtlblArgsBracket Macro
hi def link mdtlblQuickDExpTakeIdent Macro
hi def link mdtlblArgsExpand Structure
hi def link mdtlblKeyword Keyword
hi def link mdtlblStar Keyword
hi def link mdtlblOpFunKeyword Operator
hi def link mdtlblCmpTreeOper Operator
hi def link mdtlblComment Comment
hi def link mdtlblLongComment Comment
hi def link mdtlblCommentMeta Todo
hi def link mdtlblStringFailedEscape Error
hi def link mdtlblStringColor Include
hi def link mdtlblSpecialChar SpecialChar
hi def link mdtlblString String
hi def link mdtlblOIdent Identifier
hi def link mdtlblOtherVar Identifier
hi def link mdtlblNumber Number
hi def link mdtlblBoolean Boolean
hi def link mdtlblNull Boolean
hi def link mdtlblResultHandle Identifier
hi def link mdtlblDefineResultHandle Identifier
hi def link mdtlblIdentLabel Label
hi def link mdtlblIdentLabelRest mdtlblIdentLabel
hi def link mdtlblArgsBracket Macro
hi def link mdtlblQuickDExpTakeIdent Macro
hi def link mdtlblArgsExpand Structure
" }}}1
" vim:nowrap ts=8 sts=8 noet
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.20"
version = "0.3.21"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
12 changes: 6 additions & 6 deletions tools/display_source/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,25 +868,25 @@ fn display_source_test() {
parse!(line_parser, "print ($ = x;);")
.unwrap()
.display_source_and_get(&mut meta),
"`'print'` (`'set'` $ x;);"
"`'print'` (`set` $ x;);"
);
assert_eq!(
parse!(line_parser, "print (res: $ = x;);")
.unwrap()
.display_source_and_get(&mut meta),
"`'print'` (res: `'set'` $ x;);"
"`'print'` (res: `set` $ x;);"
);
assert_eq!(
parse!(line_parser, "print (noop;$ = x;);")
.unwrap()
.display_source_and_get(&mut meta),
"`'print'` (\n noop;\n `'set'` $ x;\n);"
"`'print'` (\n noop;\n `set` $ x;\n);"
);
assert_eq!(
parse!(line_parser, "print (res: noop;$ = x;);")
.unwrap()
.display_source_and_get(&mut meta),
"`'print'` (res:\n noop;\n `'set'` $ x;\n);"
"`'print'` (res:\n noop;\n `set` $ x;\n);"
);
assert_eq!(
parse!(line_parser, "print a.b.c;")
Expand Down Expand Up @@ -922,7 +922,7 @@ fn display_source_test() {
parse!(line_parser, "'take' 'set' 'print' 'const' 'take' 'op';")
.unwrap()
.display_source_and_get(&mut meta),
"'take' 'set' 'print' 'const' 'take' 'op';"
"'take' set 'print' 'const' 'take' 'op';"
);
assert_eq!(
parse!(jumpcmp_parser, "({take X = N;} => X > 10 && X < 50)")
Expand Down Expand Up @@ -958,7 +958,7 @@ fn display_source_test() {
parse!(line_parser, r#"set a "\n\\\[hi]\\n";"#)
.unwrap()
.display_source_and_get(&mut meta),
r#"`'set'` a "\n\\[[hi]\\n";"#
r#"set a "\n\\[[hi]\\n";"#
);
assert_eq!(
parse!(line_parser, r#"foo bar baz;"#)
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.13"
version = "0.3.14"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
40 changes: 28 additions & 12 deletions tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ OOCArgs<T, S>: Vec<T> = {
Span<T> = @L T @R;
#[inline]
Opt<T>: bool = T? => <>.is_some();
#[inline]
MakeDExpBody<T>: DExp = {
<var:(<Var> ":")?> <value:T>
=> DExp::new_optional_res(var, value.into()),
"`" <var:Var> "`" ":" <value:T>
=> DExp::new_notake(var, value.into()),
}

CtrlBreakStart: () = () => meta.add_control_break_level(None);
CtrlContinueStart: () = () => meta.add_control_continue_level(None);
Expand Down Expand Up @@ -148,11 +155,11 @@ NonConstRangeValue: Value = {
ConstKey => <>.into(),
"`" <Var> "`" => ReprVar(<>), // 原始值
"$" => ResultHandle,
<name:NonConstRangeValue> <args:MList<Args?>> => {
<value:NonConstRangeValue> <args:MList<Args?>> => {
// QuickDExpTake
DExp::new("__".into(), vec![
LogicLine::SetArgs(args.unwrap_or_default()),
LogicLine::SetResultHandle(name),
LogicLine::SetResultHandle(value),
].into()).into()
},
"goto" <MTuple<JumpCmpOnce>> => Value::Cmper(<>.into()),
Expand All @@ -171,6 +178,18 @@ NonConstRangeValue: Value = {
<NonConstRangeValue> "->" "$" => {
ValueBindRef::new(<>.into(), ValueBindRefTarget::ResultHandle).into()
},
<value:NonConstRangeValue> "->" <args:MList<Args?>> => {
// Refed QuickDExpTake
let value = DExp::new("__".into(), vec![
LogicLine::SetArgs(args.unwrap_or_default()),
LogicLine::SetResultHandle(value),
].into()).into();

ValueBindRef::new(
Box::new(value),
ValueBindRefTarget::ResultHandle,
).into()
},
}

pub Value: Value = {
Expand Down Expand Up @@ -333,12 +352,10 @@ pub LogicLine: LogicLine = {
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()),
OpExpr,
Print,
//ArgsRepeatBlock => <>.into(),
<Args> LEnd => LogicLine::Other(<>),
"inline" <ArgsRepeatBlock> => <>.into(),
Match => <>.into(),
Expand Down Expand Up @@ -541,11 +558,12 @@ OpExpr: LogicLine = {
},
<IOpExpr> LEnd => <>.into(),
}
OpExprDExp: DExp = METuple<((<Var> ":")? OpExprBody)> => {
let (res, body) = <>;
let line = op_expr_build_results(meta, vec![ResultHandle], vec![body]);
DExp::new_optional_res(res, vec![line].into())
#[inline]
OpExprBodySetR: Expand = OpExprBody => {
let line = op_expr_build_results(meta, vec![ResultHandle], vec![<>]);
vec![line].into()
};
OpExprDExp: DExp = METuple<MakeDExpBody<OpExprBodySetR>>;

IOpExpr: Expand = {
<v:Value> "+=" <r:OpExprBody> => { let tmp = meta.get_tmp_var(); vec![Take(tmp.clone().into(), v).into(), Op::Add (tmp.clone().into(), tmp.into(), r.into_value(meta)).into()].into() },
Expand Down Expand Up @@ -844,10 +862,8 @@ pub BuiltinCommand: LogicLine = {
BlockExpand: Expand = MBlock<Expand> => <>;
pub Block: LogicLine = BlockExpand => <>.into();

pub DExp: DExp = MTuple<(<(<Var> ":")?> <Expand>)>
=> DExp::new_optional_res(<>.0, <>.1);
FEDExp: DExp = MFETuple<(<(<Var> ":")?> <Expand>)>
=> DExp::new_optional_res(<>.0, <>.1);
pub DExp: DExp = MTuple<MakeDExpBody<Expand>>;
FEDExp: DExp = MFETuple<MakeDExpBody<Expand>>;
#[inline]
ValueDExp: Value = DExp => <>.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.27"
version = "0.1.28"
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 aacb57c

Please sign in to comment.