1use crate::vocab::{Figurative, SignClause};
5use numeric::precision::{Fixed, Places};
6use zarch::hfp::{Hfp, Precision};
7use zarch::wide::U256;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum Kind {
11 Group,
12 Alnum { justified: bool },
13 National,
14 Zoned { digits: u32, scale: u32, signed: bool, sign: Option<SignClause> },
15 Packed { digits: u32, scale: u32, signed: bool },
16 Binary { digits: u32, scale: u32, signed: bool, native: bool },
17 Float(Precision),
18 NumericEdited { edit: u32, digits: u32, scale: u32, blank_when_zero: bool },
20 AlnumEdited { edit: u32 },
21 Pointer,
23 Index,
25 ObjectReference,
27 ProgramPointer,
29}
30
31impl Kind {
32 pub fn is_numeric(self) -> bool {
33 matches!(self, Kind::Zoned { .. } | Kind::Packed { .. } | Kind::Binary { .. } | Kind::Float(_) | Kind::Index)
34 }
35
36 pub fn digits_scale(self) -> Option<(u32, u32)> {
38 match self {
39 Kind::Zoned { digits, scale, .. }
40 | Kind::Packed { digits, scale, .. }
41 | Kind::Binary { digits, scale, .. }
42 | Kind::NumericEdited { digits, scale, .. } => Some((digits, scale)),
43 Kind::Index => Some((9, 0)),
44 _ => None,
45 }
46 }
47}
48
49#[derive(Clone, Copy, Debug)]
50pub struct Loc {
51 pub offset: usize,
52 pub len: usize,
53 pub kind: Kind,
54 pub item: usize,
55}
56
57#[derive(Clone, Debug)]
58pub enum Val {
59 Bytes(Vec<u8>),
60 National(Vec<u8>),
61 Num(Fixed),
62 Float(Hfp),
63 Fig(Figurative),
64 All(Vec<u8>),
65 Address(u32),
67}
68
69pub fn literal_fixed(text: &str) -> Option<Fixed> {
71 let (negative, body) = match text.as_bytes().first() {
72 Some(b'-') => (true, &text[1..]),
73 Some(b'+') => (false, &text[1..]),
74 _ => (false, text),
75 };
76 let (int, frac) = body.split_once('.').unwrap_or((body, ""));
77 let digits = format!("{int}{frac}");
78 if digits.is_empty() || digits.len() > 31 {
79 return None;
80 }
81 let magnitude = U256::from_u128(digits.parse().ok()?);
82 Some(Fixed { negative: negative && !magnitude.is_zero(), magnitude, places: Places::new(int.len().max(1) as u32, frac.len() as u32) })
83}