diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f3e5445..95ea527 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,7 +1,7 @@ mod tree_node; use crate::tokenizer::{ - token::{self, Keyword, Symbol, Token, TokenType}, + token::{Keyword, Symbol, Token, TokenType}, Tokenizer, TokenizerBuffer, TokenizerError, }; use std::{ @@ -194,6 +194,13 @@ impl Parser { Expression::InvocationExpression(self.invocation()?) } + // match a variable expression with an assignment + TokenType::Identifier(_) + if self_matches_peek!(self, TokenType::Symbol(Symbol::Assign)) => + { + Expression::AssignmentExpression(self.assignment()?) + } + // match variable expressions with an identifier TokenType::Identifier(ref id) => Expression::Variable(id.clone()), @@ -261,6 +268,30 @@ impl Parser { } } + fn assignment(&mut self) -> Result { + let identifier = extract_token_data!( + token_from_option!(self.current_token), + TokenType::Identifier(ref id), + id.clone() + ); + + let current_token = token_from_option!(self.get_next()?).clone(); + if !token_matches!(current_token, TokenType::Symbol(Symbol::Assign)) { + return Err(ParseError::UnexpectedToken { + token: current_token.clone(), + backtrace: Backtrace::capture(), + }); + } + self.assign_next()?; + + let expression = self.expression()?.ok_or(ParseError::UnexpectedEOF)?; + + Ok(AssignmentExpression { + identifier, + expression: Box::new(expression), + }) + } + /// Handles mathmatical expressions in the explicit order of PEMDAS fn binary(&mut self, previous: Expression) -> Result { // We cannot use recursion here, as we need to handle the precedence of the operators diff --git a/tests/file.stlg b/tests/file.stlg index f5ad149..18f7c8b 100644 --- a/tests/file.stlg +++ b/tests/file.stlg @@ -3,5 +3,10 @@ fn doStuff() { return 234.5; }; - return i; -} \ No newline at end of file + i = 5; + + // This comment is going to be ignored. + return i; // we returned here +} + +doStuff(); \ No newline at end of file