feat(core): Display for EvalError + ParserError

This commit is contained in:
2026-03-31 20:14:49 +02:00
parent d4281d3538
commit d0840759b3
4 changed files with 45 additions and 1 deletions

View File

@@ -1,10 +1,12 @@
use std::fmt::Display;
use crate::parser::ParserError;
use super::environment::Environment;
use super::environment::EnvironmentLayer;
use super::expression::Expression;
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
/// All possible evaluation errors
pub enum EvalError {
SymbolNotBound(String),
@@ -14,6 +16,7 @@ pub enum EvalError {
TypeError(String),
NotASymbol(Expression),
RuntimeError(String),
ParserError(ParserError),
}
impl Display for EvalError {
@@ -26,6 +29,7 @@ impl Display for EvalError {
EvalError::TypeError(s) => write!(f, "Type error: {}", s),
EvalError::NotASymbol(e) => write!(f, "Expression {} is not a symbol", e),
EvalError::RuntimeError(s) => write!(f, "Runtime error: {}", s),
EvalError::ParserError(s) => write!(f, "Parser error: {}", s),
}
}
}

View File

@@ -3,6 +3,7 @@ use super::tokenizer::tokenize;
use super::tokenizer::TokenStream;
use super::tokenizer::TokenizerError;
use crate::lisp::Expression;
use std::fmt::Display;
use std::iter::Peekable;
#[derive(Debug, Clone, PartialEq)]
@@ -18,6 +19,16 @@ impl From<TokenizerError> for ParserError {
}
}
impl Display for ParserError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ParserError::TokenizerError(t) => write!(f, "Tokenizer Error: {}", t),
ParserError::UnexpectedToken(t) => write!(f, "Unexpecte Token: {}", t),
ParserError::UnexpectedEndOfInput => write!(f, "Unexpected end of input."),
}
}
}
fn parse_list<I>(stream: &mut Peekable<TokenStream<I>>) -> Result<Expression, ParserError>
where
I: Iterator<Item = char>,

View File

@@ -1,3 +1,5 @@
use std::fmt::Display;
#[derive(Debug, PartialEq, Clone)]
/// Sum type of different tokens
pub enum Token {
@@ -12,3 +14,20 @@ pub enum Token {
Symbol(String),
True,
}
impl Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Token::FloatLiteral(x) => write!(f, "{}", x),
Token::IntLiteral(x) => write!(f, "{}", x),
Token::Dot => write!(f, "."),
Token::Nil => write!(f, "nil"),
Token::ParClose => write!(f, ")"),
Token::ParOpen => write!(f, "("),
Token::Quote => write!(f, "'"),
Token::StringLiteral(x) => write!(f, "\"{}\"", x),
Token::Symbol(x) => write!(f, "{}", x),
Token::True => write!(f, "true"),
}
}
}

View File

@@ -1,3 +1,5 @@
use std::fmt::Display;
use super::token::Token;
#[derive(Debug, Clone, PartialEq)]
@@ -7,6 +9,14 @@ pub enum TokenizerError {
UnmatchedSequence(String),
}
impl Display for TokenizerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TokenizerError::UnmatchedSequence(s) => write!(f, "Unmatched sequence: {}", s),
}
}
}
/// A reader used to wrap the `TokenStream`.
/// When reading, it starts with the staging buffer of the stream, once
/// it's end is reached, the input stream is copied character wise to