1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::kval_enum::KVal;
use std::f64;
pub fn comp_double(num : f64) -> KVal {
if num.to_bits() == (-0.0f64).to_bits(){ return KVal::Double(num); }
let int = num.trunc();
if int != num{ return KVal::Double(num) }
if int as i64 as f64 == int {
return comp_int(int as i64);
} else if num as f32 as f64 == num {
return KVal::Float(num as f32);
} else {
return KVal::Double(num);
}
}
pub fn comp_float(num : f32) -> KVal {
if num.to_bits() == (-0.0f32).to_bits(){ return KVal::Float(num) }
let int = num.trunc();
if int != num{ return KVal::Float(num) }
if int as i32 as f32 == int {
return comp_int(int as i64);
} else{
return KVal::Float(num);
}
}
pub fn comp_int(int : i64) -> KVal {
if int == 0{ return KVal::Bit(false); }
if int == 1{ return KVal::Bit(true); }
if int as i8 as i64 == int{ return KVal::Byte(int as i8); }
return KVal::Int(int);
}
pub fn comp_str(s : String) -> KVal {
let len = s.len();
if len == 0{ return KVal::Bit(false) }
if len < 16{ return KVal::Str16(s); }
if len < 256{ return KVal::Str256(s); }
return KVal::BigStr(s);
}