Skip to content

Commit

Permalink
添加对于过长的复杂数字使用十六进制
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jan 30, 2024
1 parent ee3f9ea commit 76f908d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
6 changes: 3 additions & 3 deletions 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.14.15"
version = "0.14.16"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser-tests"
version = "0.1.9"
version = "0.1.10"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
17 changes: 17 additions & 0 deletions tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3898,6 +3898,23 @@ fn const_expr_eval_test() {
print null;
"#).unwrap()).compile().unwrap(),
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
print ($ = 999999+0;);
print ($ = 999999+1;);
print ($ = -999999 - 0;);
print ($ = -999999 - 1;);
print ($ = 1 - 1;);
"#).unwrap()).compile().unwrap(),
CompileMeta::new().compile(parse!(parser, r#"
print 999999;
print 0xF4240;
print -999999;
print 0x-F4240;
print 0;
"#).unwrap()).compile().unwrap(),
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tools/syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "syntax"
version = "0.2.10"
version = "0.2.11"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
25 changes: 21 additions & 4 deletions tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,30 @@ pub enum Value {
impl Value {
pub fn try_eval_const_num_to_var(&self, meta: &CompileMeta) -> Option<Var> {
if let Some((num, true)) = self.try_eval_const_num(meta) {
use std::num::FpCategory as FpC;
// 仅对复杂数据也就是有效运算后的数据
return match num.classify() {
std::num::FpCategory::Nan => "null".into(),
std::num::FpCategory::Infinite
FpC::Nan => "null".into(),
FpC::Infinite
if num.is_sign_negative() => (i64::MIN+1).to_string(),
std::num::FpCategory::Infinite => i64::MAX.to_string(),
_ => num.to_string(),
FpC::Infinite => i64::MAX.to_string(),
_ => loop {
let n = num.round() - num;
if let FpC::Zero | FpC::Subnormal = n.classify() {
let rng = i64::MIN as f64..=i64::MAX as f64;
let v = 999999;
if rng.contains(&num) && !(-v..=v).contains(&(num as i64)) {
let num = num.round() as i64;
break if !num.is_negative() {
format!("0x{num:X}")
} else {
let num = -num;
format!("0x-{num:X}")
}
}
}
break num.to_string()
},
}.into()
} else {
None
Expand Down

0 comments on commit 76f908d

Please sign in to comment.