From c6ce019aef1e46da636bdb3023459a4025226fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 12 Jan 2021 22:00:17 +0100 Subject: Amaizing colors --- Cargo.lock | 6 ++++++ Cargo.toml | 1 + src/vm.rs | 36 +++++++++++++++++++++--------------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6aa16a5..fc59a72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,11 @@ dependencies = [ "utf8-ranges", ] +[[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" @@ -76,6 +81,7 @@ name = "tihdy" version = "0.1.0" dependencies = [ "logos", + "owo-colors", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 7f7fb74..1268ed0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] logos = "0.11.4" +owo-colors = { git="https://github.com/FredTheDino/owo-colors.git" } diff --git a/src/vm.rs b/src/vm.rs index 2962b7f..68ab3b8 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -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) -> 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) -> 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(); -- cgit v1.2.1