add pretty print

This commit is contained in:
nora 2022-01-04 23:09:18 +01:00
parent 1a77e710d5
commit 76af14a9f4
11 changed files with 114 additions and 5 deletions

View file

@ -18,6 +18,7 @@
use crate::errors::Span;
use crate::vm::Value;
use bumpalo::collections::Vec;
use debug2::Formatter;
/// This struct contains all data for a function.
#[derive(Debug)]
@ -34,8 +35,21 @@ pub struct FnBlock<'bc> {
pub arity: u8,
}
#[cfg(feature = "pretty")]
impl debug2::Debug for FnBlock<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FnBlock")
.field("code", &self.code.as_slice())
.field("stack_sizes", &self.stack_sizes.as_slice())
.field("spans", &self.spans.as_slice())
.field("arity", &self.arity)
.finish()
}
}
/// A bytecode instruction. For more details on the structure of the bytecode, read the module level docs [`bytecode`](`self`)
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
pub enum Instr {
/// An operation that does nothing.
Nop,

View file

@ -62,7 +62,7 @@ pub fn compile<'ast, 'bc, 'gc>(
ast: &'ast Program,
bytecode_bump: &'bc Bump,
rt: &'gc mut RtAlloc,
) -> Result<Vec<'bc, FnBlock<'bc>>, CompilerError> {
) -> Result<&'bc [FnBlock<'bc>], CompilerError> {
let mut compiler = Compiler {
blocks: Vec::new_in(bytecode_bump),
current_block: 0,
@ -73,7 +73,7 @@ pub fn compile<'ast, 'bc, 'gc>(
compiler.compile(ast)?;
Ok(compiler.blocks)
Ok(compiler.blocks.into_bump_slice())
}
impl<'bc, 'gc> Compiler<'bc, 'gc> {

View file

@ -13,6 +13,7 @@ pub use span::Span;
mod span {
#[derive(Debug, Default, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
pub struct Span {
pub start: usize,
pub end: usize,

View file

@ -30,6 +30,21 @@ impl<T: ?Sized> Deref for Gc<T> {
}
}
#[cfg(feature = "pretty")]
impl<T: debug2::Debug> debug2::Debug for Gc<T> {
fn fmt(&self, f: &mut debug2::Formatter<'_>) -> std::fmt::Result {
T::fmt(&*self, f)
}
}
#[cfg(feature = "pretty")]
impl debug2::Debug for Gc<str> {
fn fmt(&self, f: &mut debug2::Formatter<'_>) -> std::fmt::Result {
let str = self.deref();
debug2::Debug::fmt(&str, f)
}
}
impl<T: Debug + ?Sized> Debug for Gc<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
T::fmt(self, f)
@ -46,6 +61,7 @@ impl<T: ?Sized> Copy for Gc<T> {}
/// 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 = "pretty", derive(debug2::Debug))]
pub struct Symbol {
gc: Gc<str>,
}
@ -58,17 +74,20 @@ type ObjectMap = HashMap<Symbol, Value>;
/// ```
/// This is inside the local x now.
#[derive(Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
pub struct Object {
gc: Gc<HeapObject>,
}
#[derive(Debug)]
#[repr(C)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
struct HeapObject {
kind: HeapObjectKind,
}
#[derive(Debug)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
enum HeapObjectKind {
String(Gc<str>),
Object(ObjectMap),

View file

@ -63,7 +63,14 @@ fn process_ast(program: &str, ast: &Program, mut runtime: RtAlloc, cfg: &mut Con
match bytecode {
Ok(code) => {
if cfg.debug {
println!("Bytecode:\n{:#?}\n", code);
#[cfg(feature = "pretty")]
{
println!("Bytecode:\n{}\n", debug2::pprint(code));
}
#[cfg(not(feature = "pretty"))]
{
println!("Bytecode:\n{:#?}\n", code);
}
}
let result = vm::execute(&code, runtime, cfg.stdout);

View file

@ -24,6 +24,7 @@ pub fn execute<'bc>(
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
pub enum Value {
Null,
Bool(bool),
@ -63,6 +64,8 @@ impl<'bc> Vm<'bc, '_> {
None => return Ok(()),
}
self.pc += 1;
// debug stack size assertion
todo!()
}
}