Skip to content

Commit

Permalink
fix: accidentally broke variables
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Jul 19, 2024
1 parent 156ecd4 commit 7bb8d80
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::scope::Scope;
use std::fmt::Display;

#[derive(Debug, PartialEq, Clone, Hash, Eq)]
Expand Down Expand Up @@ -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::*;
Expand Down
37 changes: 16 additions & 21 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down

0 comments on commit 7bb8d80

Please sign in to comment.