1
0

Improve logs; Clean up

This commit is contained in:
Jeff 2024-09-25 01:27:10 -04:00
parent daca836db1
commit 47d6ea417d
4 changed files with 38 additions and 56 deletions

View File

@ -7,6 +7,8 @@ use std::{
num::{ParseFloatError, ParseIntError}, num::{ParseFloatError, ParseIntError},
}; };
use colored::Colorize;
use crate::{ use crate::{
AnnotatedError, Chunk, ChunkError, DustError, Identifier, Instruction, LexError, Lexer, AnnotatedError, Chunk, ChunkError, DustError, Identifier, Instruction, LexError, Lexer,
Operation, Span, Token, TokenKind, TokenOwned, Value, Operation, Span, Token, TokenKind, TokenOwned, Value,
@ -40,7 +42,11 @@ impl<'src> Parser<'src> {
pub fn new(mut lexer: Lexer<'src>) -> Result<Self, ParseError> { pub fn new(mut lexer: Lexer<'src>) -> Result<Self, ParseError> {
let (current_token, current_position) = lexer.next_token()?; let (current_token, current_position) = lexer.next_token()?;
log::info!("Starting parser with token \"{current_token}\" at {current_position}"); log::info!(
"{} at {}",
current_token.to_string().bold(),
current_position.to_string()
);
Ok(Parser { Ok(Parser {
lexer, lexer,
@ -96,7 +102,11 @@ impl<'src> Parser<'src> {
let (new_token, position) = self.lexer.next_token()?; let (new_token, position) = self.lexer.next_token()?;
log::info!("Parsing \"{new_token}\" at {position}"); log::info!(
"{} at {}",
new_token.to_string().bold(),
position.to_string()
);
self.previous_token = replace(&mut self.current_token, new_token); self.previous_token = replace(&mut self.current_token, new_token);
self.previous_position = replace(&mut self.current_position, position); self.previous_position = replace(&mut self.current_position, position);
@ -705,9 +715,7 @@ impl<'src> Parser<'src> {
); );
} }
if !self.allow(TokenKind::Comma)? { self.allow(TokenKind::Comma)?;
self.expect(TokenKind::RightSquareBrace)?;
}
} }
let end_register = self.current_register - 1; let end_register = self.current_register - 1;
@ -717,7 +725,6 @@ impl<'src> Parser<'src> {
Instruction::load_list(self.current_register, start_register, end_register), Instruction::load_list(self.current_register, start_register, end_register),
Span(start, end), Span(start, end),
); );
self.increment_register()?;
Ok(()) Ok(())
} }
@ -900,8 +907,8 @@ impl<'src> Parser<'src> {
if let Some(prefix_parser) = ParseRule::from(&self.current_token.kind()).prefix { if let Some(prefix_parser) = ParseRule::from(&self.current_token.kind()).prefix {
log::debug!( log::debug!(
"Prefix \"{}\" has precedence {precedence}", "{} is {precedence} prefix",
self.current_token, self.current_token.to_string().bold(),
); );
prefix_parser(self, allow_assignment, allow_return)?; prefix_parser(self, allow_assignment, allow_return)?;
@ -912,8 +919,8 @@ impl<'src> Parser<'src> {
while precedence <= infix_rule.precedence { while precedence <= infix_rule.precedence {
if let Some(infix_parser) = infix_rule.infix { if let Some(infix_parser) = infix_rule.infix {
log::debug!( log::debug!(
"Infix \"{}\" has precedence {precedence}", "{} is {precedence} infix",
self.current_token, self.current_token.to_string().bold(),
); );
if allow_assignment && self.current_token == Token::Equal { if allow_assignment && self.current_token == Token::Equal {

View File

@ -238,10 +238,10 @@ impl<'src> Display for Token<'src> {
Token::BangEqual => write!(f, "!="), Token::BangEqual => write!(f, "!="),
Token::Bang => write!(f, "!"), Token::Bang => write!(f, "!"),
Token::Bool => write!(f, "bool"), Token::Bool => write!(f, "bool"),
Token::Boolean(value) => write!(f, "{}", value), Token::Boolean(value) => write!(f, "{value}"),
Token::Break => write!(f, "break"), Token::Break => write!(f, "break"),
Token::Byte(value) => write!(f, "{}", value), Token::Byte(value) => write!(f, "{value}"),
Token::Character(value) => write!(f, "'{}'", value), Token::Character(value) => write!(f, "{value}"),
Token::Colon => write!(f, ":"), Token::Colon => write!(f, ":"),
Token::Comma => write!(f, ","), Token::Comma => write!(f, ","),
Token::Dot => write!(f, "."), Token::Dot => write!(f, "."),
@ -252,14 +252,14 @@ impl<'src> Display for Token<'src> {
Token::Else => write!(f, "else"), Token::Else => write!(f, "else"),
Token::Eof => write!(f, "EOF"), Token::Eof => write!(f, "EOF"),
Token::Equal => write!(f, "="), Token::Equal => write!(f, "="),
Token::Float(value) => write!(f, "{}", value), Token::Float(value) => write!(f, "{value}"),
Token::FloatKeyword => write!(f, "float"), Token::FloatKeyword => write!(f, "float"),
Token::Greater => write!(f, ">"), Token::Greater => write!(f, ">"),
Token::GreaterEqual => write!(f, ">="), Token::GreaterEqual => write!(f, ">="),
Token::Identifier(value) => write!(f, "{}", value), Token::Identifier(value) => write!(f, "{value}"),
Token::If => write!(f, "if"), Token::If => write!(f, "if"),
Token::Int => write!(f, "int"), Token::Int => write!(f, "int"),
Token::Integer(value) => write!(f, "{}", value), Token::Integer(value) => write!(f, "{value}"),
Token::LeftCurlyBrace => write!(f, "{{"), Token::LeftCurlyBrace => write!(f, "{{"),
Token::LeftParenthesis => write!(f, "("), Token::LeftParenthesis => write!(f, "("),
Token::LeftSquareBrace => write!(f, "["), Token::LeftSquareBrace => write!(f, "["),
@ -281,7 +281,7 @@ impl<'src> Display for Token<'src> {
Token::Slash => write!(f, "/"), Token::Slash => write!(f, "/"),
Token::Star => write!(f, "*"), Token::Star => write!(f, "*"),
Token::Str => write!(f, "str"), Token::Str => write!(f, "str"),
Token::String(value) => write!(f, "\"{}\"", value), Token::String(value) => write!(f, "{value}"),
Token::Struct => write!(f, "struct"), Token::Struct => write!(f, "struct"),
Token::While => write!(f, "while"), Token::While => write!(f, "while"),
} }

View File

@ -56,11 +56,11 @@ impl Vm {
} }
while let Ok((instruction, position)) = self.read(Span(0, 0)).copied() { while let Ok((instruction, position)) = self.read(Span(0, 0)).copied() {
log::info!( let ip = self.ip - 1;
"Running IP {} {} at {position}", let operation = instruction.operation();
self.ip - 1, let position_display = position.to_string();
instruction.operation()
); log::info!("{ip:^3} | {position_display:^10} | {operation}");
match instruction.operation() { match instruction.operation() {
Operation::Move => { Operation::Move => {
@ -68,8 +68,6 @@ impl Vm {
let to = instruction.a(); let to = instruction.a();
let value = self.take(from, position)?; let value = self.take(from, position)?;
log::debug!("R{from} -{{{value}}}-> R{to}");
self.insert(value, to, position)?; self.insert(value, to, position)?;
} }
Operation::Close => { Operation::Close => {
@ -106,8 +104,6 @@ impl Vm {
let length = last_register - first_register + 1; let length = last_register - first_register + 1;
let mut list = Vec::with_capacity(length as usize); let mut list = Vec::with_capacity(length as usize);
log::debug!("R{to_register} = [R{first_register}..=R{last_register}]");
for register_index in first_register..=last_register { for register_index in first_register..=last_register {
let value = match self.take(register_index, position) { let value = match self.take(register_index, position) {
Ok(value) => value, Ok(value) => value,
@ -171,8 +167,6 @@ impl Vm {
.add(right) .add(right)
.map_err(|error| VmError::Value { error, position })?; .map_err(|error| VmError::Value { error, position })?;
log::debug!("{left} + {right} = {sum}");
self.insert(sum, instruction.a(), position)?; self.insert(sum, instruction.a(), position)?;
} }
Operation::Subtract => { Operation::Subtract => {
@ -181,8 +175,6 @@ impl Vm {
.subtract(right) .subtract(right)
.map_err(|error| VmError::Value { error, position })?; .map_err(|error| VmError::Value { error, position })?;
log::debug!("{left} - {right} = {difference}");
self.insert(difference, instruction.a(), position)?; self.insert(difference, instruction.a(), position)?;
} }
Operation::Multiply => { Operation::Multiply => {
@ -192,8 +184,6 @@ impl Vm {
.multiply(right) .multiply(right)
.map_err(|error| VmError::Value { error, position })?; .map_err(|error| VmError::Value { error, position })?;
log::debug!("{position} {left} * {right}");
self.insert(product, instruction.a(), position)?; self.insert(product, instruction.a(), position)?;
} }
Operation::Divide => { Operation::Divide => {
@ -202,8 +192,6 @@ impl Vm {
.divide(right) .divide(right)
.map_err(|error| VmError::Value { error, position })?; .map_err(|error| VmError::Value { error, position })?;
log::debug!("{left} / {right} = {quotient}");
self.insert(quotient, instruction.a(), position)?; self.insert(quotient, instruction.a(), position)?;
} }
Operation::Modulo => { Operation::Modulo => {
@ -212,8 +200,6 @@ impl Vm {
.modulo(right) .modulo(right)
.map_err(|error| VmError::Value { error, position })?; .map_err(|error| VmError::Value { error, position })?;
log::debug!("{left} % {right} = {remainder}");
self.insert(remainder, instruction.a(), position)?; self.insert(remainder, instruction.a(), position)?;
} }
Operation::Test => { Operation::Test => {
@ -262,8 +248,6 @@ impl Vm {
})?; })?;
let compare_to = instruction.a_as_boolean(); let compare_to = instruction.a_as_boolean();
log::debug!("{left} == {right} = {boolean} == {compare_to}");
if boolean == compare_to { if boolean == compare_to {
self.ip += 1; self.ip += 1;
} else { } else {
@ -295,8 +279,6 @@ impl Vm {
})?; })?;
let compare_to = instruction.a_as_boolean(); let compare_to = instruction.a_as_boolean();
log::debug!("{left} < {right} = {boolean} == {compare_to}");
if boolean == compare_to { if boolean == compare_to {
self.ip += 1; self.ip += 1;
} else { } else {
@ -327,8 +309,6 @@ impl Vm {
})?; })?;
let compare_to = instruction.a_as_boolean(); let compare_to = instruction.a_as_boolean();
log::debug!("{left} <= {right} = {boolean} == {compare_to}");
if boolean == compare_to { if boolean == compare_to {
self.ip += 1; self.ip += 1;
} else { } else {
@ -353,8 +333,6 @@ impl Vm {
.negate() .negate()
.map_err(|error| VmError::Value { error, position })?; .map_err(|error| VmError::Value { error, position })?;
log::debug!("-({value}) = {negated}");
self.insert(negated, instruction.a(), position)?; self.insert(negated, instruction.a(), position)?;
} }
Operation::Not => { Operation::Not => {
@ -367,8 +345,6 @@ impl Vm {
.not() .not()
.map_err(|error| VmError::Value { error, position })?; .map_err(|error| VmError::Value { error, position })?;
log::debug!("!{value} = {not}");
self.insert(not, instruction.a(), position)?; self.insert(not, instruction.a(), position)?;
} }
Operation::Jump => { Operation::Jump => {

View File

@ -23,22 +23,21 @@ fn main() {
env_logger::builder() env_logger::builder()
.parse_env("DUST_LOG") .parse_env("DUST_LOG")
.format(|buf, record| { .format(|buf, record| {
let level = match record.level() { let level_display = match record.level() {
Level::Info => "INFO".white().bold(), Level::Info => "INFO".bold().white(),
Level::Debug => "DEBUG".blue().bold(), Level::Debug => "DEBUG".bold().blue(),
Level::Warn => "WARN".yellow().bold(), Level::Warn => "WARN".bold().yellow(),
Level::Error => "ERROR".red().bold(), Level::Error => "ERROR".bold().red(),
Level::Trace => "TRACE".purple().bold(), Level::Trace => "TRACE".bold().purple(),
} };
.bold();
let level_display = format!("{level:<5}");
let module = record let module = record
.module_path() .module_path()
.map(|path| path.split("::").last().unwrap_or(path)) .map(|path| path.split("::").last().unwrap_or(path))
.unwrap_or("unknown") .unwrap_or("unknown")
.dimmed(); .dimmed();
let display = format!("{level_display:5} {module:^6} {args}", args = record.args());
writeln!(buf, "{level_display:^10} {module:^6} {}", record.args()) writeln!(buf, "{display}")
}) })
.init(); .init();