From ac540ebd22762ba87ba239910f72a511ca76e207 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 27 Oct 2023 00:18:02 -0500 Subject: [PATCH] parser: parse let statements - TODO: Type expressions token: Add `mut` keyword --- libconlang/src/parser.rs | 10 +++++++++- libconlang/src/token/token_type.rs | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/libconlang/src/parser.rs b/libconlang/src/parser.rs index db19575..0e84b83 100644 --- a/libconlang/src/parser.rs +++ b/libconlang/src/parser.rs @@ -359,7 +359,15 @@ impl Parser { fn stmt(&mut self) -> PResult { let token = self.peek()?; match token.ty() { - Type::Keyword(Keyword::Let) => todo!("Let statements"), + Type::Keyword(Keyword::Let) => Ok(Stmt::Let { + mutable: self.consume().keyword(Keyword::Mut).is_ok(), + name: self.identifier()?, + ty: self + .consume_type(Type::Colon) + .and_then(Self::identifier) + .ok(), + init: self.consume_type(Type::Eq).and_then(Self::expr).ok(), + }), _ => { let out = Stmt::Expr(self.expr()?); self.consume_type(Type::Semi)?; diff --git a/libconlang/src/token/token_type.rs b/libconlang/src/token/token_type.rs index 3205b32..47b44ec 100644 --- a/libconlang/src/token/token_type.rs +++ b/libconlang/src/token/token_type.rs @@ -83,6 +83,7 @@ pub enum Keyword { If, In, Let, + Mut, Return, True, While, @@ -167,6 +168,7 @@ impl Display for Keyword { Self::If => "if".fmt(f), Self::In => "in".fmt(f), Self::Let => "let".fmt(f), + Self::Mut => "mut".fmt(f), Self::Return => "return".fmt(f), Self::True => "true".fmt(f), Self::While => "while".fmt(f), @@ -187,6 +189,7 @@ impl FromStr for Keyword { "if" => Self::If, "in" => Self::In, "let" => Self::Let, + "mut" => Self::Mut, "return" => Self::Return, "true" => Self::True, "while" => Self::While,