From 5fb47a32caed3f351639bab9647dba8d653c52e2 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 27 Jan 2022 13:41:15 +0700 Subject: [PATCH] feat: null type and register debug info --- blspc/src/vm/instr.rs | 12 ++++++++++++ blspc/src/vm/vm.rs | 12 ++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index a6ed1f2..19f1286 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -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), diff --git a/blspc/src/vm/vm.rs b/blspc/src/vm/vm.rs index d962daf..c5d3b1d 100644 --- a/blspc/src/vm/vm.rs +++ b/blspc/src/vm/vm.rs @@ -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::>(); + 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) => {