make Debug print easier to read

main
_ 2023-09-29 17:17:53 -05:00
parent 35e62027e6
commit 24d576879b
3 changed files with 32 additions and 3 deletions

View File

@ -87,6 +87,10 @@ fn main () {
match input.as_str ().trim_end () {
"c" => in_break = false,
"q" => return,
"registers" => {
dbg! (&vm.registers);
continue;
}
"s" => {
match vm.step () {
None => (),

View File

@ -48,7 +48,7 @@ pub struct Breakpoint {
#[derive (Debug)]
pub struct State <'a> {
registers: Vec <Value>,
pub registers: Vec <Value>,
// Currently only used for native function calls
top: usize,
pub stack: Vec <StackFrame>,

View File

@ -15,7 +15,7 @@ pub struct BogusClosure {
pub upvalues: Vec <Value>,
}
#[derive (Clone, Debug, PartialEq)]
#[derive (Clone, PartialEq)]
pub enum Value {
Nil,
Boolean (bool),
@ -35,6 +35,23 @@ pub enum Value {
BogusClosure (Rc <RefCell <BogusClosure>>),
}
impl fmt::Debug for Value {
fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::Nil => write! (f, "nil"),
Value::Boolean (false) => write! (f, "false"),
Value::Boolean (true) => write! (f, "true"),
Value::Float (x) => write! (f, "{:?}", x),
Value::Integer (x) => write! (f, "{}", x),
Value::RsFunc (x) => write! (f, "function: {:?}", x),
Value::String (s) => write! (f, "{}", s),
Value::Table (t) => write! (f, "{:?}", t.borrow ()),
Value::BogusClosure (x) => write! (f, "{:?}", x.borrow ()),
}
}
}
impl Default for Value {
fn default () -> Self {
Self::Nil
@ -213,12 +230,20 @@ impl Value {
}
}
#[derive (Debug, Default, Eq, PartialEq)]
#[derive (Default, Eq, PartialEq)]
pub struct Table {
array: Vec <Value>,
hash: HashMap <Value, Value>,
}
impl fmt::Debug for Table {
fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
write! (f, "Table {:#?}", self.hash)?;
Ok (())
}
}
impl Table {
fn get_inner (&self, key: &Value) -> Value {
self.hash.get (key).cloned ().unwrap_or_default ()