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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::ast::numberic::{ Float, Numberic, };

const BINARY: u32 = 2;
const OCTAL: u32  = 8;
const HEX: u32    = 16;


#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum NumbericErrorKind {
    Overflow,
    InvalidDigit,
    // invalid float literal
    InvalidFloat,
}


#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub struct ParseNumbericError {
    kind: NumbericErrorKind,
    offset: usize,
}

impl ParseNumbericError {
    pub fn new(kind: NumbericErrorKind, offset: usize) -> Self {
        Self { kind, offset }
    }

    pub fn kind(&self) -> &NumbericErrorKind {
        &self.kind
    }

    pub fn offset(&self) -> usize {
        self.offset
    }
}

#[inline]
pub fn from_chars_radix(digits: &[char], radix: u32, pre_offset: usize) -> Result<u64, ParseNumbericError> {
    assert_eq!(radix == BINARY || radix == OCTAL || radix == HEX, true);

    let digits_len = digits.len() as u32;

    if digits_len == 0 {
        return Err(ParseNumbericError::new(NumbericErrorKind::InvalidDigit, pre_offset));
    }

    let mut offset: u32 = 0;
    let mut acc: u64 = 0;

    while offset < digits_len {
        let digit: u32 = digits[offset as usize]
                            .to_digit(radix)
                            .ok_or(ParseNumbericError::new(NumbericErrorKind::InvalidDigit, pre_offset + offset as usize))?;

        let idx = digits_len - 1 - offset;
        match radix.checked_pow(idx) {
            Some(n) => match digit.checked_mul(n) {
                Some(n) => {
                    // acc += digit * radix.pow(idx);
                    acc += n as u64;
                },
                None => {
                    return Err(ParseNumbericError::new(NumbericErrorKind::Overflow, pre_offset + offset as usize));
                },
            },
            None => {
                return Err(ParseNumbericError::new(NumbericErrorKind::Overflow, pre_offset + offset as usize));
            },
        }
        
        offset += 1;
    }

    Ok(acc)
}



#[inline]
pub fn parse_numberic(input: &[char]) -> Result<Numberic, ParseNumbericError> {
    let input_len = input.len();
    let mut idx = 0usize;

    if idx >= input_len {
        return Err(ParseNumbericError::new(NumbericErrorKind::InvalidDigit, idx));
    }

    let c = input[idx];
    match c {
        '0' => {
            idx += 1;
            match input.get(idx) {
                Some('b') | Some('B') => {
                    idx += 1;
                    
                    if idx >= input_len {
                        return Err(ParseNumbericError::new(NumbericErrorKind::InvalidDigit, idx));
                    }

                    let n = from_chars_radix(&input[idx..], BINARY, idx)?;

                    return Ok(n.into())
                },
                Some('o') | Some('O') => {
                    idx += 1;
                    
                    if idx >= input_len {
                        return Err(ParseNumbericError::new(NumbericErrorKind::InvalidDigit, idx));
                    }

                    let n = from_chars_radix(&input[idx..], OCTAL, idx)?;

                    return Ok(n.into())
                },
                Some('x') | Some('X') => {
                    idx += 1;
                    
                    if idx >= input_len {
                        return Err(ParseNumbericError::new(NumbericErrorKind::InvalidDigit, idx));
                    }

                    let n = from_chars_radix(&input[idx..], HEX, idx)?;

                    return Ok(n.into())
                },
                Some('.') => {
                    let s = input.iter().collect::<String>();
                    match s.parse::<f64>() {
                        Ok(float) => {
                            return Ok(Numberic::F64(float.into()));
                        },
                        Err(_) => {
                            return Err(ParseNumbericError::new(NumbericErrorKind::InvalidFloat, 0));
                        }
                    }
                },
                Some('e') | Some('E') => {
                    let s = input.iter().collect::<String>();
                    match s.parse::<f64>() {
                        Ok(float) => {
                            return Ok(Numberic::F64(float.into()));
                        },
                        Err(_) => {
                            return Err(ParseNumbericError::new(NumbericErrorKind::InvalidFloat, 0));
                        }
                    }
                },
                Some(c) => {
                    // c.is_es_decimal_digit()
                    // warn!("please don't add zero on numeric's head.");
                    return Err(ParseNumbericError::new(NumbericErrorKind::InvalidDigit, idx));
                },
                None => {
                    return Ok(Numberic::ZERO);
                }
            }
        },
        '1' ... '9' => {
            let s = input.iter().collect::<String>();
            match s.parse::<f64>() {
                Ok(float) => {
                    let fract = float.fract();
                    // TODO: 计算 EPSILON ? std::f64::EPSILON
                    if fract < 0.0 || fract > 0.0 {
                        return Ok(Numberic::F64(float.into()));
                    } else {
                        // int
                        let trunc = float.trunc() as i64;
                        return Ok(Numberic::I64(trunc));
                    }
                },
                Err(_) => {
                    return Err(ParseNumbericError::new(NumbericErrorKind::InvalidFloat, 0));
                }
            }
        },
        '-' => {
            idx += 1;
            return parse_numberic(&input[idx..]).map(|n| {
                match n {
                    Numberic::I64(int) => Numberic::I64(-int),
                    Numberic::F64(float) => Numberic::F64( Float(-(float.0)) ),
                }
            });
        },
        '+' => {
            idx += 1;
            return parse_numberic(&input[idx..]);
        },
        _ => {
            return Err(ParseNumbericError::new(NumbericErrorKind::InvalidDigit, idx));
        }
    }
}


#[test]
fn test_from_chars_radix() {
    let f = |s: &str| -> Vec<char> {
        s.chars().collect::<Vec<char>>()
    };

    assert_eq!(from_chars_radix(&f("111"), 2, 0), Ok(7));
    assert_eq!(from_chars_radix(&f("1000"), 2, 0), Ok(8));

    assert_eq!(from_chars_radix(&f("123"), 8, 0), Ok(83));
    assert_eq!(from_chars_radix(&f("255"), 8, 0), Ok(173));

    assert_eq!(from_chars_radix(&f("69"), 16, 0), Ok(105));
    assert_eq!(from_chars_radix(&f("ff"), 16, 0), Ok(255));
}

#[test]
fn test_parse_float() {
    let f = |s: &str| -> Vec<char> {
        s.chars().collect::<Vec<char>>()
    };
    
    assert_eq!(parse_numberic(&f("0.234235834589")), Ok(Numberic::F64( 0.234235834589f64.into() )));
    assert_eq!(parse_numberic(&f("0.234235834589e1")), Ok(Numberic::F64( 0.234235834589e1f64.into() )));
}

#[test]
fn test_parse_int() {
    let f = |s: &str| -> Vec<char> {
        s.chars().collect::<Vec<char>>()
    };

    assert_eq!(parse_numberic(&f("0")), Ok(Numberic::I64(0)));
    assert_eq!(parse_numberic(&f("023424")).is_err(), true);
    assert_eq!(parse_numberic(&f("12342342")), Ok(Numberic::I64(12342342)));

    assert_eq!(parse_numberic(&f("0b111111")), Ok(Numberic::I64(63)));
    assert_eq!(parse_numberic(&f("0o124234126")), Ok(Numberic::I64(22100054)));
    assert_eq!(parse_numberic(&f("0x69")), Ok(Numberic::I64(105)));
}