feat(core): Display for EvalError + ParserError
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use crate::parser::ParserError;
|
||||||
|
|
||||||
use super::environment::Environment;
|
use super::environment::Environment;
|
||||||
use super::environment::EnvironmentLayer;
|
use super::environment::EnvironmentLayer;
|
||||||
use super::expression::Expression;
|
use super::expression::Expression;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
/// All possible evaluation errors
|
/// All possible evaluation errors
|
||||||
pub enum EvalError {
|
pub enum EvalError {
|
||||||
SymbolNotBound(String),
|
SymbolNotBound(String),
|
||||||
@@ -14,6 +16,7 @@ pub enum EvalError {
|
|||||||
TypeError(String),
|
TypeError(String),
|
||||||
NotASymbol(Expression),
|
NotASymbol(Expression),
|
||||||
RuntimeError(String),
|
RuntimeError(String),
|
||||||
|
ParserError(ParserError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for EvalError {
|
impl Display for EvalError {
|
||||||
@@ -26,6 +29,7 @@ impl Display for EvalError {
|
|||||||
EvalError::TypeError(s) => write!(f, "Type error: {}", s),
|
EvalError::TypeError(s) => write!(f, "Type error: {}", s),
|
||||||
EvalError::NotASymbol(e) => write!(f, "Expression {} is not a symbol", e),
|
EvalError::NotASymbol(e) => write!(f, "Expression {} is not a symbol", e),
|
||||||
EvalError::RuntimeError(s) => write!(f, "Runtime error: {}", s),
|
EvalError::RuntimeError(s) => write!(f, "Runtime error: {}", s),
|
||||||
|
EvalError::ParserError(s) => write!(f, "Parser error: {}", s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use super::tokenizer::tokenize;
|
|||||||
use super::tokenizer::TokenStream;
|
use super::tokenizer::TokenStream;
|
||||||
use super::tokenizer::TokenizerError;
|
use super::tokenizer::TokenizerError;
|
||||||
use crate::lisp::Expression;
|
use crate::lisp::Expression;
|
||||||
|
use std::fmt::Display;
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[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>
|
fn parse_list<I>(stream: &mut Peekable<TokenStream<I>>) -> Result<Expression, ParserError>
|
||||||
where
|
where
|
||||||
I: Iterator<Item = char>,
|
I: Iterator<Item = char>,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
/// Sum type of different tokens
|
/// Sum type of different tokens
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
@@ -12,3 +14,20 @@ pub enum Token {
|
|||||||
Symbol(String),
|
Symbol(String),
|
||||||
True,
|
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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use super::token::Token;
|
use super::token::Token;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
@@ -7,6 +9,14 @@ pub enum TokenizerError {
|
|||||||
UnmatchedSequence(String),
|
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`.
|
/// A reader used to wrap the `TokenStream`.
|
||||||
/// When reading, it starts with the staging buffer of the stream, once
|
/// 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
|
/// it's end is reached, the input stream is copied character wise to
|
||||||
|
|||||||
Reference in New Issue
Block a user