From 01e6d7802e8e75c8c0be5b650385678a4935c034 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 24 Apr 2022 12:50:12 +0200 Subject: [PATCH] more better working debug --- src/bytecode.rs | 1 + src/compile.rs | 3 ++- src/errors.rs | 22 ++++++++++++++++++++-- src/gc.rs | 12 ++++++++++-- src/util.rs | 2 +- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/bytecode.rs b/src/bytecode.rs index 96cd4b1..e47fde2 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -142,6 +142,7 @@ impl dbg_pls::DebugPls for FnBlock<'_> { f.debug_struct("FnBlock") .field("arity", &self.arity) .field("code", &self.code.as_slice()) + .field("stack_sizes", &self.stack_sizes.as_slice()) .finish_non_exhaustive() } } diff --git a/src/compile.rs b/src/compile.rs index c62755e..a49aed0 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -473,10 +473,11 @@ impl<'bc, 'gc> Compiler<'bc, 'gc> { for param in params.iter() { self.compile_expr(param)?; + todo!("no params yet") } self.push_instr(Instr::Load(offset), StackChange::Grow, call.span); - self.push_instr(Instr::Call, StackChange::Grow, call.span); + self.push_instr(Instr::Call, StackChange::None, call.span); Ok(()) } diff --git a/src/errors.rs b/src/errors.rs index 89da85a..fcc1db6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -11,9 +11,9 @@ use std::fmt::Debug; pub use span::Span; mod span { + use std::fmt::{Debug, Formatter}; - #[derive(Debug, Default, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)] - #[cfg_attr(feature = "_debug", derive(dbg_pls::DebugPls))] + #[derive(Default, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)] pub struct Span { pub start: usize, pub end: usize, @@ -66,6 +66,24 @@ mod span { self.end - self.start } } + + impl Debug for Span { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("Span") + .field(&(self.start..self.end)) + .finish() + } + } + + #[cfg(feature = "_debug")] + impl dbg_pls::DebugPls for Span { + fn fmt(&self, f: dbg_pls::Formatter<'_>) { + f.debug_tuple_struct("Span") + // todo: wait for https://github.com/conradludgate/dbg-pls/pull/1 + .field(&format!("{:?}", (self.start..self.end))) + .finish() + } + } } #[derive(Debug, Clone, PartialEq)] diff --git a/src/gc.rs b/src/gc.rs index e81aadf..b3998ad 100644 --- a/src/gc.rs +++ b/src/gc.rs @@ -54,7 +54,6 @@ impl Copy for Gc {} /// An reference to an interned String. Hashing and Equality are O(1) and just look at the pointer address #[derive(Clone, Copy)] -#[cfg_attr(feature = "_debug", derive(dbg_pls::DebugPls))] pub struct Symbol { gc: Gc, } @@ -74,11 +73,13 @@ pub struct Object { #[derive(Debug)] #[repr(C)] +#[cfg_attr(feature = "_debug", derive(dbg_pls::DebugPls))] struct HeapObject { kind: HeapObjectKind, } #[derive(Debug)] +#[cfg_attr(feature = "_debug", derive(dbg_pls::DebugPls))] enum HeapObjectKind { String(Gc), Object(ObjectMap), @@ -213,6 +214,13 @@ impl Debug for Symbol { } } +#[cfg(feature = "_debug")] +impl dbg_pls::DebugPls for Symbol { + fn fmt(&self, f: dbg_pls::Formatter<'_>) { + DebugPls::fmt(self.as_str(), f) + } +} + impl Deref for Object { type Target = ObjectMap; @@ -226,6 +234,6 @@ impl Deref for Object { impl Debug for Object { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - self.gc.deref().fmt(f) + Debug::fmt(self.gc.deref(), f) } } diff --git a/src/util.rs b/src/util.rs index 9b2e0d4..d6767b3 100644 --- a/src/util.rs +++ b/src/util.rs @@ -11,7 +11,7 @@ use std::fmt::Display; #[cfg(feature = "_debug")] pub fn dbg(prefix: impl Display, x: impl dbg_pls::DebugPls) { - eprintln!("{prefix}{}", dbg_pls::pretty(&x)) + eprintln!("{prefix}{}", dbg_pls::color(&x)) } #[cfg(not(feature = "_debug"))]