osiris_typed/types/
numeral.rs1use osiris_data::data::atomic::Word;
2use osiris_data::data::composite::Array;
3use osiris_process::register::floating_point::Number as Float;
4use crate::types::DataType;
5
6#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)]
7pub struct Size(u64);
8
9impl Size {
10 pub fn new(value: u64) -> Self { Self(value) }
11 pub fn from(word: Word) -> Self { Self::new(word.to_u64()) }
12 pub fn to_word(self) -> Word { Word::new(self.0) }
13}
14
15#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
16pub enum Number {
17 Integer(i64),
18 Float(f64),
19}
20
21impl Number {
22 pub fn to_word(self) -> Word {
23 match self {
24 Number::Integer(i) => Word::new(i as u64),
25 Number::Float(f) => Float::new(f).to_word(),
26 }
27 }
28 pub fn float_from_word(word: Word) -> Self {
29 Number::Float(f64::from_bits(word.to_u64()))
30 }
31 pub fn integer_from_word(word: Word) -> Self {
32 Number::Integer(word.to_u64() as i64)
33 }
34}
35
36#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
37pub enum Numeral {
38 Size(Size),
39 Number(Number),
40}
41
42impl Numeral {
43 pub fn to_word(&self) -> Word {
44 match self {
45 Numeral::Size(size) => size.to_word(),
46 Numeral::Number(number) => number.to_word(),
47 }
48 }
49}
50
51impl DataType for Numeral {
52 fn typename(&self) -> String {
53 match self {
54 Numeral::Size(_) => "size",
55 Numeral::Number(n) => match n {
56 Number::Integer(_) => "int",
57 Number::Float(_) => "float"
58 }
59 }.to_string()
60 }
61
62 fn to_array(&self) -> Array {
63 Array::from(&[match self {
64 Numeral::Size(s) => s.to_word(),
65 Numeral::Number(n) => match n {
66 Number::Integer(i) => Word::new(*i as u64),
67 Number::Float(f) => Float::new(*f).to_word()
68 }
69 }])
70 }
71
72 fn size(&self) -> usize {
73 match self {
74 Numeral::Size(_) => 1,
75 Numeral::Number(_) => 1,
76 }
77 }
78}