Change token to use FloatWrapper.

This commit is contained in:
Stephen Chung 2021-02-12 23:07:28 +08:00
parent aea5ec50c9
commit 2846d1b63f
2 changed files with 16 additions and 5 deletions

View File

@ -10,6 +10,7 @@ use crate::stdlib::{
hash::Hash,
num::{NonZeroU64, NonZeroUsize},
ops::{Add, AddAssign},
str::FromStr,
string::String,
vec,
vec::Vec,
@ -1126,7 +1127,7 @@ pub struct FnCallExpr {
/// A type that wraps a [`FLOAT`] and implements [`Hash`].
#[cfg(not(feature = "no_float"))]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, PartialOrd)]
pub struct FloatWrapper(FLOAT);
#[cfg(not(feature = "no_float"))]
@ -1195,6 +1196,15 @@ impl From<FLOAT> for FloatWrapper {
}
}
#[cfg(not(feature = "no_float"))]
impl FromStr for FloatWrapper {
type Err = <FLOAT as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
FLOAT::from_str(s).map(Into::<Self>::into)
}
}
#[cfg(not(feature = "no_float"))]
impl FloatWrapper {
pub const fn new(value: FLOAT) -> Self {

View File

@ -15,7 +15,7 @@ use crate::stdlib::{
use crate::{Engine, LexError, StaticVec, INT};
#[cfg(not(feature = "no_float"))]
use crate::FLOAT;
use crate::ast::FloatWrapper;
type LERR = LexError;
@ -153,7 +153,7 @@ impl fmt::Debug for Position {
/// # Volatile Data Structure
///
/// This type is volatile and may change.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Hash)]
pub enum Token {
/// An `INT` constant.
IntegerConstant(INT),
@ -161,7 +161,7 @@ pub enum Token {
///
/// Reserved under the `no_float` feature.
#[cfg(not(feature = "no_float"))]
FloatConstant(FLOAT),
FloatConstant(FloatWrapper),
/// An identifier.
Identifier(String),
/// A character constant.
@ -1180,7 +1180,8 @@ fn get_next_token_inner(
// If integer parsing is unnecessary, try float instead
#[cfg(not(feature = "no_float"))]
let num = num.or_else(|_| FLOAT::from_str(&out).map(Token::FloatConstant));
let num =
num.or_else(|_| FloatWrapper::from_str(&out).map(Token::FloatConstant));
return Some((
num.unwrap_or_else(|_| {