parser: parse let statements

- TODO: Type expressions
token: Add `mut` keyword
This commit is contained in:
John 2023-10-27 00:18:02 -05:00
parent f3aad36f32
commit ac540ebd22
2 changed files with 12 additions and 1 deletions

View File

@ -359,7 +359,15 @@ impl Parser {
fn stmt(&mut self) -> PResult<Stmt> { fn stmt(&mut self) -> PResult<Stmt> {
let token = self.peek()?; let token = self.peek()?;
match token.ty() { 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()?); let out = Stmt::Expr(self.expr()?);
self.consume_type(Type::Semi)?; self.consume_type(Type::Semi)?;

View File

@ -83,6 +83,7 @@ pub enum Keyword {
If, If,
In, In,
Let, Let,
Mut,
Return, Return,
True, True,
While, While,
@ -167,6 +168,7 @@ impl Display for Keyword {
Self::If => "if".fmt(f), Self::If => "if".fmt(f),
Self::In => "in".fmt(f), Self::In => "in".fmt(f),
Self::Let => "let".fmt(f), Self::Let => "let".fmt(f),
Self::Mut => "mut".fmt(f),
Self::Return => "return".fmt(f), Self::Return => "return".fmt(f),
Self::True => "true".fmt(f), Self::True => "true".fmt(f),
Self::While => "while".fmt(f), Self::While => "while".fmt(f),
@ -187,6 +189,7 @@ impl FromStr for Keyword {
"if" => Self::If, "if" => Self::If,
"in" => Self::In, "in" => Self::In,
"let" => Self::Let, "let" => Self::Let,
"mut" => Self::Mut,
"return" => Self::Return, "return" => Self::Return,
"true" => Self::True, "true" => Self::True,
"while" => Self::While, "while" => Self::While,