This commit is contained in:
2024-11-20 01:16:46 -07:00
parent 66064a21d7
commit 7cff659275
4 changed files with 236 additions and 1 deletions

View File

@@ -36,6 +36,20 @@ pub enum TokenType {
EOF,
}
impl std::fmt::Display for TokenType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TokenType::String(s) => write!(f, "{}", s),
TokenType::Number(n) => write!(f, "{}", n),
TokenType::Boolean(b) => write!(f, "{}", b),
TokenType::Keyword(k) => write!(f, "{:?}", k),
TokenType::Identifier(i) => write!(f, "{}", i),
TokenType::Symbol(s) => write!(f, "{:?}", s),
TokenType::EOF => write!(f, "EOF"),
}
}
}
#[derive(Debug, PartialEq, Hash, Eq)]
pub enum Number {
/// Represents an integer number
@@ -44,6 +58,15 @@ pub enum Number {
Decimal(u64, u64),
}
impl std::fmt::Display for Number {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Number::Integer(i) => write!(f, "{}", i),
Number::Decimal(i, d) => write!(f, "{}.{}", i, d),
}
}
}
#[derive(Debug, PartialEq, Hash, Eq)]
pub enum Symbol {
// Single Character Symbols
@@ -99,6 +122,41 @@ pub enum Symbol {
GreaterThanOrEqual,
}
impl Symbol {
pub fn is_operator(&self) -> bool {
match self {
Symbol::Plus | Symbol::Minus | Symbol::Asterisk | Symbol::Slash => true,
_ => false,
}
}
pub fn is_comparison(&self) -> bool {
match self {
Symbol::LessThan
| Symbol::GreaterThan
| Symbol::Equal
| Symbol::NotEqual
| Symbol::LessThanOrEqual
| Symbol::GreaterThanOrEqual => true,
_ => false,
}
}
pub fn is_logical(&self) -> bool {
match self {
Symbol::LogicalAnd | Symbol::LogicalOr => true,
_ => false,
}
}
pub fn is_assignment(&self) -> bool {
match self {
Symbol::Assign => true,
_ => false,
}
}
}
#[derive(Debug, PartialEq, Hash, Eq)]
pub enum Keyword {
/// Represents the `let` keyword