Skip to content

Commit

Permalink
增加了一个标志, 编译期求值时对简单值不做干扰, 进一步降低破坏
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Dec 16, 2023
1 parent 63c3583 commit 6712152
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 82 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.13.6"
version = "0.13.7"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
32 changes: 19 additions & 13 deletions src/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ pub enum Value {
}
impl TakeHandle for Value {
fn take_handle(self, meta: &mut CompileMeta) -> Var {
// 改为使用空字符串代表空返回字符串
// 如果空的返回字符串被编译将会被编译为tmp_var
if let Some(num) = self.try_eval_const_num(meta) {
if let Some((num, true)) = self.try_eval_const_num(meta) {
// 仅对复杂数据也就是有效运算后的数据
return match num.classify() {
std::num::FpCategory::Nan => "null".into(),
std::num::FpCategory::Infinite
Expand All @@ -181,6 +180,8 @@ impl TakeHandle for Value {
_ => num.to_string(),
}
}
// 改为使用空字符串代表空返回字符串
// 如果空的返回字符串被编译将会被编译为tmp_var
match self {
Self::Var(var) => var.take_handle(meta),
Self::DExp(dexp) => dexp.take_handle(meta),
Expand Down Expand Up @@ -340,27 +341,32 @@ impl Value {
}

/// 尝试解析为一个常量数字
pub fn try_eval_const_num(&self, meta: &CompileMeta) -> Option<f64> {
fn num(s: &str) -> Option<f64> {
s.as_var_type().as_number().copied()
///
/// 如果直接得到结果例如直接编写一个数字, 那么复杂标志为假
pub fn try_eval_const_num(&self, meta: &CompileMeta) -> Option<(f64, bool)> {
fn num(s: &str, complex: bool) -> Option<(f64, bool)> {
s.as_var_type().as_number().map(|&x| (x, complex))
}
match self {
Self::ReprVar(var) => num(var),
Self::ReprVar(var) => num(var, false),
Self::Var(name) => {
match meta.get_const_value(name) {
Some((_, Self::Var(var))) => num(var),
Some((_, Self::Var(var))) => num(var, false),
Some((_, Self::ReprVar(repr_var))) => {
unreachable!("被const的reprvar {:?}", repr_var)
},
Some((_, x @ Self::DExp(_))) => x.try_eval_const_num(meta),
Some(_) => None?,
None => num(name),
None => num(name, false),
}
},
Self::DExp(dexp) if dexp.len() == 1 && dexp.result.is_empty() => {
let logic_line = &dexp.first().unwrap();
match logic_line {
LogicLine::Op(op) => op.try_eval_const_num(meta),
LogicLine::Op(op) => {
op.try_eval_const_num(meta)
.map(|x| (x, true))
},
LogicLine::Other(args) => {
let Value::ReprVar(cmd) = &args[0] else {
return None;
Expand All @@ -369,7 +375,7 @@ impl Value {
"set"
if args.len() == 3
&& args[1].is_result_handle()
=> args[2].try_eval_const_num(meta),
=> (args[2].try_eval_const_num(meta)?.0, true).into(),
_ => None,
}
},
Expand Down Expand Up @@ -1571,9 +1577,9 @@ impl Op {
let OpInfo { result, arg1, arg2, .. } = self.get_info();
result.as_result_handle()?;
let (a, b) = (
arg1.try_eval_const_num(meta)?,
arg1.try_eval_const_num(meta)?.0,
match arg2 {
Some(value) => value.try_eval_const_num(meta)?,
Some(value) => value.try_eval_const_num(meta)?.0,
None => 0.0,
},
);
Expand Down
147 changes: 80 additions & 67 deletions src/syntax/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn goto_compile_test() {
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"op strictEqual __0 a b",
"jump 2 equal __0 0",
"jump 2 equal __0 false",
"end",
]);

Expand All @@ -357,7 +357,7 @@ fn goto_compile_test() {
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"op strictEqual __0 a b",
"jump 2 equal __0 0",
"jump 2 equal __0 false",
"end",
]);

Expand Down Expand Up @@ -1268,8 +1268,8 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 2 equal a 0",
"jump 3 notEqual b 0",
"jump 2 equal a false",
"jump 3 notEqual b false",
"foo",
"end",
]);
Expand All @@ -1281,9 +1281,9 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 2 notEqual a 0",
"jump 3 equal b 0",
"jump 4 notEqual c 0",
"jump 2 notEqual a false",
"jump 3 equal b false",
"jump 4 notEqual c false",
"foo",
"end",
]);
Expand All @@ -1295,10 +1295,10 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 2 notEqual a 0",
"jump 4 equal b 0",
"jump 5 notEqual c 0",
"jump 5 notEqual d 0",
"jump 2 notEqual a false",
"jump 4 equal b false",
"jump 5 notEqual c false",
"jump 5 notEqual d false",
"foo",
"end",
]);
Expand All @@ -1310,11 +1310,11 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 6 notEqual a 0",
"jump 6 notEqual b 0",
"jump 6 notEqual c 0",
"jump 6 notEqual d 0",
"jump 6 notEqual e 0",
"jump 6 notEqual a false",
"jump 6 notEqual b false",
"jump 6 notEqual c false",
"jump 6 notEqual d false",
"jump 6 notEqual e false",
"foo",
"end",
]);
Expand All @@ -1326,11 +1326,11 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 5 equal a 0",
"jump 5 equal b 0",
"jump 5 equal c 0",
"jump 5 equal d 0",
"jump 6 notEqual e 0",
"jump 5 equal a false",
"jump 5 equal b false",
"jump 5 equal c false",
"jump 5 equal d false",
"jump 6 notEqual e false",
"foo",
"end",
]);
Expand All @@ -1342,11 +1342,11 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 5 equal a 0",
"jump 5 equal b 0",
"jump 5 equal c 0",
"jump 5 equal d 0",
"jump 6 notEqual e 0",
"jump 5 equal a false",
"jump 5 equal b false",
"jump 5 equal c false",
"jump 5 equal d false",
"jump 6 notEqual e false",
"foo",
"end",
]);
Expand All @@ -1358,11 +1358,11 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 5 equal a 0",
"jump 5 equal b 0",
"jump 5 equal c 0",
"jump 5 equal d 0",
"jump 6 notEqual e 0",
"jump 5 equal a false",
"jump 5 equal b false",
"jump 5 equal c false",
"jump 5 equal d false",
"jump 6 notEqual e false",
"foo",
"end",
]);
Expand All @@ -1374,9 +1374,9 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 3 equal a 0",
"jump 3 equal a false",
"op land __0 b c",
"jump 4 notEqual __0 0",
"jump 4 notEqual __0 false",
"foo",
"end",
]);
Expand All @@ -1388,10 +1388,10 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 2 equal a 0",
"jump 5 notEqual b 0",
"jump 4 equal c 0",
"jump 5 notEqual d 0",
"jump 2 equal a false",
"jump 5 notEqual b false",
"jump 4 equal c false",
"jump 5 notEqual d false",
"foo",
"end",
]);
Expand All @@ -1403,10 +1403,10 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 2 notEqual a 0",
"jump 5 notEqual b 0",
"jump 4 equal c 0",
"jump 5 notEqual d 0",
"jump 2 notEqual a false",
"jump 5 notEqual b false",
"jump 4 equal c false",
"jump 5 notEqual d false",
"foo",
"end",
]);
Expand All @@ -1418,10 +1418,10 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 2 equal a 0",
"jump 5 notEqual b 0",
"jump 5 equal c 0",
"jump 5 equal d 0",
"jump 2 equal a false",
"jump 5 notEqual b false",
"jump 5 equal c false",
"jump 5 equal d false",
"foo",
"end",
]);
Expand All @@ -1433,11 +1433,11 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 3 equal a 0",
"jump 3 equal b 0",
"jump 6 notEqual c 0",
"jump 5 equal d 0",
"jump 6 notEqual e 0",
"jump 3 equal a false",
"jump 3 equal b false",
"jump 6 notEqual c false",
"jump 5 equal d false",
"jump 6 notEqual e false",
"foo",
"end",
]);
Expand All @@ -1449,11 +1449,11 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 2 equal a 0",
"jump 6 notEqual b 0",
"jump 6 notEqual c 0",
"jump 5 equal d 0",
"jump 6 notEqual e 0",
"jump 2 equal a false",
"jump 6 notEqual b false",
"jump 6 notEqual c false",
"jump 5 equal d false",
"jump 6 notEqual e false",
"foo",
"end",
]);
Expand All @@ -1465,11 +1465,11 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 2 equal a 0",
"jump 6 notEqual b 0",
"jump 6 notEqual c 0",
"jump 5 equal d 0",
"jump 6 notEqual e 0",
"jump 2 equal a false",
"jump 6 notEqual b false",
"jump 6 notEqual c false",
"jump 5 equal d false",
"jump 6 notEqual e false",
"foo",
"end",
]);
Expand All @@ -1481,11 +1481,11 @@ fn cmptree_test() {
end;
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"jump 3 equal a 0",
"jump 6 notEqual b 0",
"jump 6 notEqual c 0",
"jump 5 equal d 0",
"jump 6 notEqual e 0",
"jump 3 equal a false",
"jump 6 notEqual b false",
"jump 6 notEqual c false",
"jump 5 equal d false",
"jump 6 notEqual e false",
"foo",
"end",
]);
Expand All @@ -1498,9 +1498,9 @@ fn cmptree_test() {
"#).unwrap()).compile().unwrap();
assert_eq!(logic_lines, vec![
"op add __0 a 2",
"jump 4 equal __0 0",
"jump 4 equal __0 false",
"op add __1 b 2",
"jump 5 notEqual __1 0",
"jump 5 notEqual __1 false",
"foo",
"end",
]);
Expand Down Expand Up @@ -3724,6 +3724,19 @@ fn const_expr_eval_test() {
assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
print 1.00;
print `1.00`;
print (1.00:);
"#).unwrap()).compile().unwrap(),
CompileMeta::new().compile(parse!(parser, r#"
print 1.00;
print 1.00;
print 1.00;
"#).unwrap()).compile().unwrap(),
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
print ($ = 1.00;);
"#).unwrap()).compile().unwrap(),
CompileMeta::new().compile(parse!(parser, r#"
print 1;
Expand Down

0 comments on commit 6712152

Please sign in to comment.