From f42b831a1171296f9f8c0e6f9f0cb3afe08d7ba4 Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Thu, 3 Aug 2023 19:48:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8E=9F=E5=A7=8B=E5=80=BC,?= =?UTF-8?q?=20=E6=9D=A5=E9=81=BF=E5=85=8D=E4=B8=80=E4=BA=9B=E9=81=BF?= =?UTF-8?q?=E4=B8=8D=E5=BC=80=E7=9A=84=E8=A2=AB=E5=B8=B8=E9=87=8F=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E7=9A=84=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/value.mdtlbl | 5 +++++ src/syntax.rs | 29 +++++++++++++++++++++++++++-- src/syntax_def.lalrpop | 1 + 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9fb703..951f443 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -261,7 +261,7 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "mindustry_logic_bang_lang" -version = "0.7.4" +version = "0.7.5" dependencies = [ "lalrpop", "lalrpop-util", diff --git a/Cargo.toml b/Cargo.toml index ea67e6c..f05aefd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.7.4" +version = "0.7.5" edition = "2021" authors = ["A4-Tacks "] diff --git a/examples/value.mdtlbl b/examples/value.mdtlbl index 0ae52ab..61114c6 100644 --- a/examples/value.mdtlbl +++ b/examples/value.mdtlbl @@ -20,6 +20,9 @@ * 与mdt中数字格式基本相符 * 字符串: 由一个双引号起始, 一个双引号终止, 中间为0至若干个非双引号字符组成 * 如果其中包含换行, 将会被替换为一个字面量反斜杠接一个ASCII字符n + * 原始值: 由一对反引号包裹着的任意一个Var成员, 其中值不会被进行常量替换 + * 至于常量, 后面会讲. + * 如果它不是一个常量, 那么这反引号加与不加行为应一致 * * 还有一种特殊数字, LiteralUInt, 这种数字会被真正编译时识别使用. * 例如switch-case中, 必须是一个无符号非负不越界整数 @@ -42,3 +45,5 @@ set n_int -123_456; set bin 0b1001_0010; set n_bin 0b-1001_0010; set x 'abc"def'; # 其中的双引号会被替换为单引号 +set y `y`; # 这里`y`会被编译为y +set z `"z"`; # 这里`"z"`会被编译为"z" diff --git a/src/syntax.rs b/src/syntax.rs index 2d98e2e..92e20a0 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -84,8 +84,12 @@ pub const COUNTER: &str = "@counter"; #[derive(Debug, PartialEq, Clone)] pub enum Value { + /// 一个普通值 Var(Var), + /// DExp DExp(DExp), + /// 不可被常量替换的普通值 + ReprVar(Var), /// 编译时被替换为当前DExp返回句柄 ResultHandle, } @@ -146,6 +150,7 @@ impl Value { result }, Self::ResultHandle => meta.dexp_handle().clone(), + Self::ReprVar(var) => var, } } @@ -194,9 +199,10 @@ impl Deref for Value { fn deref(&self) -> &Self::Target { match self { - Self::Var(ref s) => &s, + Self::Var(ref s) | Self::ReprVar(ref s) => s, Self::DExp(DExp { result, .. }) => &result, - Self::ResultHandle => panic!("未进行AST编译, 而DExp的返回句柄是进行AST编译时已知"), + Self::ResultHandle => + panic!("未进行AST编译, 而DExp的返回句柄是进行AST编译时已知"), } } } @@ -2614,4 +2620,23 @@ mod tests { "print m", ]); } + + #[test] + fn repr_var_test() { + let parser = ExpandParser::new(); + + let logic_lines = CompileMeta::new().compile(parse!(parser, r#" + print a; + print `a`; + const a = b; + print a; + print `a`; + "#).unwrap()).compile().unwrap(); + assert_eq!(logic_lines, vec![ + "print a", + "print a", + "print b", + "print a", + ]); + } } diff --git a/src/syntax_def.lalrpop b/src/syntax_def.lalrpop index 1670b49..1eb8797 100644 --- a/src/syntax_def.lalrpop +++ b/src/syntax_def.lalrpop @@ -75,6 +75,7 @@ pub Var: Var = { pub Value: Value = { Var => <>.into(), DExp => <>.into(), + "`" "`" => Value::ReprVar(<>), "$" => Value::ResultHandle, }