Refine debug print-out.
This commit is contained in:
152
src/ast.rs
152
src/ast.rs
@@ -25,6 +25,9 @@ use crate::{
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
use crate::{stdlib::str::FromStr, FLOAT};
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
use num_traits::Float;
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
use crate::Array;
|
||||
|
||||
@@ -776,7 +779,7 @@ pub struct Ident {
|
||||
impl fmt::Debug for Ident {
|
||||
#[inline(always)]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Ident({:?} @ {:?})", self.name, self.pos)
|
||||
write!(f, "{:?} @ {:?}", self.name, self.pos)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1440,13 +1443,13 @@ impl FnCallExpr {
|
||||
}
|
||||
}
|
||||
|
||||
/// A type that wraps a [`FLOAT`] and implements [`Hash`].
|
||||
/// A type that wraps a floating-point number and implements [`Hash`].
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[derive(Clone, Copy, PartialEq, PartialOrd)]
|
||||
pub struct FloatWrapper(FLOAT);
|
||||
pub struct FloatWrapper<F>(F);
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl Hash for FloatWrapper {
|
||||
impl Hash for FloatWrapper<FLOAT> {
|
||||
#[inline(always)]
|
||||
fn hash<H: crate::stdlib::hash::Hasher>(&self, state: &mut H) {
|
||||
self.0.to_ne_bytes().hash(state);
|
||||
@@ -1454,24 +1457,24 @@ impl Hash for FloatWrapper {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl AsRef<FLOAT> for FloatWrapper {
|
||||
impl<F: Float> AsRef<F> for FloatWrapper<F> {
|
||||
#[inline(always)]
|
||||
fn as_ref(&self) -> &FLOAT {
|
||||
fn as_ref(&self) -> &F {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl AsMut<FLOAT> for FloatWrapper {
|
||||
impl<F: Float> AsMut<F> for FloatWrapper<F> {
|
||||
#[inline(always)]
|
||||
fn as_mut(&mut self) -> &mut FLOAT {
|
||||
fn as_mut(&mut self) -> &mut F {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl crate::stdlib::ops::Deref for FloatWrapper {
|
||||
type Target = FLOAT;
|
||||
impl<F: Float> crate::stdlib::ops::Deref for FloatWrapper<F> {
|
||||
type Target = F;
|
||||
|
||||
#[inline(always)]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@@ -1480,7 +1483,7 @@ impl crate::stdlib::ops::Deref for FloatWrapper {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl crate::stdlib::ops::DerefMut for FloatWrapper {
|
||||
impl<F: Float> crate::stdlib::ops::DerefMut for FloatWrapper<F> {
|
||||
#[inline(always)]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
@@ -1488,52 +1491,67 @@ impl crate::stdlib::ops::DerefMut for FloatWrapper {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl fmt::Debug for FloatWrapper {
|
||||
impl<F: Float + fmt::Display> fmt::Debug for FloatWrapper<F> {
|
||||
#[inline(always)]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
fmt::Display::fmt(&self.0, f)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl fmt::Display for FloatWrapper {
|
||||
impl<F: Float + fmt::Display + fmt::LowerExp + From<f32>> fmt::Display for FloatWrapper<F> {
|
||||
#[inline(always)]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
#[cfg(feature = "no_std")]
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
use num_traits::Float;
|
||||
|
||||
let abs = self.0.abs();
|
||||
if abs > 10000000000000.0 || abs < 0.0000000000001 {
|
||||
if abs > Self::MAX_NATURAL_FLOAT_FOR_DISPLAY.into()
|
||||
|| abs < Self::MIN_NATURAL_FLOAT_FOR_DISPLAY.into()
|
||||
{
|
||||
write!(f, "{:e}", self.0)
|
||||
} else {
|
||||
self.0.fmt(f)
|
||||
fmt::Display::fmt(&self.0, f)?;
|
||||
if abs.fract().is_zero() {
|
||||
f.write_str(".0")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl From<FLOAT> for FloatWrapper {
|
||||
impl<F: Float> From<F> for FloatWrapper<F> {
|
||||
#[inline(always)]
|
||||
fn from(value: FLOAT) -> Self {
|
||||
fn from(value: F) -> Self {
|
||||
Self::new(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl FromStr for FloatWrapper {
|
||||
type Err = <FLOAT as FromStr>::Err;
|
||||
impl<F: Float + FromStr> FromStr for FloatWrapper<F> {
|
||||
type Err = <F as FromStr>::Err;
|
||||
|
||||
#[inline(always)]
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
FLOAT::from_str(s).map(Into::<Self>::into)
|
||||
F::from_str(s).map(Into::<Self>::into)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl FloatWrapper {
|
||||
impl<F: Float> FloatWrapper<F> {
|
||||
/// Maximum floating-point number for natural display before switching to scientific notation.
|
||||
pub const MAX_NATURAL_FLOAT_FOR_DISPLAY: f32 = 10000000000000.0;
|
||||
|
||||
/// Minimum floating-point number for natural display before switching to scientific notation.
|
||||
pub const MIN_NATURAL_FLOAT_FOR_DISPLAY: f32 = 0.0000000000001;
|
||||
|
||||
#[inline(always)]
|
||||
pub const fn new(value: FLOAT) -> Self {
|
||||
pub fn new(value: F) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl FloatWrapper<FLOAT> {
|
||||
#[inline(always)]
|
||||
pub(crate) const fn const_new(value: FLOAT) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
@@ -1544,7 +1562,7 @@ impl FloatWrapper {
|
||||
/// # Volatile Data Structure
|
||||
///
|
||||
/// This type is volatile and may change.
|
||||
#[derive(Debug, Clone, Hash)]
|
||||
#[derive(Clone, Hash)]
|
||||
pub enum Expr {
|
||||
/// Dynamic constant.
|
||||
/// Used to hold either an [`Array`] or [`Map`][crate::Map] literal for quick cloning.
|
||||
@@ -1556,7 +1574,7 @@ pub enum Expr {
|
||||
IntegerConstant(INT, Position),
|
||||
/// Floating-point constant.
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
FloatConstant(FloatWrapper, Position),
|
||||
FloatConstant(FloatWrapper<FLOAT>, Position),
|
||||
/// Character constant.
|
||||
CharConstant(char, Position),
|
||||
/// [String][ImmutableString] constant.
|
||||
@@ -1613,6 +1631,80 @@ impl Default for Expr {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Expr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::DynamicConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
|
||||
Self::BoolConstant(value, pos) => write!(f, "{} @ {:?}", value, pos),
|
||||
Self::IntegerConstant(value, pos) => write!(f, "{} @ {:?}", value, pos),
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
Self::FloatConstant(value, pos) => write!(f, "{} @ {:?}", value, pos),
|
||||
Self::CharConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
|
||||
Self::StringConstant(value, pos) => write!(f, "{:?} @ {:?}", value, pos),
|
||||
Self::FnPointer(value, pos) => write!(f, "Fn({:?}) @ {:?}", value, pos),
|
||||
Self::Unit(pos) => write!(f, "() @ {:?}", pos),
|
||||
Self::InterpolatedString(x) => {
|
||||
f.write_str("InterpolatedString")?;
|
||||
f.debug_list().entries(x.iter()).finish()
|
||||
}
|
||||
Self::Array(x, pos) => {
|
||||
f.write_str("Array")?;
|
||||
f.debug_list().entries(x.iter()).finish()?;
|
||||
write!(f, " @ {:?}", pos)
|
||||
}
|
||||
Self::Map(x, pos) => {
|
||||
f.write_str("Map")?;
|
||||
f.debug_map()
|
||||
.entries(x.0.iter().map(|(k, v)| (k, v)))
|
||||
.finish()?;
|
||||
write!(f, " @ {:?}", pos)
|
||||
}
|
||||
Self::Variable(i, pos, x) => {
|
||||
f.write_str("Variable(")?;
|
||||
match x.1 {
|
||||
Some((_, ref namespace)) => write!(f, "{}::", namespace)?,
|
||||
_ => (),
|
||||
}
|
||||
write!(f, "{}", x.2)?;
|
||||
match i.map_or_else(|| x.0, |n| NonZeroUsize::new(n.get() as usize)) {
|
||||
Some(n) => write!(f, " [{}]", n)?,
|
||||
_ => (),
|
||||
}
|
||||
write!(f, ") @ {:?}", pos)
|
||||
}
|
||||
Self::Property(x) => write!(f, "Property({:?} @ {:?})", x.2.name, x.2.pos),
|
||||
Self::Stmt(x) => {
|
||||
f.write_str("Stmt")?;
|
||||
f.debug_list().entries(x.statements.iter()).finish()?;
|
||||
write!(f, " @ {:?}", x.pos)
|
||||
}
|
||||
Self::FnCall(x, pos) => {
|
||||
f.debug_tuple("FnCall").field(x).finish()?;
|
||||
write!(f, " @ {:?}", pos)
|
||||
}
|
||||
Self::Dot(x, pos) | Self::Index(x, pos) | Self::And(x, pos) | Self::Or(x, pos) => {
|
||||
let op = match self {
|
||||
Self::Dot(_, _) => "Dot",
|
||||
Self::Index(_, _) => "Index",
|
||||
Self::And(_, _) => "And",
|
||||
Self::Or(_, _) => "Or",
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
f.debug_struct(op)
|
||||
.field("lhs", &x.lhs)
|
||||
.field("rhs", &x.rhs)
|
||||
.finish()?;
|
||||
write!(f, " @ {:?}", pos)
|
||||
}
|
||||
Self::Custom(x, pos) => {
|
||||
f.debug_tuple("Custom").field(x).finish()?;
|
||||
write!(f, " @ {:?}", pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Expr {
|
||||
/// Get the [`Dynamic`] value of a constant expression.
|
||||
///
|
||||
@@ -1914,7 +2006,7 @@ mod tests {
|
||||
assert_eq!(size_of::<Option<ast::Expr>>(), 16);
|
||||
assert_eq!(size_of::<ast::Stmt>(), 32);
|
||||
assert_eq!(size_of::<Option<ast::Stmt>>(), 32);
|
||||
assert_eq!(size_of::<FnPtr>(), 80);
|
||||
assert_eq!(size_of::<FnPtr>(), 96);
|
||||
assert_eq!(size_of::<Scope>(), 288);
|
||||
assert_eq!(size_of::<LexError>(), 56);
|
||||
assert_eq!(size_of::<ParseError>(), 16);
|
||||
|
Reference in New Issue
Block a user