Skip to content

Commit

Permalink
新增可使用内建函数手动开启的match未命中日志
Browse files Browse the repository at this point in the history
- 稍微修改语法, 使do-while中可以包含if语句

  为了语法的明确性, 我们不会在do-while或if中引入LogicLine, 尽管可以
  • Loading branch information
A4-Tacks committed Mar 29, 2024
1 parent 05d961f commit 3737172
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 21 deletions.
8 changes: 4 additions & 4 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.15.4"
version = "0.15.5"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
1 change: 1 addition & 0 deletions examples/builtin_functions.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* * `EvalNum[num]`: 尝试常量计算一个值, 失败则返回`__`
* * `IsString[str]`: 返回给定量是否是一个字符串
* * `RefArg`: 返回指定下标的上层参数句柄, 一般再用Const解开
* * `MissesMatch[enable]`: 当enable非0时, 将match失配打印日志功能开启
*#

print Builtin.Type[x];
Expand Down
1 change: 1 addition & 0 deletions examples/control.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,4 @@ print 1
jump 0 lessThan c d
jump 0 lessThan a b
*#
# 在0.15.5中, if也可以写进do_while了
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.6"
version = "0.3.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/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ pub Control: LogicLine = {
ControlWithOptionalEnd,
}
ControlBody: LogicLine = {
ControlWithoutOptionalEnd,
Control,
Block,
}
ControlWithoutOptionalEnd: LogicLine = {
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.17"
version = "0.1.18"
edition = "2021"

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

let _ = parse!(parser, r#"
do if c < d {
print 1;
} elif e < f {
print 2;
} else {
print 3;
} while a < b;
"#).unwrap();

let _ = parse!(parser, r#"
do print; while a < b;
"#).unwrap_err();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tools/syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "syntax"
version = "0.2.15"
version = "0.2.16"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
7 changes: 7 additions & 0 deletions tools/syntax/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,5 +359,12 @@ pub fn build_builtins() -> Vec<BuiltinFunc> {
})
})
}

fn misses_match:MissesMatch(meta) [v:enable] {
check_type!("var" Value::Var(enable) = enable.value() => {
meta.enable_misses_match_log_info = enable != "0";
Ok("__".into())
})
}
}
}
43 changes: 31 additions & 12 deletions tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod builtins;
mod tests;

use std::{
borrow::Borrow,
borrow::{Borrow, Cow},
collections::{HashMap, HashSet},
convert::identity,
fmt::{self, Debug, Display},
Expand Down Expand Up @@ -79,6 +79,17 @@ macro_rules! do_return {
}
};
}
macro_rules! csi {
(@ignore($($i:tt)*) $($t:tt)*) => ($($t)*);
($fcode:expr $(, $code:expr)*; $($arg:tt)*) => {
format_args!(
concat!("\x1b[{}", $(csi!(@ignore($code) ";{}"), )* "m{}\x1b[0m"),
$fcode,
$($code,)*
format_args!($($arg)*),
)
};
}

#[derive(Debug, PartialEq, Clone)]
pub struct Error {
Expand Down Expand Up @@ -2500,15 +2511,16 @@ pub struct Match {
impl Compile for Match {
fn compile(self, meta: &mut CompileMeta) {
let args = self.args.into_taked_args_handle(meta);
let mut iter = self.cases.into_iter();
loop {
let Some(case) = iter.next() else { break };
let (pat, block) = case;
for (pat, block) in self.cases {
if pat.do_pattern(&args, meta) {
block.compile(meta);
break;
return;
}
}
if meta.enable_misses_match_log_info {
meta.log_info(format_args!("Misses match, [{}]", args.join(" ")));
meta.log_expand_stack();
}
}
}
impl Match {
Expand Down Expand Up @@ -3009,6 +3021,7 @@ pub struct CompileMeta {
/// 值绑定全局常量表, 只有值绑定在使用它
value_bind_global_consts: HashMap<Var, ConstData>,
last_builtin_exit_code: u8,
enable_misses_match_log_info: bool,
}
impl Debug for CompileMeta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -3033,6 +3046,7 @@ impl Debug for CompileMeta {
.field("value_binds", &self.value_binds)
.field("value_bind_global_consts", &self.value_bind_global_consts)
.field("last_builtin_exit_code", &self.last_builtin_exit_code)
.field("enable_misses_match_log_info", &self.enable_misses_match_log_info)
.field("..", &DotDot)
.finish()
}
Expand Down Expand Up @@ -3064,6 +3078,7 @@ impl CompileMeta {
value_binds: HashMap::new(),
value_bind_global_consts: HashMap::new(),
last_builtin_exit_code: 0,
enable_misses_match_log_info: false,
};
let builtin = String::from("Builtin");
for builtin_func in build_builtins() {
Expand Down Expand Up @@ -3506,20 +3521,24 @@ impl CompileMeta {
}

pub fn log_info(&mut self, s: impl std::fmt::Display) {
eprintln!("\x1b[1m[I] {}\x1b[0m", s.to_string()
.trim_end().replace('\n', "\n "))
eprintln!("{}", csi!(1; "[I] {}",
s.to_string().trim_end().replace('\n', "\n ")))
}

pub fn log_err(&mut self, s: impl std::fmt::Display) {
eprintln!("\x1b[1;91m[E] {}\x1b[0m", s.to_string()
.trim_end().replace('\n', "\n "))
eprintln!("{}", csi!(1, 91; "[E] {}",
s.to_string().trim_end().replace('\n', "\n ")))
}

pub fn debug_expand_stack(&self) -> impl Iterator<Item = String> + '_ {
pub fn debug_expand_stack(&self) -> impl Iterator<
Item = Cow<'_, str>
> + '_ {
self.const_expand_names().iter()
.zip(&self.dexp_expand_binders)
.map(|x| match x {
(name, Some(binder)) => format!("{name} ..{binder}"),
(name, Some(binder)) => {
format!("{name} ..{binder}").into()
},
(name, None) => name.into(),
})
}
Expand Down

0 comments on commit 3737172

Please sign in to comment.