From ad571700100c3cb8c472b681006c2b0581260ff1 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Sun, 13 Aug 2023 07:35:20 +0530 Subject: [PATCH] lexer: allow underscore for identifiers Signed-off-by: Amneesh Singh --- src/ast.rs | 56 +++++++++++++++++++++++++++++++++++----------------- src/lexer.rs | 10 +++++----- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 81fc08a..1b17cb1 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -82,24 +82,6 @@ pub struct Let { pub expr: Option, } -type Op = crate::lexer::TokenSymbol; - -/// Lowest form of expression -/// -/// TODO: refine -#[derive(Debug, PartialEq)] -pub enum Expr { - Int(i32), - Float(f32), - Char(char), - Op(Op, Box, Option>), - If(Box, Box, Option>), - Block(Vec), - Loop(Vec), - Break, - Continue, -} - /// Primitives /// /// TODO: add arrays and pointers maybe @@ -109,3 +91,41 @@ pub enum Ty { Float, Char, } + +#[derive(Debug, PartialEq)] +pub struct If { + pub cond: Box, + pub then: Vec, + pub or: Option>, +} + +#[derive(Debug, PartialEq)] +pub enum ElseType { + If(If), + Else(Vec), +} + +type Op = crate::lexer::TokenSymbol; + +#[derive(Debug, PartialEq)] +pub enum Literal { + Int(i32), + Float(f32), + Char(char), +} + +/// Lowest form of expression +/// +/// TODO: refine +#[derive(Debug, PartialEq)] +pub enum Expr { + Literal(Literal), + Identifier(Rc), + Op(Op, Box, Option>), + If(If), + Block(Vec), + Loop(Vec), + Break, + Continue, + Return(Option>), +} diff --git a/src/lexer.rs b/src/lexer.rs index 6a1460c..b8695dd 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -14,7 +14,7 @@ pub enum TokenLiteral { } /// All token symbols -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone, Copy)] pub enum TokenSymbol { // arithmetic Plus, @@ -38,9 +38,7 @@ pub enum TokenSymbol { // relational Gt, - Ge, Lt, - Le, GtEq, LtEq, EqEq, @@ -88,6 +86,7 @@ pub enum TokenKeyword { Loop, Break, Continue, + Return, // primitives Int, @@ -280,7 +279,7 @@ impl<'a> Lexer<'a> { fn get_alphanumeric(&mut self) -> Token { while let Some(c) = self.peek() { match c { - 'a'..='z' | 'A'..='Z' | '0'..='9' => {} + 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => {} _ => break, } self.next(); @@ -302,6 +301,7 @@ impl<'a> Lexer<'a> { "loop" => Keyword(Loop), "break" => Keyword(Break), "continue" => Keyword(Continue), + "return" => Keyword(Return), "int" => Keyword(Int), "float" => Keyword(Float), "char" => Keyword(Char), @@ -409,7 +409,7 @@ impl<'a> Lexer<'a> { self.new_token(TokenKind::Newline) } '0'..='9' => self.get_numeric(), - 'a'..='z' | 'A'..='Z' => self.get_alphanumeric(), + 'a'..='z' | 'A'..='Z' | '_' => self.get_alphanumeric(), '\'' => self.get_char(), _ => self.get_symbol(), }