From 7bb8d80ef82cfb0e72f4d09041d190bce46bffe5 Mon Sep 17 00:00:00 2001 From: MilkeeyCat Date: Fri, 19 Jul 2024 19:52:35 +0300 Subject: [PATCH] fix: accidentally broke variables --- src/lexer/token.rs | 11 +++++++++++ src/parser/parser.rs | 37 ++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/lexer/token.rs b/src/lexer/token.rs index 186add0..9703430 100644 --- a/src/lexer/token.rs +++ b/src/lexer/token.rs @@ -1,3 +1,4 @@ +use crate::scope::Scope; use std::fmt::Display; #[derive(Debug, PartialEq, Clone, Hash, Eq)] @@ -50,6 +51,16 @@ pub enum Token { Void, } +impl Token { + pub fn is_type(&self, scope: &Scope) -> bool { + match &self { + Token::U8 | Token::U16 | Token::I8 | Token::I16 | Token::Bool | Token::Void => true, + Token::Ident(ident) => scope.find_type(ident).is_some(), + _ => false, + } + } +} + impl Display for Token { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { use Token::*; diff --git a/src/parser/parser.rs b/src/parser/parser.rs index e22f3a3..ef9e4b6 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -238,31 +238,26 @@ impl Parser { self.expect(&Token::LBrace)?; while !self.cur_token_is(&Token::RBrace) { - match &self.cur_token { - Token::U8 - | Token::U16 - | Token::I8 - | Token::I16 - | Token::Bool - | Token::Void - | Token::Ident(_) => { - let type_ = self.parse_type()?; + if self.cur_token.is_type(&self.scope) { + let type_ = self.parse_type()?; - if self.peek_token_is(&Token::Semicolon) { - stmts.push(self.var_decl(type_)?); - } else { - self.function(type_, false)?; - } + if self.peek_token_is(&Token::Semicolon) { + stmts.push(self.var_decl(type_)?); + } else { + self.function(type_, false)?; } - Token::Return => stmts.push(self.parse_return()?), - _ => { - let expr = self.expr(Precedence::default())?; - expr.type_(&self.scope)?; - let expr = Stmt::Expr(expr); + } else { + match &self.cur_token { + Token::Return => stmts.push(self.parse_return()?), + _ => { + let expr = self.expr(Precedence::default())?; + expr.type_(&self.scope)?; + let expr = Stmt::Expr(expr); - self.expect(&Token::Semicolon)?; + self.expect(&Token::Semicolon)?; - stmts.push(expr); + stmts.push(expr); + } } } }