Skip to content

Commit

Permalink
refactor(lexer/token): implement custom PartialEq & Hash traits
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Dec 31, 2024
1 parent acc1f59 commit 2c4d9a2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 78 deletions.
146 changes: 76 additions & 70 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
@@ -1,140 +1,146 @@
#[derive(Debug, PartialEq, Clone, Hash, Eq)]
use derive_more::derive::Display;
use std::{
hash::{Hash, Hasher},
mem::discriminant,
};

#[derive(Debug, Clone, Eq, Display)]
pub enum Token {
#[display("ident")]
Ident(String),
#[display("integer literal")]
Integer(String),
#[display("string literal")]
String(String),

#[display("=")]
Assign,
#[display("+")]
Plus,
#[display("-")]
Minus,
#[display("!")]
Bang,
#[display("*")]
Asterisk,
#[display("/")]
Slash,
#[display("->")]
Arrow,
#[display(".")]
Period,
#[display("~")]
Tilde,
#[display("&")]
Ampersand,
#[display("|")]
Bar,
#[display("==")]
Equal,
#[display("!=")]
NotEqual,
#[display("<")]
LessThan,
#[display(">")]
GreaterThan,
#[display("<=")]
LessEqual,
#[display(">=")]
GreaterEqual,
#[display("&&")]
And,
#[display("||")]
Or,
#[display("<<")]
Shl,
#[display(">>")]
Shr,
#[display(",")]
Comma,
#[display(";")]
Semicolon,
#[display(":")]
Colon,
#[display("(")]
LParen,
#[display(")")]
RParen,
#[display("{{")]
LBrace,
#[display("}}")]
RBrace,
#[display("[")]
LBracket,
#[display("]")]
RBracket,

#[display("const")]
Const,
#[display("true")]
True,
#[display("false")]
False,
#[display("let")]
Let,
#[display("fn")]
Fn,
#[display("enum")]
Enum,
#[display("struct")]
Struct,
#[display("if")]
If,
#[display("while")]
While,
#[display("for")]
For,
#[display("else")]
Else,
#[display("return")]
Return,
#[display("as")]
As,
#[display("continue")]
Continue,
#[display("break")]
Break,

#[display("u8")]
U8,
#[display("u16")]
U16,
#[display("u32")]
U32,
#[display("u64")]
U64,
#[display("i8")]
I8,
#[display("i16")]
I16,
#[display("i32")]
I32,
#[display("i64")]
I64,
#[display("usize")]
Usize,
#[display("isize")]
Isize,
#[display("bool")]
Bool,
#[display("void")]
Void,

#[display("null")]
Null,
}

impl std::fmt::Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Token::*;
impl PartialEq<Token> for Token {
fn eq(&self, other: &Token) -> bool {
discriminant(self) == discriminant(other)
}
}

match self {
Ident(ident) => {
write!(f, "{}", if ident.is_empty() { "ident" } else { ident })
}
Integer(int) => write!(f, "{int}"),
String(string) => write!(f, "\"{string}\""),
Assign => write!(f, "="),
Plus => write!(f, "+"),
Minus => write!(f, "-"),
Bang => write!(f, "!"),
Asterisk => write!(f, "*"),
Slash => write!(f, "/"),
Arrow => write!(f, "->"),
Period => write!(f, "."),
Tilde => write!(f, "~"),
Ampersand => write!(f, "&"),
Bar => write!(f, "|"),
Equal => write!(f, "=="),
NotEqual => write!(f, "!="),
LessThan => write!(f, "<"),
GreaterThan => write!(f, ">"),
LessEqual => write!(f, "<="),
GreaterEqual => write!(f, "=>"),
And => write!(f, "&&"),
Or => write!(f, "||"),
Shl => write!(f, "<<"),
Shr => write!(f, ">>"),
Comma => write!(f, ","),
Semicolon => write!(f, ";"),
Colon => write!(f, ":"),
LParen => write!(f, "("),
RParen => write!(f, ")"),
LBrace => write!(f, "{{"),
RBrace => write!(f, "}}"),
LBracket => write!(f, "["),
RBracket => write!(f, "]"),
Const => write!(f, "const"),
True => write!(f, "true"),
False => write!(f, "false"),
Let => write!(f, "let"),
Fn => write!(f, "fn"),
Enum => write!(f, "enum"),
Struct => write!(f, "struct"),
If => write!(f, "if"),
While => write!(f, "while"),
For => write!(f, "for"),
Else => write!(f, "else"),
Return => write!(f, "return"),
As => write!(f, "as"),
Continue => write!(f, "continue"),
Break => write!(f, "break"),
U8 => write!(f, "u8"),
U16 => write!(f, "u16"),
U32 => write!(f, "u32"),
U64 => write!(f, "u64"),
I8 => write!(f, "i8"),
I16 => write!(f, "i16"),
I32 => write!(f, "i32"),
I64 => write!(f, "i64"),
Usize => write!(f, "usize"),
Isize => write!(f, "isize"),
Bool => write!(f, "bool"),
Void => write!(f, "void"),
Null => write!(f, "NULL"),
}
impl Hash for Token {
fn hash<H: Hasher>(&self, state: &mut H) {
discriminant(self).hash(state)
}
}
11 changes: 3 additions & 8 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod expr;

use crate::{
diagnostics::{Diagnostic, Diagnostics},
lexer::{self, span::Span, Token},
lexer::{span::Span, Token},
};
pub use error::{Error, TyError};
pub use expr::*;
Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct Parser<'a, 'src, T: Iterator<Item = Result<Token, Span>>> {
}

impl<'a, 'src, T: Iterator<Item = Result<Token, Span>>> Parser<'a, 'src, T> {
pub fn new(mut lexer: T, diag: &'a mut Diagnostics<'src>) -> Result<Self, Error> {
pub fn new(lexer: T, diag: &'a mut Diagnostics<'src>) -> Result<Self, Error> {
let mut parser = Self {
cur_token: None,
peek_token: None,
Expand Down Expand Up @@ -147,12 +147,7 @@ impl<'a, 'src, T: Iterator<Item = Result<Token, Span>>> Parser<'a, 'src, T> {
}

pub fn expr(&mut self, precedence: Precedence) -> Result<Expr, Error> {
let token = match self.cur_token.as_ref().unwrap() {
Token::Ident(_) => Token::Ident(Default::default()),
Token::Integer(_) => Token::Integer(Default::default()),
Token::String(_) => Token::String(Default::default()),
token => token.clone(),
};
let token = self.cur_token.clone().unwrap();

let mut left = match self.prefix_fns.get(&token) {
Some(func) => func(self),
Expand Down

0 comments on commit 2c4d9a2

Please sign in to comment.