diff --git a/Cargo.lock b/Cargo.lock index 5dc472e..4083a6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -288,7 +288,7 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "mindustry_logic_bang_lang" -version = "0.13.6" +version = "0.13.7" dependencies = [ "display_source", "lalrpop", diff --git a/Cargo.toml b/Cargo.toml index b0e7fc7..8f568b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.13.6" +version = "0.13.7" edition = "2021" authors = ["A4-Tacks "] diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 241caaf..b118fb1 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -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 @@ -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), @@ -340,27 +341,32 @@ impl Value { } /// 尝试解析为一个常量数字 - pub fn try_eval_const_num(&self, meta: &CompileMeta) -> Option { - fn num(s: &str) -> Option { - 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; @@ -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, } }, @@ -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, }, ); diff --git a/src/syntax/tests.rs b/src/syntax/tests.rs index 9b74941..bb8fe51 100644 --- a/src/syntax/tests.rs +++ b/src/syntax/tests.rs @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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", ]); @@ -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;