Skip to content

Commit

Permalink
添加原始值, 来避免一些避不开的被常量替换的场景
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Aug 3, 2023
1 parent fb3b917 commit f42b831
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 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.7.4"
version = "0.7.5"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
5 changes: 5 additions & 0 deletions examples/value.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* 与mdt中数字格式基本相符
* 字符串: 由一个双引号起始, 一个双引号终止, 中间为0至若干个非双引号字符组成
* 如果其中包含换行, 将会被替换为一个字面量反斜杠接一个ASCII字符n
* 原始值: 由一对反引号包裹着的任意一个Var成员, 其中值不会被进行常量替换
* 至于常量, 后面会讲.
* 如果它不是一个常量, 那么这反引号加与不加行为应一致
*
* 还有一种特殊数字, LiteralUInt, 这种数字会被真正编译时识别使用.
* 例如switch-case中, 必须是一个无符号非负不越界整数
Expand All @@ -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"
29 changes: 27 additions & 2 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -146,6 +150,7 @@ impl Value {
result
},
Self::ResultHandle => meta.dexp_handle().clone(),
Self::ReprVar(var) => var,
}
}

Expand Down Expand Up @@ -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编译时已知"),
}
}
}
Expand Down Expand Up @@ -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",
]);
}
}
1 change: 1 addition & 0 deletions src/syntax_def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub Var: Var = {
pub Value: Value = {
Var => <>.into(),
DExp => <>.into(),
"`" <Var> "`" => Value::ReprVar(<>),
"$" => Value::ResultHandle,
}

Expand Down

0 comments on commit f42b831

Please sign in to comment.