Skip to content

Commit

Permalink
DExp返回句柄添加可被常量替换
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jul 27, 2023
1 parent 25da64c commit 16290f0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 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.4.1"
version = "0.4.2"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
42 changes: 42 additions & 0 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ impl Value {
Self::DExp(DExp { mut result, lines }) => {
if result.is_empty() {
result = meta.get_tmp_var(); /* init tmp_var */
} else if let Some((_, _, value)) = meta.get_const_value(&result) {
// 对返回句柄使用常量值的处理
if let Some(dexp) = value.as_dexp() {
err!(
concat!(
"尝试在`DExp`的返回句柄处使用值为`DExp`的const, ",
"此处仅允许使用`Var`\n",
"DExp: {:?}\n",
"名称: {:?}",
),
dexp,
result
);
exit(5);
}
assert!(value.is_var());
result = value.as_var().unwrap().clone()
}
assert!(! result.is_empty());
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -1684,4 +1701,29 @@ mod tests {
]
);
}

#[test]
fn dexp_result_handle_use_const_test() {
let parser = ExpandParser::new();

let ast = parse!(parser, r#"
{
print (R: $ = 2;);
const R = x;
print (R: $ = 2;);
}
print (R: $ = 2;);
"#).unwrap();
let meta = CompileMeta::new();
let mut tag_codes = meta.compile(ast);
let logic_lines = tag_codes.compile().unwrap();
assert_eq!(logic_lines, vec![
"set R 2",
"print R",
"set x 2",
"print x",
"set R 2",
"print R",
]);
}
}

0 comments on commit 16290f0

Please sign in to comment.