Skip to content

Commit

Permalink
Made tokens printable
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Dec 11, 2024
1 parent 33ce483 commit c6085e3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/config/verbosity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use serde::Deserialize;
#[derive(Default, Debug, Deserialize)]
pub enum VerbosityLevel {
/// No messages logged.
#[default]
None,

/// All messages are logged.
Expand All @@ -16,6 +15,7 @@ pub enum VerbosityLevel {
Medium,

/// Only high priority messages are logged.
#[default]
High,
}

Expand Down
24 changes: 24 additions & 0 deletions src/interpreter/ast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

/// Token that is parsed by a [Parser] from source input and can be consumed by an [Interpreter].
#[derive(Debug, Clone)]
pub enum Token {
Expand All @@ -22,3 +24,25 @@ pub enum Token {
/// Signals the [Interpreter] to read a byte from `stdin` and store it at the currently pointed at location.
Read,
}

impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Inc => write!(f, "Inc"),
Self::Dec => write!(f, "Dec"),
Self::MoveRight => write!(f, "MoveRght"),
Self::MoveLeft => write!(f, "MoveLeft"),
Self::Loop(vec) => {
write!(f, "Loop[")?;

for ele in vec {
write!(f, "{},", ele)?;
}

return write!(f, "]");
}
Self::Print => write!(f, "Print"),
Self::Read => write!(f, "Read"),
}
}
}
16 changes: 14 additions & 2 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ pub enum ProgramError {

/// Error when performing an I/O operation.
IOError,

/// Error when performing illegal operation
IllegalOperation
}

impl fmt::Display for ProgramError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProgramError::OutOfBounds => write!(f, "Attempted to move pointer past end of buffer"),
ProgramError::IOError => write!(f, "Error when attempting to perform IO operation"),
ProgramError::IllegalOperation => write!(f, "Operation is illegal")
}
}
}
Expand Down Expand Up @@ -106,8 +110,16 @@ impl Interpreter {
/// Processes the given [Token] and returns the result of its computation.
fn process_token(&self, token: Token) -> Result<(), ProgramError> {
match token {
Token::Inc => self.data.borrow_mut().inc(),
Token::Dec => self.data.borrow_mut().dec(),
Token::Inc => {
self.data.borrow_mut().inc();
}
Token::Dec => {
if self.data.borrow().get() == 0 {
return Err(ProgramError::IllegalOperation);
}

self.data.borrow_mut().dec();
}
Token::MoveLeft => {
if self.data.borrow().pointer() == 0 {
return Err(ProgramError::OutOfBounds);
Expand Down

0 comments on commit c6085e3

Please sign in to comment.