From 2846d1b63f84f401ab81aa51cbdb607ce188da24 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 12 Feb 2021 23:07:28 +0800 Subject: [PATCH] Change token to use FloatWrapper. --- src/ast.rs | 12 +++++++++++- src/token.rs | 9 +++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 77c001ff..8b91d7de 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -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 for FloatWrapper { } } +#[cfg(not(feature = "no_float"))] +impl FromStr for FloatWrapper { + type Err = ::Err; + + fn from_str(s: &str) -> Result { + FLOAT::from_str(s).map(Into::::into) + } +} + #[cfg(not(feature = "no_float"))] impl FloatWrapper { pub const fn new(value: FLOAT) -> Self { diff --git a/src/token.rs b/src/token.rs index b473e374..c781f2f8 100644 --- a/src/token.rs +++ b/src/token.rs @@ -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(|_| {