diff options
| -rw-r--r-- | Cargo.lock | 6 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/vm.rs | 36 |
3 files changed, 28 insertions, 15 deletions
@@ -37,6 +37,11 @@ dependencies = [ ] [[package]] +name = "owo-colors" +version = "1.2.1" +source = "git+https://github.com/FredTheDino/owo-colors.git#4ca8cc3da368d17831af67b85fa33215deb4570f" + +[[package]] name = "proc-macro2" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -76,6 +81,7 @@ name = "tihdy" version = "0.1.0" dependencies = [ "logos", + "owo-colors", ] [[package]] @@ -8,3 +8,4 @@ edition = "2018" [dependencies] logos = "0.11.4" +owo-colors = { git="https://github.com/FredTheDino/owo-colors.git" } @@ -1,6 +1,7 @@ use std::path::{Path, PathBuf}; use std::rc::Rc; use std::collections::HashMap; +use owo_colors::OwoColorize; use crate::error::{Error, ErrorKind}; @@ -135,20 +136,20 @@ impl VM { self.stack.get(self.stack.len() - amount) } - fn error(&self, kind: ErrorKind, message: Option<String>) -> Error { - let find_line = || { - for i in (0..=self.ip).rev() { - if let Some(line) = self.block.line_offsets.get(&i) { - return *line; - } + fn line(&self) -> usize { + for i in (0..=self.ip).rev() { + if let Some(line) = self.block.line_offsets.get(&i) { + return *line; } - return 0; - }; + } + return 0; + } + fn error(&self, kind: ErrorKind, message: Option<String>) -> Error { Error { kind, file: self.block.file.clone(), - line: find_line(), + line: self.line(), message, } } @@ -158,9 +159,14 @@ impl VM { const PRINT_BLOCK: bool = true; if PRINT_BLOCK { - println!(" === {} ===", self.block.name); - for s in self.block.ops.iter() { - println!("| {:?}", s); + println!(" === {} ===", self.block.name.blue()); + for (i, s) in self.block.ops.iter().enumerate() { + if self.block.line_offsets.contains_key(&i) { + print!("{:5} ", self.block.line_offsets[&i].red()); + } else { + print!(" {} ", "|".red()); + } + println!("{:05} {:?}", i.blue(), s); } println!(""); } @@ -173,13 +179,13 @@ impl VM { print!(" "); } match s { - Value::String(rc) => print!("{:?}<{}>", rc, Rc::strong_count(rc)), - _ => print!("{:?}", s), + Value::String(rc) => print!("{:?}<{}>", rc.green(), Rc::strong_count(rc)), + _ => print!("{:?}", s.green()), } } println!("]"); - println!("{:?}", self.block.ops[self.ip]); + println!("{:5} {:05} {:?}", self.line().red(), self.ip.blue(), self.block.ops[self.ip]); } let op = self.block.ops[self.ip].clone(); |
