Skip to content

Commit

Permalink
为闭包增加捕获标签的能力,并小修整关于颜色输出
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed May 15, 2024
1 parent d0bd736 commit 5ced7ce
Show file tree
Hide file tree
Showing 18 changed files with 362 additions and 57 deletions.
14 changes: 7 additions & 7 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.9"
version = "0.16.10"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
42 changes: 42 additions & 0 deletions examples/closured_value.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
* 有省略语法糖, 可以使用`A`表示`A:A`, 使用`&B`表示`&B:B`
*
* 语法起始处使用圆括号接方括号, 方括号内是捕获表, 方括号结束后接着一个值
*
* 在0.16.10添加了对label的捕获, 终于不用怕内层label和外层的同名了
* 使用方法在捕获的参数那里添加一个竖线, 然后在右边编写捕获的label
* 比如`([A B | :x](goto :x;))`
*#

const A = (a: print "makeA";);
Expand Down Expand Up @@ -54,3 +58,41 @@ const Clos.V = (
print "inited";
print "do";
take Clos.V;


# 使用闭包捕获标记, 不会被内层跳转影响
const F = (
const Run = (const match @ {
F {
:x
print "unexpected";
take F[];
}
});

:x
print "expected";

take Run[([| :x](
goto :x;
))];
);
take F F; # 这里证明它不会被展开标签重命名破坏
#* >>>
print "expected"
print "unexpected"
jump 0 always 0 0
print "expected"
print "unexpected"
jump 3 always 0 0
*#
# 可以看到, 并没有跳到Run里面的:x, 而是闭包捕获到的
# 如果我们将闭包去掉的话, 会得到我们不希望看到的, 如下
#* >>>
print "expected"
print "unexpected"
jump 1 always 0 0
print "expected"
print "unexpected"
jump 4 always 0 0
*#
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ macro_rules! err {
( $($args:tt)* ) => {{
let str = format!($($args)*);
let mut iter = str.lines();
eprintln!("\x1b[1;31mMainError: {}\x1b[0m", iter.next().unwrap());
eprintln!("\x1b[1;31mMainError: {}\x1b[22;39m", iter.next().unwrap());
for line in iter {
eprintln!(" \x1b[1;31m{}\x1b[0m", line);
eprintln!(" \x1b[1;31m{}\x1b[22;39m", line);
}
}};
}
Expand Down
7 changes: 2 additions & 5 deletions syntax/vim/mdtlbl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ syn match mdtlblDefineResultHandle /\%(([%?]\=\%(\s\|#\*.*\*#\|\%(#[^*].*\|#\)\=
syn match mdtlblQuickDExpTakeIdent /\I\i*\%(\[\)\@=/
syn match mdtlblQuickDExpTakeIdent /'[^' \t]\+'\%(\[\)\@=/
syn match mdtlblQuickDExpTakeIdent /->/
syn match mdtlblIdentLabel /:\I\i*/
syn match mdtlblIdentLabel /:@\I\i*\%(-\i*\)*/
syn match mdtlblIdentLabel /:'[^' \t]\+'/
syn match mdtlblIdentLabel /:\v0%(x-=%(_)@![0-9a-fA-F_]+|x-=%(_)@![01_]+)/
syn match mdtlblIdentLabel /:\v-=%(_)@![0-9_]+/
syn match mdtlblIdentLabel /\v%(\w@1<!|%(goto)@4<=)%(:%(\I\i*|\@\I\i*%(-\i*)*|'[^' \t]+'|0%(x-=%(_)@![0-9a-fA-F_]+|x-=%(_)@![01_]+)|-=%(_)@![0-9_]+))+%(:"@=)=/
syn match mdtlblIdentLabel /\v%(\w@1<!|%(goto)@4<=):"@=/

" Fold {{{1
setlocal foldmethod=syntax
Expand Down
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.17"
version = "0.3.18"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
49 changes: 46 additions & 3 deletions tools/display_source/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl DisplaySource for ClosuredValue {
match self {
ClosuredValue::Uninit {
catch_values,
catch_labels,
value,
labels,
} => {
Expand All @@ -60,13 +61,25 @@ impl DisplaySource for ClosuredValue {
},
catch_values,
);
if !catch_labels.is_empty() {
if !catch_values.is_empty() {
meta.add_space();
}
meta.push("|");
for label in catch_labels {
meta.add_space();
meta.push(":");
label.display_source(meta);
}
}
meta.push("]");
value.display_source(meta);
inline_labs(labels, meta);
meta.push(")");
},
ClosuredValue::Inited {
bind_handle,
rename_labels,
vars,
} => {
struct CatchedVar<'a>(&'a Var);
Expand All @@ -80,15 +93,27 @@ impl DisplaySource for ClosuredValue {
}
}
let catcheds = vars.iter()
.map(|var| CatchedVar(var))
.collect::<Vec<_>>();
.map(|var| CatchedVar(var));
meta.push("([");
meta.display_source_iter_by_splitter(
|meta| {
meta.add_space();
},
&catcheds,
catcheds,
);
if !rename_labels.is_empty() {
if !vars.is_empty() {
meta.add_space();
}
meta.push("|");
for (src, dst) in rename_labels {
meta.add_space();
meta.push(":");
src.display_source(meta);
meta.push("->:");
dst.display_source(meta);
}
}
meta.push("]");
bind_handle.display_source(meta);
meta.push(")");
Expand Down Expand Up @@ -1060,4 +1085,22 @@ fn display_source_test() {
}\
"
);

assert_eq!(
parse!(line_parser, r#"
const X = ([| :c :d]2);
"#)
.unwrap()
.display_source_and_get(&mut meta),
"const X = ([| :c :d]2);"
);

assert_eq!(
parse!(line_parser, r#"
const X = ([A B | :c :d]2);
"#)
.unwrap()
.display_source_and_get(&mut meta),
"const X = ([A:A B:B | :c :d]2);"
);
}
12 changes: 11 additions & 1 deletion tools/display_source/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl DisplaySourceMeta {
pub fn display_source_iter_by_splitter<'a, T: DisplaySource + 'a>(
&mut self,
mut split: impl FnMut(&mut Self),
iter: impl IntoIterator<Item = &'a T>
iter: impl IntoIterator<Item = T>
) {
let mut iter = iter.into_iter();
if let Some(s) = iter.next() {
Expand Down Expand Up @@ -202,6 +202,16 @@ where T: DisplaySource
T::display_source_and_get(&self, meta)
}
}
impl<T> DisplaySource for &'_ mut T
where T: DisplaySource
{
fn display_source(&self, meta: &mut DisplaySourceMeta) {
T::display_source(self, meta)
}
fn display_source_and_get<'a>(&self, meta: &'a mut DisplaySourceMeta) -> &'a str {
T::display_source_and_get(&self, meta)
}
}

#[cfg(test)]
mod tests {
Expand Down
2 changes: 1 addition & 1 deletion tools/logic_lint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "logic_lint"
version = "0.1.6"
version = "0.1.7"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion tools/logic_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ macro_rules! color_str {
$(";", stringify!($num), )*
"m",
$str,
"\x1b[0m",
"\x1b[22;39m",
)
};
}
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.12"
version = "0.3.13"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
11 changes: 9 additions & 2 deletions tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,17 @@ ClosuredValueCatch: ClosuredValueMethod = {
=> Const(name.into(), val, labels).into(),
}
ClosuredValue: ClosuredValue = {
MLTuple<ClosuredValueCatch*, (ConstStart <Value> <ConstStop>)> => {
let (catchs, (value, labels)) = <>;
MLTuple<
(
ClosuredValueCatch*
("|" <Label*>)?
),
(ConstStart <Value> <ConstStop>),
> => {
let ((catchs, catch_labels), (value, labels)) = <>;
ClosuredValue::Uninit {
catch_values: catchs,
catch_labels: catch_labels.unwrap_or_default(),
value: value.into(),
labels,
}
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.25"
version = "0.1.26"
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 5ced7ce

Please sign in to comment.