diff --git a/src/lexer/token.rs b/src/lexer/token.rs index ab29493..1079b74 100644 --- a/src/lexer/token.rs +++ b/src/lexer/token.rs @@ -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 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(&self, state: &mut H) { + discriminant(self).hash(state) } } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 15180f6..cca6f1d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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::*; @@ -43,7 +43,7 @@ pub struct Parser<'a, 'src, T: Iterator>> { } impl<'a, 'src, T: Iterator>> Parser<'a, 'src, T> { - pub fn new(mut lexer: T, diag: &'a mut Diagnostics<'src>) -> Result { + pub fn new(lexer: T, diag: &'a mut Diagnostics<'src>) -> Result { let mut parser = Self { cur_token: None, peek_token: None, @@ -147,12 +147,7 @@ impl<'a, 'src, T: Iterator>> Parser<'a, 'src, T> { } pub fn expr(&mut self, precedence: Precedence) -> Result { - 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),