1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

feat: null type and register debug info

This commit is contained in:
Natapat Samutpong 2022-01-27 13:41:15 +07:00
parent 04b2c27930
commit 5fb47a32ca
2 changed files with 22 additions and 2 deletions

View file

@ -5,6 +5,7 @@ use crate::vm::vm::Error::{self, InvalidAriphmeticOperation};
/// Literal types for the assembler.
#[derive(Clone, Debug)]
pub enum Type {
Null,
Int(i64),
Float(f64),
Boolean(bool),
@ -14,6 +15,7 @@ pub enum Type {
impl Type {
pub fn as_bool(&self) -> bool {
match self {
Type::Null => false,
Type::Boolean(b) => *b,
Type::Int(i) => *i != 0,
Type::Float(f) => *f != 0.0,
@ -21,8 +23,16 @@ impl Type {
}
}
pub fn is_null(&self) -> bool {
match self {
Type::Null => true,
_ => false,
}
}
pub fn trim(&self) -> Type {
match self {
Type::Null => Type::Null,
Type::Int(i) => Type::Int(*i),
Type::Float(f) => Type::Float(*f),
Type::Boolean(b) => Type::Boolean(*b),
@ -32,6 +42,7 @@ impl Type {
pub fn fmt(&self) -> String {
match self {
Type::Null => "null".to_string(),
Type::Int(i) => i.to_string(),
Type::Float(f) => f.to_string(),
Type::Boolean(b) => match b {
@ -103,6 +114,7 @@ impl Div for Type {
impl Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Type::Null => write!(f, ":NULL"),
Type::Int(i) => write!(f, ":{}", i),
Type::Float(fl) => write!(f, ":{}", fl),
Type::Boolean(b) => write!(f, ":{}", b),

View file

@ -31,7 +31,7 @@ impl VM {
pub fn new() -> Self {
VM {
instr_pointer: 0,
registers: vec![Type::Int(0); 1024],
registers: vec![Type::Null; 1024],
stack: Vec::new(),
}
}
@ -42,7 +42,7 @@ impl VM {
if self.instr_pointer - 1 == instrs.len() as isize { return Ok(()); }
let instr = &instrs[self.instr_pointer as usize - 1];
if debug { println!("ptr: {} | stack: {:?} | curr: {}", self.instr_pointer - 1, &self.stack, &instr); }
if debug { print_debug(&self, instr); }
match instr {
Store { address, value, .. } => {
self.store(&address, &value)?;
@ -114,6 +114,14 @@ impl VM {
}
}
fn print_debug(vm: &VM, curr_instr: &Instr) {
// get all register that are not null
let regs = vm.registers.iter().enumerate().filter(|(_, v)| !v.is_null()).collect::<Vec<_>>();
println!("regis: {:?}", regs);
println!("stack: {:?}", vm.stack);
println!("currn: {} {}", vm.instr_pointer, curr_instr);
}
fn call(index: &Type, args: &Type, line: isize) -> Result<(), Error> {
match index {
Type::Int(i) => {