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},
};
use colored::Colorize;
use crate::{
AnnotatedError, Chunk, ChunkError, DustError, Identifier, Instruction, LexError, Lexer,
Operation, Span, Token, TokenKind, TokenOwned, Value,
@ -40,7 +42,11 @@ impl<'src> Parser<'src> {
pub fn new(mut lexer: Lexer<'src>) -> Result<Self, ParseError> {
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 {
lexer,
@ -96,7 +102,11 @@ impl<'src> Parser<'src> {
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_position = replace(&mut self.current_position, position);
@ -705,9 +715,7 @@ impl<'src> Parser<'src> {
);
}
if !self.allow(TokenKind::Comma)? {
self.expect(TokenKind::RightSquareBrace)?;
}
self.allow(TokenKind::Comma)?;
}
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),
Span(start, end),
);
self.increment_register()?;
Ok(())
}
@ -900,8 +907,8 @@ impl<'src> Parser<'src> {
if let Some(prefix_parser) = ParseRule::from(&self.current_token.kind()).prefix {
log::debug!(
"Prefix \"{}\" has precedence {precedence}",
self.current_token,
"{} is {precedence} prefix",
self.current_token.to_string().bold(),
);
prefix_parser(self, allow_assignment, allow_return)?;
@ -912,8 +919,8 @@ impl<'src> Parser<'src> {
while precedence <= infix_rule.precedence {
if let Some(infix_parser) = infix_rule.infix {
log::debug!(
"Infix \"{}\" has precedence {precedence}",
self.current_token,
"{} is {precedence} infix",
self.current_token.to_string().bold(),
);
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::Bang => write!(f, "!"),
Token::Bool => write!(f, "bool"),
Token::Boolean(value) => write!(f, "{}", value),
Token::Boolean(value) => write!(f, "{value}"),
Token::Break => write!(f, "break"),
Token::Byte(value) => write!(f, "{}", value),
Token::Character(value) => write!(f, "'{}'", value),
Token::Byte(value) => write!(f, "{value}"),
Token::Character(value) => write!(f, "{value}"),
Token::Colon => write!(f, ":"),
Token::Comma => write!(f, ","),
Token::Dot => write!(f, "."),
@ -252,14 +252,14 @@ impl<'src> Display for Token<'src> {
Token::Else => write!(f, "else"),
Token::Eof => write!(f, "EOF"),
Token::Equal => write!(f, "="),
Token::Float(value) => write!(f, "{}", value),
Token::Float(value) => write!(f, "{value}"),
Token::FloatKeyword => write!(f, "float"),
Token::Greater => write!(f, ">"),
Token::GreaterEqual => write!(f, ">="),
Token::Identifier(value) => write!(f, "{}", value),
Token::Identifier(value) => write!(f, "{value}"),
Token::If => write!(f, "if"),
Token::Int => write!(f, "int"),
Token::Integer(value) => write!(f, "{}", value),
Token::Integer(value) => write!(f, "{value}"),
Token::LeftCurlyBrace => write!(f, "{{"),
Token::LeftParenthesis => write!(f, "("),
Token::LeftSquareBrace => write!(f, "["),
@ -281,7 +281,7 @@ impl<'src> Display for Token<'src> {
Token::Slash => write!(f, "/"),
Token::Star => write!(f, "*"),
Token::Str => write!(f, "str"),
Token::String(value) => write!(f, "\"{}\"", value),
Token::String(value) => write!(f, "{value}"),
Token::Struct => write!(f, "struct"),
Token::While => write!(f, "while"),
}

View File

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

View File

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