From e076ddba00666774be6105860e3df187f336e664 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 00:37:29 +0100 Subject: [PATCH 01/13] feat: seperated body parsing from outside parsing --- parser/src/ast/func/mod.rs | 4 ++-- parser/src/ast/mod.rs | 31 ++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/parser/src/ast/func/mod.rs b/parser/src/ast/func/mod.rs index 9444457..2f312a9 100644 --- a/parser/src/ast/func/mod.rs +++ b/parser/src/ast/func/mod.rs @@ -3,7 +3,7 @@ use std::fmt::Debug; use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; -use crate::{ParserError, ParserResult, ast::{parse_ast_node, tree::{ASTTreeNode, FunctionDeclarationArgument}}}; +use crate::{ParserError, ParserResult, ast::{parse_ast_node_in_body, tree::{ASTTreeNode, FunctionDeclarationArgument}}}; pub mod decl; pub mod call; @@ -15,7 +15,7 @@ pub fn parse_node_body(tokens: &Vec, ind: &mut usize) -> PositionedR let mut body: Vec> = Vec::new(); while tok.tok_type != LexerTokenType::END_OF_FILE && tok.tok_type != LexerTokenType::BRACKET_CLOSE { - let n = parse_ast_node(tokens, ind)?; + let n = parse_ast_node_in_body(tokens, ind)?; body.push(n); diff --git a/parser/src/ast/mod.rs b/parser/src/ast/mod.rs index d12aa8d..f4d36c4 100644 --- a/parser/src/ast/mod.rs +++ b/parser/src/ast/mod.rs @@ -110,12 +110,29 @@ pub fn parse_ast_value(tokens: &Vec, ind: &mut usize) -> PositionedR } pub fn parse_ast_node(tokens: &Vec, ind: &mut usize) -> PositionedResult> { - println!("Ind: {}, tok at: {:#?}", ind, tokens[*ind].tok_type); - match &tokens[*ind].tok_type { LexerTokenType::FUNCTION => { return parse_function_declaraction(tokens, ind); + }, + + LexerTokenType::STRUCT => { + return parse_type_declaration(tokens, ind, false); + }, + + LexerTokenType::LAYOUT => { + return parse_type_declaration(tokens, ind, true); + }, + + _ => { + return Err(tokens[*ind].make_err("Expected valid token type in this context!")); } + } +} + +pub fn parse_ast_node_in_body(tokens: &Vec, ind: &mut usize) -> PositionedResult> { + println!("Ind: {}, tok at: {:#?}", ind, tokens[*ind].tok_type); + + match &tokens[*ind].tok_type { LexerTokenType::VAR => { return parse_variable_declaration(tokens, ind); @@ -131,15 +148,7 @@ pub fn parse_ast_node(tokens: &Vec, ind: &mut usize) -> PositionedRe LexerTokenType::FOR => { return parse_for_loop(tokens, ind); - }, - - LexerTokenType::STRUCT => { - return parse_type_declaration(tokens, ind, false); - }, - - LexerTokenType::LAYOUT => { - return parse_type_declaration(tokens, ind, true); - }, + } LexerTokenType::KEYWORD(str, _) => { if tokens[*ind + 1].tok_type == LexerTokenType::PAREN_OPEN { From 0060a180710b646eb1206ed1bffb425bbfddd856 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 00:41:09 +0100 Subject: [PATCH 02/13] feat: added comparing operators --- lexer/src/toks/comp.rs | 11 +++++++++++ lexer/src/toks/mod.rs | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 lexer/src/toks/comp.rs diff --git a/lexer/src/toks/comp.rs b/lexer/src/toks/comp.rs new file mode 100644 index 0000000..302abe8 --- /dev/null +++ b/lexer/src/toks/comp.rs @@ -0,0 +1,11 @@ +//! Comparing token related utils + +/// The different comparing tokens +pub enum ComparingOperator { + EQUAL, // A == B + N_EQUAL, // A != B + HIGHER, // A > B + HIGHER_EQ, // A >= B + LOWER, // A < B + LOWER_EQ // A <= B +} \ No newline at end of file diff --git a/lexer/src/toks/mod.rs b/lexer/src/toks/mod.rs index 354c7c9..1854b67 100644 --- a/lexer/src/toks/mod.rs +++ b/lexer/src/toks/mod.rs @@ -1 +1,2 @@ -pub mod math; \ No newline at end of file +pub mod math; +pub mod comp; \ No newline at end of file From 47a6ad852847fddf12182c0c8d2c3232ff77bb67 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 00:56:20 +0100 Subject: [PATCH 03/13] feat: added comp operators as lexer tokens --- lexer/src/lexer.rs | 64 ++++++++++++++++++++++++++++++++++++++++-- lexer/src/token.rs | 4 ++- lexer/src/toks/comp.rs | 1 + 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/lexer/src/lexer.rs b/lexer/src/lexer.rs index 2bc997c..ae635c9 100644 --- a/lexer/src/lexer.rs +++ b/lexer/src/lexer.rs @@ -2,11 +2,11 @@ //! Module containing the core lexer algorithm //! -use std::{fs, hash::{DefaultHasher, Hash, Hasher}, io::Error}; +use std::{arch::naked_asm, fs, hash::{DefaultHasher, Hash, Hasher}, io::Error}; use commons::Position; -use crate::{LexerParseResult, LexerParsingError, token::{LexerToken, LexerTokenType}, toks::math::MathOperator}; +use crate::{LexerParseResult, LexerParsingError, token::{LexerToken, LexerTokenType}, toks::{comp::ComparingOperator, math::MathOperator}}; const FUNC_KEYWORD_HASH: u64 = 17439195341824537259; const RET_KEYWORD_HASH: u64 = 9222097151127739705; @@ -80,6 +80,19 @@ pub fn lexer_parse_file(file_path: &String) -> LexerParseResult> continue; } + if c == '=' || c == '>' || c == '<' { + let col = i - last_line_break + 1; + + let parse = parse_comp_operator(&contents, &mut i, Position::new(file_path.to_string(), line, col)); + + if parse.is_some() { + tokens.push(parse.unwrap()); + continue; + } + + i -= 2; // Try parsing operator as normal token. + } + i += c.len_utf8(); @@ -125,7 +138,7 @@ fn parse_math_operator(contents: &String, ind: &mut usize, start_pos: Position) *ind += 1; let assigns = match contents.chars().nth(*ind) { - Some(v) => v == '=', + Some(v) => v == '=', // TODO: make this enforce either '=' or ' ' None => false }; @@ -145,6 +158,51 @@ fn parse_math_operator(contents: &String, ind: &mut usize, start_pos: Position) } +fn parse_comp_operator(contents: &String, ind: &mut usize, start_pos: Position) -> Option { + let firstChar = contents.chars().nth(*ind).unwrap(); + *ind += 1; + let secondChar = contents.chars().nth(*ind).unwrap(); + + *ind += 1; + + let end = start_pos.increment_by(2); + + if secondChar != '=' || secondChar != ' ' { + return None; + } + + match firstChar { + '=' => { + if secondChar != '=' { + return None; + } + + return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::EQUAL))); + }, + + '>' => { + if secondChar == '=' { + return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::HIGHER_EQ))); + } + + return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::HIGHER))); + }, + + '<' => { + if secondChar == '=' { + return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::LOWER_EQ))); + } + + return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::LOWER))); + }, + + _ => { + return None; + } + } + +} + fn parse_number_token(str: &String, ind: &mut usize, start_pos: Position) -> LexerParseResult { let start = *ind + 1; let mut end: usize = start; diff --git a/lexer/src/token.rs b/lexer/src/token.rs index 1d7e27d..a01a5da 100644 --- a/lexer/src/token.rs +++ b/lexer/src/token.rs @@ -6,7 +6,7 @@ use std::any::Any; use commons::{Position, err::{PositionedError, PositionedResult}}; -use crate::{LexerParseResult, LexerParsingError, toks::math::MathOperator}; +use crate::{LexerParseResult, LexerParsingError, toks::{comp::ComparingOperator, math::MathOperator}}; /// The token type for the lexer #[derive(PartialEq, Debug)] @@ -22,6 +22,8 @@ pub enum LexerTokenType { /// 0: the operator /// 1: does the operator affect the original variable! MATH_OPERATOR(MathOperator, bool), + + COMPARING_OPERATOR(ComparingOperator), /// Represent the ret keyword diff --git a/lexer/src/toks/comp.rs b/lexer/src/toks/comp.rs index 302abe8..facfbb9 100644 --- a/lexer/src/toks/comp.rs +++ b/lexer/src/toks/comp.rs @@ -1,6 +1,7 @@ //! Comparing token related utils /// The different comparing tokens +#[derive(Debug, PartialEq, Clone)] pub enum ComparingOperator { EQUAL, // A == B N_EQUAL, // A != B From e0f69d9678629e1f89871d83f1d72fc689620079 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 00:57:23 +0100 Subject: [PATCH 04/13] chore: removed AST side of condition operators --- parser/src/ast/cond/mod.rs | 2 -- parser/src/ast/cond/operators.rs | 60 -------------------------------- 2 files changed, 62 deletions(-) delete mode 100644 parser/src/ast/cond/operators.rs diff --git a/parser/src/ast/cond/mod.rs b/parser/src/ast/cond/mod.rs index 0d5d07c..f675c8b 100644 --- a/parser/src/ast/cond/mod.rs +++ b/parser/src/ast/cond/mod.rs @@ -1,5 +1,3 @@ use lexer::token::LexerToken; use crate::{ParserError, ParserResult, ast::tree::ASTTreeNode}; - -pub mod operators; diff --git a/parser/src/ast/cond/operators.rs b/parser/src/ast/cond/operators.rs deleted file mode 100644 index 8c9909d..0000000 --- a/parser/src/ast/cond/operators.rs +++ /dev/null @@ -1,60 +0,0 @@ -use commons::err::PositionedResult; -use lexer::token::{LexerToken, LexerTokenType}; - -use crate::{ParserError, ParserResult}; - -#[derive(Debug, PartialEq, Clone)] -pub enum ConditionOperator { - EQUAL, - NOT_EQUAL, - - HIGHER, // A > B - LOWER, // A < B - - HIGHEREQ, // A >= B - LOWEREQ // A <= B -} - -pub fn parse_condition_operator(tokens: &Vec, ind: &mut usize) -> PositionedResult { - match &tokens[*ind].tok_type { - LexerTokenType::EQUAL_SIGN => { - *ind += 1; - - tokens[*ind].expects(LexerTokenType::EQUAL_SIGN); - - return Ok(ConditionOperator::EQUAL) - }, - - LexerTokenType::EXCLAMATION_MARK => { - *ind += 1; - - if tokens[*ind].tok_type == LexerTokenType::EQUAL_SIGN { - return Ok(ConditionOperator::NOT_EQUAL) - } - }, - - LexerTokenType::ANGEL_BRACKET_OPEN => { - *ind += 1; - - if tokens[*ind].tok_type == LexerTokenType::EQUAL_SIGN { - return Ok(ConditionOperator::LOWEREQ); - } - - return Ok(ConditionOperator::LOWER); - }, - - LexerTokenType::ANGEL_BRACKET_CLOSE => { - *ind += 1; - - if tokens[*ind].tok_type == LexerTokenType::EQUAL_SIGN { - return Ok(ConditionOperator::HIGHEREQ); - } - - return Ok(ConditionOperator::HIGHER); - }, - - _ => {} - } - - Err(tokens[*ind].make_err("Token doesn't make a valid condition operator")) -} \ No newline at end of file From 6b3da0776e66e8cee990db33a347f54b691d219b Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:00:31 +0100 Subject: [PATCH 05/13] feat: added LexerToken::expects_comp_operator --- lexer/src/token.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lexer/src/token.rs b/lexer/src/token.rs index a01a5da..0277386 100644 --- a/lexer/src/token.rs +++ b/lexer/src/token.rs @@ -98,6 +98,13 @@ impl LexerToken { }; } + pub fn expects_comp_operator(&self) -> PositionedResult { + match &self.tok_type { + LexerTokenType::COMPARING_OPERATOR(op) => return Ok(op.clone()), + _ => return Err(self.make_err("Expected comparing operator here!")) + }; + } + pub fn expects_math_operator(&self) -> PositionedResult<(MathOperator, bool)> { match &self.tok_type { LexerTokenType::MATH_OPERATOR(a, b) => return Ok((a.clone(), *b)), From 44df66704b2035eb6e36cbde9baa53da81ab1edb Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:03:03 +0100 Subject: [PATCH 06/13] feat: migrated AST to use the new comparing operators --- parser/src/ast/cond/mod.rs | 3 --- parser/src/ast/mod.rs | 7 +++---- parser/src/ast/tree.rs | 6 ++---- 3 files changed, 5 insertions(+), 11 deletions(-) delete mode 100644 parser/src/ast/cond/mod.rs diff --git a/parser/src/ast/cond/mod.rs b/parser/src/ast/cond/mod.rs deleted file mode 100644 index f675c8b..0000000 --- a/parser/src/ast/cond/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -use lexer::token::LexerToken; - -use crate::{ParserError, ParserResult, ast::tree::ASTTreeNode}; diff --git a/parser/src/ast/mod.rs b/parser/src/ast/mod.rs index f4d36c4..78b2f8e 100644 --- a/parser/src/ast/mod.rs +++ b/parser/src/ast/mod.rs @@ -12,13 +12,12 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; -use crate::{ParserError, ParserResult, ast::{cond::operators::parse_condition_operator, control::{forloop::parse_for_loop, ifelse::parse_if_statement, whileblock::parse_while_block}, func::{call::parse_function_call, decl::parse_function_declaraction}, literals::{parse_integer_literal, parse_string_literal}, math::parse_math_operation, tree::ASTTreeNode, types::parse_type_declaration, var::decl::parse_variable_declaration}}; +use crate::{ParserError, ParserResult, ast::{control::{forloop::parse_for_loop, ifelse::parse_if_statement, whileblock::parse_while_block}, func::{call::parse_function_call, decl::parse_function_declaraction}, literals::{parse_integer_literal, parse_string_literal}, math::parse_math_operation, tree::ASTTreeNode, types::parse_type_declaration, var::decl::parse_variable_declaration}}; pub mod tree; pub mod func; pub mod var; pub mod literals; -pub mod cond; pub mod control; pub mod math; pub mod types; @@ -52,8 +51,8 @@ pub fn parse_ast_value_post_l(tokens: &Vec, ind: &mut usize, origina return Ok(parse_math_operation(tokens, ind, k, invoked_on_body)?); }, - LexerTokenType::ANGEL_BRACKET_CLOSE | LexerTokenType::EQUAL_SIGN | LexerTokenType::ANGEL_BRACKET_OPEN => { - let operator = parse_condition_operator(tokens, ind)?; + LexerTokenType::COMPARING_OPERATOR(op) => { + let operator = op.clone(); let o = &original?; let k = Box::new(ASTTreeNode::clone(o.as_ref())); diff --git a/parser/src/ast/tree.rs b/parser/src/ast/tree.rs index b3ca6ec..7daa3a3 100644 --- a/parser/src/ast/tree.rs +++ b/parser/src/ast/tree.rs @@ -2,11 +2,9 @@ //! AST tree related definitions. //! -use lexer::toks::math::MathOperator; +use lexer::toks::{comp::ComparingOperator, math::MathOperator}; use utils::hash::{TypeHash, WithHash}; -use crate::ast::{cond::operators::ConditionOperator}; - #[derive(Debug, PartialEq, Clone)] pub struct FunctionDeclarationArgument { pub name: WithHash, @@ -24,7 +22,7 @@ pub enum ASTTreeNode { IntegerLit(i64), StringLit(String), - OperatorBasedConditionMember { lval: Box, rval: Box, operator: ConditionOperator }, + OperatorBasedConditionMember { lval: Box, rval: Box, operator: ComparingOperator }, BooleanBasedConditionMember { val: Box, negate: bool }, MathResult { lval: Box, rval: Box, operator: MathOperator, assigns: bool }, From 66ff4159199622da53fd658a1632d9a0a91dd1cb Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:05:37 +0100 Subject: [PATCH 07/13] chore: removed useless code & imports --- parser/src/ast/control/forloop.rs | 2 +- parser/src/ast/control/ifelse.rs | 2 +- parser/src/ast/control/whileblock.rs | 2 +- parser/src/ast/func/call.rs | 2 +- parser/src/ast/func/decl.rs | 2 +- parser/src/ast/func/mod.rs | 4 +--- parser/src/ast/literals.rs | 6 +++--- parser/src/ast/mod.rs | 4 +--- parser/src/ast/types/mod.rs | 2 -- parser/src/ast/var/decl.rs | 2 +- parser/src/lib.rs | 22 ---------------------- 11 files changed, 11 insertions(+), 39 deletions(-) diff --git a/parser/src/ast/control/forloop.rs b/parser/src/ast/control/forloop.rs index f65ed39..d9528c0 100644 --- a/parser/src/ast/control/forloop.rs +++ b/parser/src/ast/control/forloop.rs @@ -1,7 +1,7 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; -use crate::{ParserError, ParserResult, ast::{func::parse_node_body, parse_ast_node, parse_ast_value, tree::ASTTreeNode, var::decl::parse_variable_declaration}}; +use crate::{ast::{func::parse_node_body, parse_ast_node, parse_ast_value, tree::ASTTreeNode, var::decl::parse_variable_declaration}}; pub fn parse_for_loop(tokens: &Vec, ind: &mut usize) -> PositionedResult> { *ind += 1; diff --git a/parser/src/ast/control/ifelse.rs b/parser/src/ast/control/ifelse.rs index e29033c..faef57a 100644 --- a/parser/src/ast/control/ifelse.rs +++ b/parser/src/ast/control/ifelse.rs @@ -5,7 +5,7 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; -use crate::{ParserError, ParserResult, ast::{func::parse_node_body, parse_ast_value, tree::ASTTreeNode}}; +use crate::{ast::{func::parse_node_body, parse_ast_value, tree::ASTTreeNode}}; pub fn parse_condition_member(tokens: &Vec, ind: &mut usize) -> PositionedResult> { tokens[*ind].expects(LexerTokenType::PAREN_OPEN)?; diff --git a/parser/src/ast/control/whileblock.rs b/parser/src/ast/control/whileblock.rs index b4103d7..d9c7235 100644 --- a/parser/src/ast/control/whileblock.rs +++ b/parser/src/ast/control/whileblock.rs @@ -1,7 +1,7 @@ use commons::err::PositionedResult; use lexer::token::LexerToken; -use crate::{ParserError, ParserResult, ast::{control::ifelse::parse_condition_member, func::parse_node_body, tree::ASTTreeNode}}; +use crate::{ast::{control::ifelse::parse_condition_member, func::parse_node_body, tree::ASTTreeNode}}; pub fn parse_while_block(tokens: &Vec, ind: &mut usize) -> PositionedResult> { *ind += 1; diff --git a/parser/src/ast/func/call.rs b/parser/src/ast/func/call.rs index c23da7e..c39a394 100644 --- a/parser/src/ast/func/call.rs +++ b/parser/src/ast/func/call.rs @@ -2,7 +2,7 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; -use crate::{ParserError, ParserResult, ast::{parse_ast_value, tree::ASTTreeNode}}; +use crate::{ast::{parse_ast_value, tree::ASTTreeNode}}; pub fn parse_function_call(tokens: &Vec, ind: &mut usize) -> PositionedResult> { diff --git a/parser/src/ast/func/decl.rs b/parser/src/ast/func/decl.rs index 8f89de9..f554ef4 100644 --- a/parser/src/ast/func/decl.rs +++ b/parser/src/ast/func/decl.rs @@ -2,7 +2,7 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; -use crate::{ParserError, ParserResult, ast::{func::{parse_function_arguments, parse_node_body}, tree::ASTTreeNode}}; +use crate::{ast::{func::{parse_function_arguments, parse_node_body}, tree::ASTTreeNode}}; pub fn parse_function_declaraction(tokens: &Vec, ind: &mut usize) -> PositionedResult> { *ind += 1; diff --git a/parser/src/ast/func/mod.rs b/parser/src/ast/func/mod.rs index 2f312a9..37af710 100644 --- a/parser/src/ast/func/mod.rs +++ b/parser/src/ast/func/mod.rs @@ -3,7 +3,7 @@ use std::fmt::Debug; use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; -use crate::{ParserError, ParserResult, ast::{parse_ast_node_in_body, tree::{ASTTreeNode, FunctionDeclarationArgument}}}; +use crate::{ast::{parse_ast_node_in_body, tree::{ASTTreeNode, FunctionDeclarationArgument}}}; pub mod decl; pub mod call; @@ -19,8 +19,6 @@ pub fn parse_node_body(tokens: &Vec, ind: &mut usize) -> PositionedR body.push(n); - //println!("Func index: {}", *ind); - tok = &tokens[*ind]; } diff --git a/parser/src/ast/literals.rs b/parser/src/ast/literals.rs index 69f7e2d..d5f74ac 100644 --- a/parser/src/ast/literals.rs +++ b/parser/src/ast/literals.rs @@ -1,7 +1,7 @@ -use commons::err::{PositionedError, PositionedResult}; -use lexer::token::{LexerToken, LexerTokenType}; +use commons::err::{PositionedResult}; +use lexer::token::{LexerToken}; -use crate::{ParserError, ParserResult, ast::tree::ASTTreeNode}; +use crate::{ast::tree::ASTTreeNode}; pub fn parse_integer_literal(tokens: &Vec, ind: &mut usize) -> PositionedResult> { let val = tokens[*ind].expects_int_lit()?; diff --git a/parser/src/ast/mod.rs b/parser/src/ast/mod.rs index 78b2f8e..0bfe516 100644 --- a/parser/src/ast/mod.rs +++ b/parser/src/ast/mod.rs @@ -6,13 +6,11 @@ //! Indexes passed to parsing functions SHOULD be the "detected" token rather than the next one. //! -use std::fmt::Debug; - use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; -use crate::{ParserError, ParserResult, ast::{control::{forloop::parse_for_loop, ifelse::parse_if_statement, whileblock::parse_while_block}, func::{call::parse_function_call, decl::parse_function_declaraction}, literals::{parse_integer_literal, parse_string_literal}, math::parse_math_operation, tree::ASTTreeNode, types::parse_type_declaration, var::decl::parse_variable_declaration}}; +use crate::{ast::{control::{forloop::parse_for_loop, ifelse::parse_if_statement, whileblock::parse_while_block}, func::{call::parse_function_call, decl::parse_function_declaraction}, literals::{parse_integer_literal, parse_string_literal}, math::parse_math_operation, tree::ASTTreeNode, types::parse_type_declaration, var::decl::parse_variable_declaration}}; pub mod tree; pub mod func; diff --git a/parser/src/ast/types/mod.rs b/parser/src/ast/types/mod.rs index fb39816..a915df2 100644 --- a/parser/src/ast/types/mod.rs +++ b/parser/src/ast/types/mod.rs @@ -1,5 +1,3 @@ -use std::mem; - use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; diff --git a/parser/src/ast/var/decl.rs b/parser/src/ast/var/decl.rs index 2821a2b..9f76eae 100644 --- a/parser/src/ast/var/decl.rs +++ b/parser/src/ast/var/decl.rs @@ -2,7 +2,7 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; -use crate::{ParserError, ParserResult, ast::{parse_ast_node, parse_ast_value, tree::ASTTreeNode}}; +use crate::{ast::{parse_ast_value, tree::ASTTreeNode}}; pub fn parse_variable_declaration(tokens: &Vec, ind: &mut usize) -> PositionedResult> { *ind += 1; diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 5cd3274..851c0bc 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -1,23 +1 @@ -use core::fmt; - pub mod ast; - -type ParserResult = std::result::Result; - -#[derive(Debug, Clone)] -pub struct ParserError { - reason: String, - position: usize -} - -impl ParserError { - pub fn new(reason: String, position: usize) -> Self { - ParserError { reason, position } - } -} - -impl fmt::Display for ParserError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "Parsing Error: {} at position {}", self.reason, self.position) - } -} \ No newline at end of file From 37f942bbcc332a9ad037c4895872ec1faecb9de7 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:15:49 +0100 Subject: [PATCH 08/13] chore: renamed every lexer token type to fit unified case --- lexer/src/lexer.rs | 68 +++++++++++++------------- lexer/src/token.rs | 72 ++++++++++++++-------------- parser/src/ast/control/forloop.rs | 10 ++-- parser/src/ast/control/ifelse.rs | 14 +++--- parser/src/ast/control/whileblock.rs | 2 +- parser/src/ast/func/call.rs | 6 +-- parser/src/ast/func/decl.rs | 4 +- parser/src/ast/func/mod.rs | 10 ++-- parser/src/ast/literals.rs | 6 +-- parser/src/ast/mod.rs | 34 ++++++------- parser/src/ast/types/mod.rs | 4 +- parser/src/ast/var/decl.rs | 2 +- 12 files changed, 115 insertions(+), 117 deletions(-) diff --git a/lexer/src/lexer.rs b/lexer/src/lexer.rs index ae635c9..8b02a2d 100644 --- a/lexer/src/lexer.rs +++ b/lexer/src/lexer.rs @@ -101,25 +101,25 @@ pub fn lexer_parse_file(file_path: &String) -> LexerParseResult> let pos = Position::new(file_path.to_string(), line, col); match c { - '{' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::BRACKET_OPEN)), - '}' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::BRACKET_CLOSE)), - '(' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::PAREN_OPEN)), - ')' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::PAREN_CLOSE)), - '[' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ARRAY_OPEN)), - ']' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ARRAY_CLOSE)), - '=' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::EQUAL_SIGN)), - ',' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::COMMA)), - '.' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::DOT)), - '!' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::EXCLAMATION_MARK)), - '&' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::AMPERSAND)), - '<' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ANGEL_BRACKET_OPEN)), - '>' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ANGEL_BRACKET_CLOSE)), + '{' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::BracketOpen)), + '}' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::BracketClose)), + '(' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ParenOpen)), + ')' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ParenClose)), + '[' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ArrayOpen)), + ']' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ArrayClose)), + '=' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::EqualSign)), + ',' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::Comma)), + '.' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::Dot)), + '!' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::ExclamationMark)), + '&' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::Ampersand)), + '<' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::AngelBracketOpen)), + '>' => tokens.push(LexerToken::make_single_sized(pos, LexerTokenType::AngelBracketClose)), _ => continue } } - tokens.push(LexerToken::make_single_sized(Position::new(file_path.to_string(), line, i - last_line_break + 1), LexerTokenType::END_OF_FILE)); + tokens.push(LexerToken::make_single_sized(Position::new(file_path.to_string(), line, i - last_line_break + 1), LexerTokenType::EndOfFile)); Ok(tokens) } @@ -154,7 +154,7 @@ fn parse_math_operator(contents: &String, ind: &mut usize, start_pos: Position) let end = start_pos.increment_by(incrementCount); - return Ok(LexerToken::new(start_pos, end, LexerTokenType::MATH_OPERATOR(operator, assigns))); + return Ok(LexerToken::new(start_pos, end, LexerTokenType::MathOperator(operator, assigns))); } @@ -177,23 +177,23 @@ fn parse_comp_operator(contents: &String, ind: &mut usize, start_pos: Position) return None; } - return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::EQUAL))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::EQUAL))); }, '>' => { if secondChar == '=' { - return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::HIGHER_EQ))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::HIGHER_EQ))); } - return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::HIGHER))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::HIGHER))); }, '<' => { if secondChar == '=' { - return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::LOWER_EQ))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::LOWER_EQ))); } - return Some(LexerToken::new(start_pos, end, LexerTokenType::COMPARING_OPERATOR(ComparingOperator::LOWER))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::LOWER))); }, _ => { @@ -224,7 +224,7 @@ fn parse_number_token(str: &String, ind: &mut usize, start_pos: Position) -> Lex *ind = end; let endpos = start_pos.increment_by(end - start); - return Ok(LexerToken::new(start_pos, endpos, LexerTokenType::INT_LIT(num))); + return Ok(LexerToken::new(start_pos, endpos, LexerTokenType::IntLit(num))); } fn parse_string_token(str: &String, ind: &mut usize, start_pos: Position) -> LexerToken { @@ -245,7 +245,7 @@ fn parse_string_token(str: &String, ind: &mut usize, start_pos: Position) -> Lex *ind = end; let endpos: Position = start_pos.increment_by(end - start); - return LexerToken::new(start_pos, endpos, LexerTokenType::STRING_LIT(slice.to_string())); + return LexerToken::new(start_pos, endpos, LexerTokenType::StringLit(slice.to_string())); } fn parse_keyword(str: &String, ind: &mut usize, start_pos: Position) -> LexerToken { @@ -270,18 +270,18 @@ fn parse_keyword(str: &String, ind: &mut usize, start_pos: Position) -> LexerTok *ind = end; let token_type = match hash { - FUNC_KEYWORD_HASH => LexerTokenType::FUNCTION, - RET_KEYWORD_HASH => LexerTokenType::RETURN, - STRUCT_KEYWORD_HASH => LexerTokenType::STRUCT, - LAYOUT_KEYWORD_HASH => LexerTokenType::LAYOUT, - LAY_KEYWORD_HASH => LexerTokenType::LAY, - TRUE_KEYWORD_HASH => LexerTokenType::TRUE, - FALSE_KEYWORD_HASH => LexerTokenType::FALSE, - VAR_KEYWORD_HASH => LexerTokenType::VAR, - IF_KEYWORD_HASH => LexerTokenType::IF, - ELSE_KEYWORD_HASH => LexerTokenType::ELSE, - WHILE_KEYWORD_HASH => LexerTokenType::WHILE, - FOR_KEYWORD_HASH => LexerTokenType::FOR, + FUNC_KEYWORD_HASH => LexerTokenType::Function, + RET_KEYWORD_HASH => LexerTokenType::Return, + STRUCT_KEYWORD_HASH => LexerTokenType::Struct, + LAYOUT_KEYWORD_HASH => LexerTokenType::Layout, + LAY_KEYWORD_HASH => LexerTokenType::Lay, + TRUE_KEYWORD_HASH => LexerTokenType::True, + FALSE_KEYWORD_HASH => LexerTokenType::False, + VAR_KEYWORD_HASH => LexerTokenType::Var, + IF_KEYWORD_HASH => LexerTokenType::If, + ELSE_KEYWORD_HASH => LexerTokenType::Else, + WHILE_KEYWORD_HASH => LexerTokenType::While, + FOR_KEYWORD_HASH => LexerTokenType::For, _ => LexerTokenType::KEYWORD(slice.to_string(), hash) }; diff --git a/lexer/src/token.rs b/lexer/src/token.rs index 0277386..85e237f 100644 --- a/lexer/src/token.rs +++ b/lexer/src/token.rs @@ -12,55 +12,55 @@ use crate::{LexerParseResult, LexerParsingError, toks::{comp::ComparingOperator, #[derive(PartialEq, Debug)] pub enum LexerTokenType { /// Represent the func keyword - FUNCTION, + Function, - VAR, - STRUCT, - LAYOUT, - LAY, + Var, + Struct, + Layout, + Lay, /// 0: the operator /// 1: does the operator affect the original variable! - MATH_OPERATOR(MathOperator, bool), + MathOperator(MathOperator, bool), - COMPARING_OPERATOR(ComparingOperator), + ComparingOperator(ComparingOperator), /// Represent the ret keyword - RETURN, + Return, - TRUE, - FALSE, + True, + False, - FOR, - IF, - ELSE, - WHILE, + For, + If, + Else, + While, - EQUAL_SIGN, - EXCLAMATION_MARK, + EqualSign, + ExclamationMark, - COMMA, - DOT, - AMPERSAND, + Comma, + Dot, + Ampersand, - BRACKET_OPEN, - BRACKET_CLOSE, + BracketOpen, + BracketClose, - PAREN_OPEN, - PAREN_CLOSE, + ParenOpen, + ParenClose, - ARRAY_OPEN, - ARRAY_CLOSE, + ArrayOpen, + ArrayClose, - INT_LIT(i64), - STRING_LIT(String), + IntLit(i64), + StringLit(String), - ANGEL_BRACKET_OPEN, - ANGEL_BRACKET_CLOSE, + AngelBracketOpen, + AngelBracketClose, KEYWORD(String, u64), - END_OF_FILE + EndOfFile } pub struct LexerToken { @@ -91,30 +91,30 @@ impl LexerToken { return Ok(true); } - pub fn expects_int_lit(&self) -> PositionedResult { + pub fn expects_IntLit(&self) -> PositionedResult { match &self.tok_type { - LexerTokenType::INT_LIT(v) => return Ok(*v), + LexerTokenType::IntLit(v) => return Ok(*v), _ => return Err(self.make_err("Expected int litteral here!")) }; } pub fn expects_comp_operator(&self) -> PositionedResult { match &self.tok_type { - LexerTokenType::COMPARING_OPERATOR(op) => return Ok(op.clone()), + LexerTokenType::ComparingOperator(op) => return Ok(op.clone()), _ => return Err(self.make_err("Expected comparing operator here!")) }; } pub fn expects_math_operator(&self) -> PositionedResult<(MathOperator, bool)> { match &self.tok_type { - LexerTokenType::MATH_OPERATOR(a, b) => return Ok((a.clone(), *b)), + LexerTokenType::MathOperator(a, b) => return Ok((a.clone(), *b)), _ => return Err(self.make_err("Expected math operator here!")) }; } - pub fn expects_string_lit(&self) -> PositionedResult { + pub fn expects_StringLit(&self) -> PositionedResult { match &self.tok_type { - LexerTokenType::STRING_LIT(v) => return Ok(v.to_string()), + LexerTokenType::StringLit(v) => return Ok(v.to_string()), _ => return Err(self.make_err("Expected string litteral here!")) }; } diff --git a/parser/src/ast/control/forloop.rs b/parser/src/ast/control/forloop.rs index d9528c0..f403549 100644 --- a/parser/src/ast/control/forloop.rs +++ b/parser/src/ast/control/forloop.rs @@ -6,26 +6,26 @@ use crate::{ast::{func::parse_node_body, parse_ast_node, parse_ast_value, tree:: pub fn parse_for_loop(tokens: &Vec, ind: &mut usize) -> PositionedResult> { *ind += 1; - tokens[*ind].expects(LexerTokenType::PAREN_OPEN)?; + tokens[*ind].expects(LexerTokenType::ParenOpen)?; let initial = parse_variable_declaration(tokens, ind)?; - tokens[*ind].expects(LexerTokenType::COMMA)?; + tokens[*ind].expects(LexerTokenType::Comma)?; *ind += 1; let cond = parse_ast_value(tokens, ind)?; - tokens[*ind].expects(LexerTokenType::COMMA)?; + tokens[*ind].expects(LexerTokenType::Comma)?; *ind += 1; let increment = parse_ast_node(tokens, ind)?; *ind += 1; - tokens[*ind].expects(LexerTokenType::PAREN_CLOSE)?; + tokens[*ind].expects(LexerTokenType::ParenClose)?; *ind += 1; - tokens[*ind].expects(LexerTokenType::BRACKET_OPEN)?; + tokens[*ind].expects(LexerTokenType::BracketOpen)?; let body = parse_node_body(tokens, ind)?; diff --git a/parser/src/ast/control/ifelse.rs b/parser/src/ast/control/ifelse.rs index faef57a..8cc88a0 100644 --- a/parser/src/ast/control/ifelse.rs +++ b/parser/src/ast/control/ifelse.rs @@ -8,12 +8,12 @@ use lexer::token::{LexerToken, LexerTokenType}; use crate::{ast::{func::parse_node_body, parse_ast_value, tree::ASTTreeNode}}; pub fn parse_condition_member(tokens: &Vec, ind: &mut usize) -> PositionedResult> { - tokens[*ind].expects(LexerTokenType::PAREN_OPEN)?; + tokens[*ind].expects(LexerTokenType::ParenOpen)?; *ind += 1; let cond = parse_ast_value(tokens, ind)?; - tokens[*ind].expects(LexerTokenType::PAREN_CLOSE)?; + tokens[*ind].expects(LexerTokenType::ParenClose)?; *ind += 1; @@ -25,7 +25,7 @@ pub fn parse_if_statement(tokens: &Vec, ind: &mut usize) -> Position let cond = parse_condition_member(tokens, ind)?; - tokens[*ind].expects(LexerTokenType::BRACKET_OPEN)?; + tokens[*ind].expects(LexerTokenType::BracketOpen)?; let body = match parse_node_body(tokens, ind) { Ok(v) => v, @@ -34,7 +34,7 @@ pub fn parse_if_statement(tokens: &Vec, ind: &mut usize) -> Position let mut elseStatement = None; - if tokens[*ind + 1].tok_type == LexerTokenType::ELSE { + if tokens[*ind + 1].tok_type == LexerTokenType::Else { *ind += 1; elseStatement = Some(parse_else_statement(tokens, ind)?); @@ -48,12 +48,12 @@ pub fn parse_else_statement(tokens: &Vec, ind: &mut usize) -> Positi let mut cond = None; - if tokens[*ind].tok_type == LexerTokenType::IF { + if tokens[*ind].tok_type == LexerTokenType::If { *ind += 1; cond = Some(parse_condition_member(tokens, ind)?); } - tokens[*ind].expects(LexerTokenType::BRACKET_OPEN)?; + tokens[*ind].expects(LexerTokenType::BracketOpen)?; let body = match parse_node_body(tokens, ind) { Ok(v) => v, @@ -63,7 +63,7 @@ pub fn parse_else_statement(tokens: &Vec, ind: &mut usize) -> Positi if cond.is_some() { let mut elseStatement = None; - if tokens[*ind + 1].tok_type == LexerTokenType::ELSE { + if tokens[*ind + 1].tok_type == LexerTokenType::Else { *ind += 1; elseStatement = Some(parse_else_statement(tokens, ind)?); diff --git a/parser/src/ast/control/whileblock.rs b/parser/src/ast/control/whileblock.rs index d9c7235..c9532b5 100644 --- a/parser/src/ast/control/whileblock.rs +++ b/parser/src/ast/control/whileblock.rs @@ -8,7 +8,7 @@ pub fn parse_while_block(tokens: &Vec, ind: &mut usize) -> Positione let cond = parse_condition_member(tokens, ind)?; - tokens[*ind].expects(lexer::token::LexerTokenType::BRACKET_OPEN)?; + tokens[*ind].expects(lexer::token::LexerTokenType::BracketOpen)?; let body = match parse_node_body(tokens, ind) { Ok(v) => v, diff --git a/parser/src/ast/func/call.rs b/parser/src/ast/func/call.rs index c39a394..c42cb2d 100644 --- a/parser/src/ast/func/call.rs +++ b/parser/src/ast/func/call.rs @@ -10,16 +10,16 @@ pub fn parse_function_call(tokens: &Vec, ind: &mut usize) -> Positio *ind += 1; - tokens[*ind].expects(LexerTokenType::PAREN_OPEN)?; + tokens[*ind].expects(LexerTokenType::ParenOpen)?; *ind += 1; let mut vals: Vec> = Vec::new(); - while tokens[*ind].tok_type != LexerTokenType::PAREN_CLOSE { + while tokens[*ind].tok_type != LexerTokenType::ParenClose { vals.push(parse_ast_value(tokens, ind)?); - tokens[*ind].expects(LexerTokenType::COMMA)?; + tokens[*ind].expects(LexerTokenType::Comma)?; *ind += 1; } diff --git a/parser/src/ast/func/decl.rs b/parser/src/ast/func/decl.rs index f554ef4..25c5b16 100644 --- a/parser/src/ast/func/decl.rs +++ b/parser/src/ast/func/decl.rs @@ -9,13 +9,13 @@ pub fn parse_function_declaraction(tokens: &Vec, ind: &mut usize) -> let functionName = tokens[*ind].expects_keyword()?; *ind += 1; - tokens[*ind].expects(LexerTokenType::PAREN_OPEN)?; + tokens[*ind].expects(LexerTokenType::ParenOpen)?; let args = parse_function_arguments(tokens, ind)?; *ind += 1; - tokens[*ind].expects(LexerTokenType::BRACKET_OPEN)?; + tokens[*ind].expects(LexerTokenType::BracketOpen)?; let body = parse_node_body(tokens, ind)?; diff --git a/parser/src/ast/func/mod.rs b/parser/src/ast/func/mod.rs index 37af710..c982c26 100644 --- a/parser/src/ast/func/mod.rs +++ b/parser/src/ast/func/mod.rs @@ -1,5 +1,3 @@ -use std::fmt::Debug; - use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; @@ -14,7 +12,7 @@ pub fn parse_node_body(tokens: &Vec, ind: &mut usize) -> PositionedR let mut tok: &LexerToken = &tokens[*ind]; let mut body: Vec> = Vec::new(); - while tok.tok_type != LexerTokenType::END_OF_FILE && tok.tok_type != LexerTokenType::BRACKET_CLOSE { + while tok.tok_type != LexerTokenType::EndOfFile && tok.tok_type != LexerTokenType::BracketClose { let n = parse_ast_node_in_body(tokens, ind)?; body.push(n); @@ -42,16 +40,16 @@ pub fn parse_function_arguments(tokens: &Vec, ind: &mut usize) -> Po *ind += 1; - if tokens[*ind].tok_type == LexerTokenType::PAREN_CLOSE { + if tokens[*ind].tok_type == LexerTokenType::ParenClose { break; } - tokens[*ind].expects(LexerTokenType::COMMA)?; + tokens[*ind].expects(LexerTokenType::Comma)?; *ind += 1; } - tokens[*ind].expects(LexerTokenType::PAREN_CLOSE)?; + tokens[*ind].expects(LexerTokenType::ParenClose)?; Ok(args) } \ No newline at end of file diff --git a/parser/src/ast/literals.rs b/parser/src/ast/literals.rs index d5f74ac..a269ee7 100644 --- a/parser/src/ast/literals.rs +++ b/parser/src/ast/literals.rs @@ -4,14 +4,14 @@ use lexer::token::{LexerToken}; use crate::{ast::tree::ASTTreeNode}; pub fn parse_integer_literal(tokens: &Vec, ind: &mut usize) -> PositionedResult> { - let val = tokens[*ind].expects_int_lit()?; + let val = tokens[*ind].expects_IntLit()?; *ind += 1; return Ok(Box::new(ASTTreeNode::IntegerLit(val))); } -pub fn parse_string_literal(tokens: &Vec, ind: &mut usize) -> PositionedResult> { - let val = tokens[*ind].expects_string_lit()?; +pub fn parse_StringLiteral(tokens: &Vec, ind: &mut usize) -> PositionedResult> { + let val = tokens[*ind].expects_StringLit()?; *ind += 1; return Ok(Box::new(ASTTreeNode::StringLit(val))); diff --git a/parser/src/ast/mod.rs b/parser/src/ast/mod.rs index 0bfe516..c00cd8b 100644 --- a/parser/src/ast/mod.rs +++ b/parser/src/ast/mod.rs @@ -10,7 +10,7 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; -use crate::{ast::{control::{forloop::parse_for_loop, ifelse::parse_if_statement, whileblock::parse_while_block}, func::{call::parse_function_call, decl::parse_function_declaraction}, literals::{parse_integer_literal, parse_string_literal}, math::parse_math_operation, tree::ASTTreeNode, types::parse_type_declaration, var::decl::parse_variable_declaration}}; +use crate::{ast::{control::{forloop::parse_for_loop, ifelse::parse_if_statement, whileblock::parse_while_block}, func::{call::parse_function_call, decl::parse_function_declaraction}, literals::{parse_integer_literal, parse_StringLiteral}, math::parse_math_operation, tree::ASTTreeNode, types::parse_type_declaration, var::decl::parse_variable_declaration}}; pub mod tree; pub mod func; @@ -22,7 +22,7 @@ pub mod types; pub fn parse_ast_value_post_l(tokens: &Vec, ind: &mut usize, original: PositionedResult>, invoked_on_body: bool) -> PositionedResult> { match &tokens[*ind].tok_type { - LexerTokenType::DOT => { + LexerTokenType::Dot => { let o = &original?; let k = Box::new(ASTTreeNode::clone(o.as_ref())); @@ -42,14 +42,14 @@ pub fn parse_ast_value_post_l(tokens: &Vec, ind: &mut usize, origina return Err(tokens[*ind].make_err("Invalid token type to use dot access!")); }, - LexerTokenType::MATH_OPERATOR(_, _) => { + LexerTokenType::MathOperator(_, _) => { let o = &original?; let k = Box::new(ASTTreeNode::clone(o.as_ref())); return Ok(parse_math_operation(tokens, ind, k, invoked_on_body)?); }, - LexerTokenType::COMPARING_OPERATOR(op) => { + LexerTokenType::ComparingOperator(op) => { let operator = op.clone(); let o = &original?; @@ -68,7 +68,7 @@ pub fn parse_ast_value_post_l(tokens: &Vec, ind: &mut usize, origina pub fn parse_ast_value(tokens: &Vec, ind: &mut usize) -> PositionedResult> { match &tokens[*ind].tok_type { - LexerTokenType::EXCLAMATION_MARK => { + LexerTokenType::ExclamationMark => { *ind += 1; let ast = parse_ast_value(tokens, ind)?; @@ -79,18 +79,18 @@ pub fn parse_ast_value(tokens: &Vec, ind: &mut usize) -> PositionedR return Err(tokens[*ind].make_err("Boolean negative requires either func or var access!")); }, - LexerTokenType::INT_LIT(_) => { + LexerTokenType::IntLit(_) => { let int = parse_integer_literal(tokens, ind); return parse_ast_value_post_l(tokens, ind, int, false); }, - LexerTokenType::STRING_LIT(_) => { - let str = parse_string_literal(tokens, ind); + LexerTokenType::StringLit(_) => { + let str = parse_StringLiteral(tokens, ind); return parse_ast_value_post_l(tokens, ind, str, false); }, LexerTokenType::KEYWORD(str, _) => { - if tokens[*ind + 1].tok_type == LexerTokenType::PAREN_OPEN { + if tokens[*ind + 1].tok_type == LexerTokenType::ParenOpen { let call = parse_function_call(tokens, ind); return parse_ast_value_post_l(tokens, ind, call, false); } @@ -108,15 +108,15 @@ pub fn parse_ast_value(tokens: &Vec, ind: &mut usize) -> PositionedR pub fn parse_ast_node(tokens: &Vec, ind: &mut usize) -> PositionedResult> { match &tokens[*ind].tok_type { - LexerTokenType::FUNCTION => { + LexerTokenType::Function => { return parse_function_declaraction(tokens, ind); }, - LexerTokenType::STRUCT => { + LexerTokenType::Struct => { return parse_type_declaration(tokens, ind, false); }, - LexerTokenType::LAYOUT => { + LexerTokenType::Layout => { return parse_type_declaration(tokens, ind, true); }, @@ -131,24 +131,24 @@ pub fn parse_ast_node_in_body(tokens: &Vec, ind: &mut usize) -> Posi match &tokens[*ind].tok_type { - LexerTokenType::VAR => { + LexerTokenType::Var => { return parse_variable_declaration(tokens, ind); }, - LexerTokenType::IF => { + LexerTokenType::If => { return parse_if_statement(tokens, ind); }, - LexerTokenType::WHILE => { + LexerTokenType::While => { return parse_while_block(tokens, ind); }, - LexerTokenType::FOR => { + LexerTokenType::For => { return parse_for_loop(tokens, ind); } LexerTokenType::KEYWORD(str, _) => { - if tokens[*ind + 1].tok_type == LexerTokenType::PAREN_OPEN { + if tokens[*ind + 1].tok_type == LexerTokenType::ParenOpen { let call = parse_function_call(tokens, ind); return parse_ast_value_post_l(tokens, ind, call, true); } diff --git a/parser/src/ast/types/mod.rs b/parser/src/ast/types/mod.rs index a915df2..1ec620d 100644 --- a/parser/src/ast/types/mod.rs +++ b/parser/src/ast/types/mod.rs @@ -23,13 +23,13 @@ pub fn parse_type_declaration(tokens: &Vec, ind: &mut usize, layout: let typeName = tokens[*ind].expects_keyword()?; *ind += 1; - tokens[*ind].expects(LexerTokenType::BRACKET_OPEN)?; + tokens[*ind].expects(LexerTokenType::BracketOpen)?; *ind += 1; let mut members: Vec> = Vec::new(); - while tokens[*ind].tok_type != LexerTokenType::BRACKET_CLOSE { + while tokens[*ind].tok_type != LexerTokenType::BracketClose { members.push(parse_types_field_member(tokens, ind)?); } diff --git a/parser/src/ast/var/decl.rs b/parser/src/ast/var/decl.rs index 9f76eae..df9b9ac 100644 --- a/parser/src/ast/var/decl.rs +++ b/parser/src/ast/var/decl.rs @@ -17,7 +17,7 @@ pub fn parse_variable_declaration(tokens: &Vec, ind: &mut usize) -> let mut val: Option> = None; - if tokens[*ind].tok_type == LexerTokenType::EQUAL_SIGN { + if tokens[*ind].tok_type == LexerTokenType::EqualSign { *ind += 1; val = Some(parse_ast_value(tokens, ind)?); From ea36ca6395a79f72eafc0b6d2b0922fc50be968e Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:16:44 +0100 Subject: [PATCH 09/13] chore: renamed every comparing operator to fit unified case --- lexer/src/lexer.rs | 10 +++++----- lexer/src/toks/comp.rs | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lexer/src/lexer.rs b/lexer/src/lexer.rs index 8b02a2d..062219e 100644 --- a/lexer/src/lexer.rs +++ b/lexer/src/lexer.rs @@ -177,23 +177,23 @@ fn parse_comp_operator(contents: &String, ind: &mut usize, start_pos: Position) return None; } - return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::EQUAL))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::Equal))); }, '>' => { if secondChar == '=' { - return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::HIGHER_EQ))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::HigherEqual))); } - return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::HIGHER))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::Higher))); }, '<' => { if secondChar == '=' { - return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::LOWER_EQ))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::LowerEqual))); } - return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::LOWER))); + return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::Lower))); }, _ => { diff --git a/lexer/src/toks/comp.rs b/lexer/src/toks/comp.rs index facfbb9..d8df3d0 100644 --- a/lexer/src/toks/comp.rs +++ b/lexer/src/toks/comp.rs @@ -3,10 +3,10 @@ /// The different comparing tokens #[derive(Debug, PartialEq, Clone)] pub enum ComparingOperator { - EQUAL, // A == B - N_EQUAL, // A != B - HIGHER, // A > B - HIGHER_EQ, // A >= B - LOWER, // A < B - LOWER_EQ // A <= B + Equal, // A == B + NotEqual, // A != B + Higher, // A > B + HigherEqual, // A >= B + Lower, // A < B + LowerEqual // A <= B } \ No newline at end of file From 589eddcf75a38d56037fb7aa0c109a8d0d0debde Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:20:14 +0100 Subject: [PATCH 10/13] chore: fixed more case issues --- lexer/src/lexer.rs | 26 +++++++++++++------------- lexer/src/token.rs | 6 ++---- parser/src/ast/control/forloop.rs | 2 +- parser/src/ast/control/ifelse.rs | 4 ++-- parser/src/ast/func/decl.rs | 2 +- parser/src/ast/literals.rs | 6 +++--- parser/src/ast/mod.rs | 4 ++-- parser/src/ast/tree.rs | 20 ++++++++++---------- parser/src/ast/types/mod.rs | 2 +- parser/src/ast/var/decl.rs | 2 +- 10 files changed, 36 insertions(+), 38 deletions(-) diff --git a/lexer/src/lexer.rs b/lexer/src/lexer.rs index 062219e..a3331fe 100644 --- a/lexer/src/lexer.rs +++ b/lexer/src/lexer.rs @@ -2,7 +2,7 @@ //! Module containing the core lexer algorithm //! -use std::{arch::naked_asm, fs, hash::{DefaultHasher, Hash, Hasher}, io::Error}; +use std::{fs, hash::{DefaultHasher, Hash, Hasher}}; use commons::Position; @@ -125,9 +125,9 @@ pub fn lexer_parse_file(file_path: &String) -> LexerParseResult> } fn parse_math_operator(contents: &String, ind: &mut usize, start_pos: Position) -> LexerParseResult { - let operatorChar = contents.chars().nth(*ind).unwrap(); + let operator_char = contents.chars().nth(*ind).unwrap(); - let operator = match operatorChar { + let operator = match operator_char { '+' => MathOperator::ADD, '-' => MathOperator::SUBSTRACT, '*' => MathOperator::MULTIPLY, @@ -146,34 +146,34 @@ fn parse_math_operator(contents: &String, ind: &mut usize, start_pos: Position) *ind += 1; } - let mut incrementCount = 1; + let mut increment_count = 1; if assigns { - incrementCount += 1; + increment_count += 1; } - let end = start_pos.increment_by(incrementCount); + let end = start_pos.increment_by(increment_count); return Ok(LexerToken::new(start_pos, end, LexerTokenType::MathOperator(operator, assigns))); } fn parse_comp_operator(contents: &String, ind: &mut usize, start_pos: Position) -> Option { - let firstChar = contents.chars().nth(*ind).unwrap(); + let first_char = contents.chars().nth(*ind).unwrap(); *ind += 1; - let secondChar = contents.chars().nth(*ind).unwrap(); + let second_char = contents.chars().nth(*ind).unwrap(); *ind += 1; let end = start_pos.increment_by(2); - if secondChar != '=' || secondChar != ' ' { + if second_char != '=' || second_char != ' ' { return None; } - match firstChar { + match first_char { '=' => { - if secondChar != '=' { + if second_char != '=' { return None; } @@ -181,7 +181,7 @@ fn parse_comp_operator(contents: &String, ind: &mut usize, start_pos: Position) }, '>' => { - if secondChar == '=' { + if second_char == '=' { return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::HigherEqual))); } @@ -189,7 +189,7 @@ fn parse_comp_operator(contents: &String, ind: &mut usize, start_pos: Position) }, '<' => { - if secondChar == '=' { + if second_char == '=' { return Some(LexerToken::new(start_pos, end, LexerTokenType::ComparingOperator(ComparingOperator::LowerEqual))); } diff --git a/lexer/src/token.rs b/lexer/src/token.rs index 85e237f..3cdc390 100644 --- a/lexer/src/token.rs +++ b/lexer/src/token.rs @@ -2,8 +2,6 @@ //! Module containing lexer token-based utilities and classes //! -use std::any::Any; - use commons::{Position, err::{PositionedError, PositionedResult}}; use crate::{LexerParseResult, LexerParsingError, toks::{comp::ComparingOperator, math::MathOperator}}; @@ -91,7 +89,7 @@ impl LexerToken { return Ok(true); } - pub fn expects_IntLit(&self) -> PositionedResult { + pub fn expects_int_lit(&self) -> PositionedResult { match &self.tok_type { LexerTokenType::IntLit(v) => return Ok(*v), _ => return Err(self.make_err("Expected int litteral here!")) @@ -112,7 +110,7 @@ impl LexerToken { }; } - pub fn expects_StringLit(&self) -> PositionedResult { + pub fn expects_string_lit(&self) -> PositionedResult { match &self.tok_type { LexerTokenType::StringLit(v) => return Ok(v.to_string()), _ => return Err(self.make_err("Expected string litteral here!")) diff --git a/parser/src/ast/control/forloop.rs b/parser/src/ast/control/forloop.rs index f403549..1d5ddce 100644 --- a/parser/src/ast/control/forloop.rs +++ b/parser/src/ast/control/forloop.rs @@ -29,5 +29,5 @@ pub fn parse_for_loop(tokens: &Vec, ind: &mut usize) -> PositionedRe let body = parse_node_body(tokens, ind)?; - return Ok(Box::new(ASTTreeNode::ForBlock { initialState: initial, cond, increment, body })); + return Ok(Box::new(ASTTreeNode::ForBlock { initial_state: initial, cond, increment, body })); } \ No newline at end of file diff --git a/parser/src/ast/control/ifelse.rs b/parser/src/ast/control/ifelse.rs index 8cc88a0..933cbc2 100644 --- a/parser/src/ast/control/ifelse.rs +++ b/parser/src/ast/control/ifelse.rs @@ -40,7 +40,7 @@ pub fn parse_if_statement(tokens: &Vec, ind: &mut usize) -> Position elseStatement = Some(parse_else_statement(tokens, ind)?); } - return Ok(Box::new(ASTTreeNode::IfStatement { cond, body, elseStatement })); + return Ok(Box::new(ASTTreeNode::IfStatement { cond, body, else_statement: elseStatement })); } pub fn parse_else_statement(tokens: &Vec, ind: &mut usize) -> PositionedResult> { @@ -69,7 +69,7 @@ pub fn parse_else_statement(tokens: &Vec, ind: &mut usize) -> Positi elseStatement = Some(parse_else_statement(tokens, ind)?); } - return Ok(Box::new(ASTTreeNode::IfElseStatement { cond, body, elseStatement })); + return Ok(Box::new(ASTTreeNode::IfElseStatement { cond, body, else_statement: elseStatement })); } return Ok(Box::new(ASTTreeNode::ElseStatement { body })); diff --git a/parser/src/ast/func/decl.rs b/parser/src/ast/func/decl.rs index 25c5b16..b9fda9f 100644 --- a/parser/src/ast/func/decl.rs +++ b/parser/src/ast/func/decl.rs @@ -19,5 +19,5 @@ pub fn parse_function_declaraction(tokens: &Vec, ind: &mut usize) -> let body = parse_node_body(tokens, ind)?; - return Ok(Box::new(ASTTreeNode::FunctionDeclaration { funcName: WithHash::new(functionName.0), args, body })); + return Ok(Box::new(ASTTreeNode::FunctionDeclaration { func_name: WithHash::new(functionName.0), args, body })); } \ No newline at end of file diff --git a/parser/src/ast/literals.rs b/parser/src/ast/literals.rs index a269ee7..d5f74ac 100644 --- a/parser/src/ast/literals.rs +++ b/parser/src/ast/literals.rs @@ -4,14 +4,14 @@ use lexer::token::{LexerToken}; use crate::{ast::tree::ASTTreeNode}; pub fn parse_integer_literal(tokens: &Vec, ind: &mut usize) -> PositionedResult> { - let val = tokens[*ind].expects_IntLit()?; + let val = tokens[*ind].expects_int_lit()?; *ind += 1; return Ok(Box::new(ASTTreeNode::IntegerLit(val))); } -pub fn parse_StringLiteral(tokens: &Vec, ind: &mut usize) -> PositionedResult> { - let val = tokens[*ind].expects_StringLit()?; +pub fn parse_string_literal(tokens: &Vec, ind: &mut usize) -> PositionedResult> { + let val = tokens[*ind].expects_string_lit()?; *ind += 1; return Ok(Box::new(ASTTreeNode::StringLit(val))); diff --git a/parser/src/ast/mod.rs b/parser/src/ast/mod.rs index c00cd8b..50d6aef 100644 --- a/parser/src/ast/mod.rs +++ b/parser/src/ast/mod.rs @@ -10,7 +10,7 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; -use crate::{ast::{control::{forloop::parse_for_loop, ifelse::parse_if_statement, whileblock::parse_while_block}, func::{call::parse_function_call, decl::parse_function_declaraction}, literals::{parse_integer_literal, parse_StringLiteral}, math::parse_math_operation, tree::ASTTreeNode, types::parse_type_declaration, var::decl::parse_variable_declaration}}; +use crate::{ast::{control::{forloop::parse_for_loop, ifelse::parse_if_statement, whileblock::parse_while_block}, func::{call::parse_function_call, decl::parse_function_declaraction}, literals::{parse_integer_literal, parse_string_literal}, math::parse_math_operation, tree::ASTTreeNode, types::parse_type_declaration, var::decl::parse_variable_declaration}}; pub mod tree; pub mod func; @@ -85,7 +85,7 @@ pub fn parse_ast_value(tokens: &Vec, ind: &mut usize) -> PositionedR }, LexerTokenType::StringLit(_) => { - let str = parse_StringLiteral(tokens, ind); + let str = parse_string_literal(tokens, ind); return parse_ast_value_post_l(tokens, ind, str, false); }, diff --git a/parser/src/ast/tree.rs b/parser/src/ast/tree.rs index 7daa3a3..de1280d 100644 --- a/parser/src/ast/tree.rs +++ b/parser/src/ast/tree.rs @@ -8,12 +8,12 @@ use utils::hash::{TypeHash, WithHash}; #[derive(Debug, PartialEq, Clone)] pub struct FunctionDeclarationArgument { pub name: WithHash, - pub argumentType: TypeHash + pub argument_type: TypeHash } impl FunctionDeclarationArgument { - pub fn new(name: String, argType: TypeHash) -> Self { - FunctionDeclarationArgument { name: WithHash::new(name), argumentType: argType } + pub fn new(name: String, arg_type: TypeHash) -> Self { + FunctionDeclarationArgument { name: WithHash::new(name), argument_type: arg_type } } } @@ -30,23 +30,23 @@ pub enum ASTTreeNode { VariableReference(WithHash), StructLayoutDeclaration { name: WithHash, layout: bool, members: Vec> }, - StructFieldMember { name: WithHash, memberType: TypeHash }, + StructFieldMember { name: WithHash, member_type: TypeHash }, - VarDeclaration { varName: WithHash, varType: TypeHash, value: Option> }, + VarDeclaration { var_name: WithHash, var_type: TypeHash, value: Option> }, VarValueChange { var: Box, value: Box }, - VarIncrement { var: Box, incrementBy: Option> }, // Default is by 1 + VarIncrement { var: Box, increment_by: Option> }, // Default is by 1 - IfStatement { cond: Box, body: Vec>, elseStatement: Option> }, - IfElseStatement { cond: Option>, body: Vec>, elseStatement: Option> }, + IfStatement { cond: Box, body: Vec>, else_statement: Option> }, + IfElseStatement { cond: Option>, body: Vec>, else_statement: Option> }, ElseStatement { body: Vec> }, WhileBlock { cond: Box, body: Vec> }, - ForBlock { initialState: Box, cond: Box, increment: Box, body: Vec> }, + ForBlock { initial_state: Box, cond: Box, increment: Box, body: Vec> }, Return { value: Option> }, FunctionCall { func: WithHash, args: Vec> }, - FunctionDeclaration { funcName: WithHash, args: Vec, body: Vec> }, + FunctionDeclaration { func_name: WithHash, args: Vec, body: Vec> }, StructLRVariable { l: Box, r: Box,}, StructLRFunction { l: Box, r: Box, } diff --git a/parser/src/ast/types/mod.rs b/parser/src/ast/types/mod.rs index 1ec620d..b2868b3 100644 --- a/parser/src/ast/types/mod.rs +++ b/parser/src/ast/types/mod.rs @@ -14,7 +14,7 @@ pub fn parse_types_field_member(tokens: &Vec, ind: &mut usize) -> Po *ind += 1; - return Ok(Box::new(ASTTreeNode::StructFieldMember { name: WithHash::new(fieldName.0), memberType: typeName.1 })) + return Ok(Box::new(ASTTreeNode::StructFieldMember { name: WithHash::new(fieldName.0), member_type: typeName.1 })) } pub fn parse_type_declaration(tokens: &Vec, ind: &mut usize, layout: bool) -> PositionedResult> { diff --git a/parser/src/ast/var/decl.rs b/parser/src/ast/var/decl.rs index df9b9ac..4bcb71e 100644 --- a/parser/src/ast/var/decl.rs +++ b/parser/src/ast/var/decl.rs @@ -23,5 +23,5 @@ pub fn parse_variable_declaration(tokens: &Vec, ind: &mut usize) -> val = Some(parse_ast_value(tokens, ind)?); } - return Ok(Box::new(ASTTreeNode::VarDeclaration { varName: WithHash::new(varName.0), varType: typeName.1, value: val })); + return Ok(Box::new(ASTTreeNode::VarDeclaration { var_name: WithHash::new(varName.0), var_type: typeName.1, value: val })); } \ No newline at end of file From ce507ac653504ae7408ca4a490e86047549b0d22 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:22:53 +0100 Subject: [PATCH 11/13] chore: fixed warnings --- commons/src/err/mod.rs | 2 +- commons/src/lib.rs | 2 +- parser/src/ast/control/ifelse.rs | 12 ++++++------ parser/src/ast/func/decl.rs | 4 ++-- parser/src/ast/func/mod.rs | 6 +++--- parser/src/ast/math.rs | 4 ++-- parser/src/ast/types/mod.rs | 10 +++++----- parser/src/ast/var/decl.rs | 6 +++--- test-main/src/main.rs | 6 +++--- utils/src/hash.rs | 3 +-- 10 files changed, 27 insertions(+), 28 deletions(-) diff --git a/commons/src/err/mod.rs b/commons/src/err/mod.rs index 0cef6bd..357d4c9 100644 --- a/commons/src/err/mod.rs +++ b/commons/src/err/mod.rs @@ -35,7 +35,7 @@ impl fmt::Display for PositionedError { let line = match self.start.get_line_content() { Ok(v) => v, - Err(e) => format!("{}","Couldn't read file contents!".red().bold()) + Err(e) => format!("{}: {}","Couldn't read file contents!".red().bold(), e) }; let before = &line[0..self.start.col - 1]; diff --git a/commons/src/lib.rs b/commons/src/lib.rs index 3f7e032..1a81253 100644 --- a/commons/src/lib.rs +++ b/commons/src/lib.rs @@ -32,7 +32,7 @@ impl Position { impl fmt::Display for Position { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}:{} in {}", self.line, self.col, self.file_path); + let _ = write!(f, "{}:{} in {}", self.line, self.col, self.file_path); Ok(()) } diff --git a/parser/src/ast/control/ifelse.rs b/parser/src/ast/control/ifelse.rs index 933cbc2..fc90cf1 100644 --- a/parser/src/ast/control/ifelse.rs +++ b/parser/src/ast/control/ifelse.rs @@ -32,15 +32,15 @@ pub fn parse_if_statement(tokens: &Vec, ind: &mut usize) -> Position Err(e) => return Err(e) }; - let mut elseStatement = None; + let mut else_statement = None; if tokens[*ind + 1].tok_type == LexerTokenType::Else { *ind += 1; - elseStatement = Some(parse_else_statement(tokens, ind)?); + else_statement = Some(parse_else_statement(tokens, ind)?); } - return Ok(Box::new(ASTTreeNode::IfStatement { cond, body, else_statement: elseStatement })); + return Ok(Box::new(ASTTreeNode::IfStatement { cond, body, else_statement })); } pub fn parse_else_statement(tokens: &Vec, ind: &mut usize) -> PositionedResult> { @@ -61,15 +61,15 @@ pub fn parse_else_statement(tokens: &Vec, ind: &mut usize) -> Positi }; if cond.is_some() { - let mut elseStatement = None; + let mut else_statement = None; if tokens[*ind + 1].tok_type == LexerTokenType::Else { *ind += 1; - elseStatement = Some(parse_else_statement(tokens, ind)?); + else_statement = Some(parse_else_statement(tokens, ind)?); } - return Ok(Box::new(ASTTreeNode::IfElseStatement { cond, body, else_statement: elseStatement })); + return Ok(Box::new(ASTTreeNode::IfElseStatement { cond, body, else_statement: else_statement })); } return Ok(Box::new(ASTTreeNode::ElseStatement { body })); diff --git a/parser/src/ast/func/decl.rs b/parser/src/ast/func/decl.rs index b9fda9f..5df13e1 100644 --- a/parser/src/ast/func/decl.rs +++ b/parser/src/ast/func/decl.rs @@ -6,7 +6,7 @@ use crate::{ast::{func::{parse_function_arguments, parse_node_body}, tree::ASTTr pub fn parse_function_declaraction(tokens: &Vec, ind: &mut usize) -> PositionedResult> { *ind += 1; - let functionName = tokens[*ind].expects_keyword()?; + let function_name = tokens[*ind].expects_keyword()?; *ind += 1; tokens[*ind].expects(LexerTokenType::ParenOpen)?; @@ -19,5 +19,5 @@ pub fn parse_function_declaraction(tokens: &Vec, ind: &mut usize) -> let body = parse_node_body(tokens, ind)?; - return Ok(Box::new(ASTTreeNode::FunctionDeclaration { func_name: WithHash::new(functionName.0), args, body })); + return Ok(Box::new(ASTTreeNode::FunctionDeclaration { func_name: WithHash::new(function_name.0), args, body })); } \ No newline at end of file diff --git a/parser/src/ast/func/mod.rs b/parser/src/ast/func/mod.rs index c982c26..f719479 100644 --- a/parser/src/ast/func/mod.rs +++ b/parser/src/ast/func/mod.rs @@ -31,12 +31,12 @@ pub fn parse_function_arguments(tokens: &Vec, ind: &mut usize) -> Po let mut args: Vec = Vec::new(); while *ind < tokens.len() && tokens[*ind].is_keyword() { - let varType = tokens[*ind].expects_keyword()?; + let var_type = tokens[*ind].expects_keyword()?; *ind += 1; - let varName = tokens[*ind].expects_keyword()?; + let var_name = tokens[*ind].expects_keyword()?; - args.push(FunctionDeclarationArgument::new(varName.0, varType.1)); + args.push(FunctionDeclarationArgument::new(var_name.0, var_type.1)); *ind += 1; diff --git a/parser/src/ast/math.rs b/parser/src/ast/math.rs index 82aa244..fb2a4e5 100644 --- a/parser/src/ast/math.rs +++ b/parser/src/ast/math.rs @@ -12,7 +12,7 @@ pub fn parse_math_operation(tokens: &Vec, ind: &mut usize, original: *ind += 1; - let rightMember = parse_ast_value(tokens, ind)?; + let right_member = parse_ast_value(tokens, ind)?; - return Ok(Box::new(ASTTreeNode::MathResult { lval: original, rval: rightMember, operator: oper.0, assigns: oper.1 })) + return Ok(Box::new(ASTTreeNode::MathResult { lval: original, rval: right_member, operator: oper.0, assigns: oper.1 })) } \ No newline at end of file diff --git a/parser/src/ast/types/mod.rs b/parser/src/ast/types/mod.rs index b2868b3..e60e6c4 100644 --- a/parser/src/ast/types/mod.rs +++ b/parser/src/ast/types/mod.rs @@ -6,21 +6,21 @@ use crate::ast::tree::ASTTreeNode; /// Parses a struct/layout member (field) pub fn parse_types_field_member(tokens: &Vec, ind: &mut usize) -> PositionedResult> { - let typeName = tokens[*ind].expects_keyword()?; + let type_name = tokens[*ind].expects_keyword()?; *ind += 1; - let fieldName = tokens[*ind].expects_keyword()?; + let field_name = tokens[*ind].expects_keyword()?; *ind += 1; - return Ok(Box::new(ASTTreeNode::StructFieldMember { name: WithHash::new(fieldName.0), member_type: typeName.1 })) + return Ok(Box::new(ASTTreeNode::StructFieldMember { name: WithHash::new(field_name.0), member_type: type_name.1 })) } pub fn parse_type_declaration(tokens: &Vec, ind: &mut usize, layout: bool) -> PositionedResult> { *ind += 1; - let typeName = tokens[*ind].expects_keyword()?; + let type_name = tokens[*ind].expects_keyword()?; *ind += 1; tokens[*ind].expects(LexerTokenType::BracketOpen)?; @@ -33,5 +33,5 @@ pub fn parse_type_declaration(tokens: &Vec, ind: &mut usize, layout: members.push(parse_types_field_member(tokens, ind)?); } - return Ok(Box::new(ASTTreeNode::StructLayoutDeclaration { name: WithHash::new(typeName.0), layout, members })); + return Ok(Box::new(ASTTreeNode::StructLayoutDeclaration { name: WithHash::new(type_name.0), layout, members })); } \ No newline at end of file diff --git a/parser/src/ast/var/decl.rs b/parser/src/ast/var/decl.rs index 4bcb71e..be949eb 100644 --- a/parser/src/ast/var/decl.rs +++ b/parser/src/ast/var/decl.rs @@ -7,11 +7,11 @@ use crate::{ast::{parse_ast_value, tree::ASTTreeNode}}; pub fn parse_variable_declaration(tokens: &Vec, ind: &mut usize) -> PositionedResult> { *ind += 1; - let typeName = tokens[*ind].expects_keyword()?; + let type_name = tokens[*ind].expects_keyword()?; *ind += 1; - let varName = tokens[*ind].expects_keyword()?; + let var_name = tokens[*ind].expects_keyword()?; *ind += 1; @@ -23,5 +23,5 @@ pub fn parse_variable_declaration(tokens: &Vec, ind: &mut usize) -> val = Some(parse_ast_value(tokens, ind)?); } - return Ok(Box::new(ASTTreeNode::VarDeclaration { var_name: WithHash::new(varName.0), var_type: typeName.1, value: val })); + return Ok(Box::new(ASTTreeNode::VarDeclaration { var_name: WithHash::new(var_name.0), var_type: type_name.1, value: val })); } \ No newline at end of file diff --git a/test-main/src/main.rs b/test-main/src/main.rs index 4eee812..dd20aeb 100644 --- a/test-main/src/main.rs +++ b/test-main/src/main.rs @@ -1,13 +1,13 @@ -use std::{env, hash::{DefaultHasher, Hash, Hasher}}; +use std::{env}; use lexer::lexer::lexer_parse_file; use parser::ast::parse_ast_node; fn main() { let args: Vec = env::args().collect(); - let filePath: &String = &args[1]; + let file_path: &String = &args[1]; - let lexer_res = lexer_parse_file(filePath).expect("Bad lexer!"); + let lexer_res = lexer_parse_file(file_path).expect("Bad lexer!"); let mut ind = 0; diff --git a/utils/src/hash.rs b/utils/src/hash.rs index 8df23c3..bac5f7a 100644 --- a/utils/src/hash.rs +++ b/utils/src/hash.rs @@ -2,8 +2,7 @@ //! Hash related utilities //! -use core::hash; -use std::{any::TypeId, hash::{DefaultHasher, Hash, Hasher}}; +use std::{hash::{DefaultHasher, Hash, Hasher}}; pub type TypeHash = u64; From e836fd1238000d76d77f7f1734dbb2f5b8c91fa2 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 01:44:31 +0100 Subject: [PATCH 12/13] chore: added some docse --- parser/src/ast/func/mod.rs | 2 ++ parser/src/ast/literals.rs | 2 ++ parser/src/ast/math.rs | 2 ++ parser/src/ast/mod.rs | 60 +++++++++++++++++++++++++++++++++++-- parser/src/ast/tree.rs | 1 + parser/src/ast/types/mod.rs | 2 ++ parser/src/ast/var/mod.rs | 2 ++ parser/src/lib.rs | 7 +++++ 8 files changed, 75 insertions(+), 3 deletions(-) diff --git a/parser/src/ast/func/mod.rs b/parser/src/ast/func/mod.rs index f719479..7926fb9 100644 --- a/parser/src/ast/func/mod.rs +++ b/parser/src/ast/func/mod.rs @@ -1,3 +1,5 @@ +//! AST parsing for function related elements (function declarations, arguments, calls, ...) + use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; diff --git a/parser/src/ast/literals.rs b/parser/src/ast/literals.rs index d5f74ac..b701102 100644 --- a/parser/src/ast/literals.rs +++ b/parser/src/ast/literals.rs @@ -1,3 +1,5 @@ +//! AST parsing for number & string literals + use commons::err::{PositionedResult}; use lexer::token::{LexerToken}; diff --git a/parser/src/ast/math.rs b/parser/src/ast/math.rs index fb2a4e5..49961ca 100644 --- a/parser/src/ast/math.rs +++ b/parser/src/ast/math.rs @@ -1,3 +1,5 @@ +//! AST parsing for math related elements (math operations, ...) + use commons::err::PositionedResult; use lexer::token::LexerToken; diff --git a/parser/src/ast/mod.rs b/parser/src/ast/mod.rs index 50d6aef..8542055 100644 --- a/parser/src/ast/mod.rs +++ b/parser/src/ast/mod.rs @@ -3,7 +3,18 @@ //! The AST parsing is responsible for putting tokens into structures such as functions and other stuff. //! It is an extremely important step. //! -//! Indexes passed to parsing functions SHOULD be the "detected" token rather than the next one. +//! # Parsing +//! Parsing the first AST node found in the lexer token buffer can be done using `parse_ast_node`. +//! +//! Parsing inside of an AST node body (eg functions, if statements, ...) can be done using `parse_ast_node_in_body` +//! +//! # Design notes +//! The initial index represents the given value of the index value (usually `ind`) passed to any AST parsing function. +//! +//! Whenever any AST parsing function requires a keyword (for example `var`) to be called, the initial index should be the index representing said keyword and not the next one. +//! Furthermore, AST parsing functions requiring a keyword should increment index by one at the start to skip the detected keyword if needed. +//! +//! AST parsing functions that aren't requiring a keyword will start at the actual start of the expression unless stated otherwise. //! use commons::err::PositionedResult; @@ -20,6 +31,25 @@ pub mod control; pub mod math; pub mod types; +/// Parses the post side of an AST node that can and WILL be intrepreted as a value. +/// +/// This function should only be called by `parse_ast_value` +/// +/// # Parsing Layout +/// The `parse_ast_value` function only parses the post side of the expression (noted L) if expression is: +/// `R (pre) expression L (post) expression` +/// +/// This layout allows us to seperate parsing from things like variable references, functions calls or even literals and +/// treat them as the same while parsing other elements such as math operations or conditions! +/// +/// # Possible node results +/// `parse_ast_value_post_l` can possibly return the following node types: +/// - original type +/// - variable / function on type access +/// - math operation +/// - comparing +/// - boolean negation +/// pub fn parse_ast_value_post_l(tokens: &Vec, ind: &mut usize, original: PositionedResult>, invoked_on_body: bool) -> PositionedResult> { match &tokens[*ind].tok_type { LexerTokenType::Dot => { @@ -65,6 +95,24 @@ pub fn parse_ast_value_post_l(tokens: &Vec, ind: &mut usize, origina } } +/// Parses an AST node that can and WILL be intrepreted as a value +/// +/// # Parsing Layout +/// The `parse_ast_value` function only parses the pre side of the expression (noted R) if expression is: +/// `R (pre) expression L (post) expression` +/// +/// This layout allows us to seperate parsing from things like variable references, functions calls or even literals and +/// treat them as the same while parsing other elements such as math operations or conditions! +/// +/// This function will call `parse_ast_value_post_l` to parse the L part of the expression. +/// +/// # Recognized Nodes +/// Possible nodes recognized as values include: +/// - Function calls +/// - Variable refs +/// - Math operation results (both with or without value changing) +/// - Boolean negation result +/// - Boolean compare result pub fn parse_ast_value(tokens: &Vec, ind: &mut usize) -> PositionedResult> { match &tokens[*ind].tok_type { @@ -106,6 +154,13 @@ pub fn parse_ast_value(tokens: &Vec, ind: &mut usize) -> PositionedR } } +/// Parses an AST node outside of any other node. +/// +/// # Examples +/// `parse_ast_node` is used to parse: +/// - Function declarations +/// - Struct declarations +/// - Layout declarations pub fn parse_ast_node(tokens: &Vec, ind: &mut usize) -> PositionedResult> { match &tokens[*ind].tok_type { LexerTokenType::Function => { @@ -126,9 +181,8 @@ pub fn parse_ast_node(tokens: &Vec, ind: &mut usize) -> PositionedRe } } +/// Parses an AST node inside of another compatible node (functions, control bodies) pub fn parse_ast_node_in_body(tokens: &Vec, ind: &mut usize) -> PositionedResult> { - println!("Ind: {}, tok at: {:#?}", ind, tokens[*ind].tok_type); - match &tokens[*ind].tok_type { LexerTokenType::Var => { diff --git a/parser/src/ast/tree.rs b/parser/src/ast/tree.rs index de1280d..396d180 100644 --- a/parser/src/ast/tree.rs +++ b/parser/src/ast/tree.rs @@ -17,6 +17,7 @@ impl FunctionDeclarationArgument { } } +/// The main AST node type in the AST parsing system. #[derive(Debug, PartialEq, Clone)] pub enum ASTTreeNode { IntegerLit(i64), diff --git a/parser/src/ast/types/mod.rs b/parser/src/ast/types/mod.rs index e60e6c4..90387dc 100644 --- a/parser/src/ast/types/mod.rs +++ b/parser/src/ast/types/mod.rs @@ -1,3 +1,5 @@ +//! AST parsing for struct & layout parsing + use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; use utils::hash::WithHash; diff --git a/parser/src/ast/var/mod.rs b/parser/src/ast/var/mod.rs index ac63c09..96ed48d 100644 --- a/parser/src/ast/var/mod.rs +++ b/parser/src/ast/var/mod.rs @@ -1 +1,3 @@ +//! AST parsing for variable related elements (variable declarations, modifications, ...) + pub mod decl; \ No newline at end of file diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 851c0bc..10b14b0 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -1 +1,8 @@ +//! +//! The parsing module of Quickfall. Contains all of the parsing code required for the Quickfall language. +//! +//! # Introduction +//! The `parser` module mostly contains the AST processor for Quickfall. Every element of the language is represented as an AST node which is then passed onto the AST tree. +//! The AST tree is then sent to the IR writer to actually compile. + pub mod ast; From 321ff94c070a400206c08a46355beb99e2d38c2b Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Tue, 10 Feb 2026 23:00:45 +0100 Subject: [PATCH 13/13] fix: for loop parsing --- commons/src/err/mod.rs | 6 ++++-- examples/for.qf | 6 ++++-- lexer/src/lexer.rs | 18 +++++++++--------- lexer/src/token.rs | 1 + parser/src/ast/control/forloop.rs | 6 ++---- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/commons/src/err/mod.rs b/commons/src/err/mod.rs index 357d4c9..4777486 100644 --- a/commons/src/err/mod.rs +++ b/commons/src/err/mod.rs @@ -38,9 +38,11 @@ impl fmt::Display for PositionedError { Err(e) => format!("{}: {}","Couldn't read file contents!".red().bold(), e) }; + println!("sz: {}, start: {}, end: {}", line.len(), self.start.col - 1, self.end.col - 1); + let before = &line[0..self.start.col - 1]; - let target = &line[self.start.col - 1..self.end.col].cyan().underline(); - let after = &line[self.end.col..]; + let target = &line[self.start.col - 1..self.end.col - 1].cyan().underline(); + let after = &line[self.end.col - 1..]; writeln!(f, "{}{}{}", before, target, after)?; writeln!(f, "")?; diff --git a/examples/for.qf b/examples/for.qf index ae71037..8aa5710 100644 --- a/examples/for.qf +++ b/examples/for.qf @@ -1,3 +1,5 @@ -for(si32 test = 0, test <= 10, test += 1) { - +func test() { + for(si32 test = 0, test <= 10, test += 1) { + + } } \ No newline at end of file diff --git a/lexer/src/lexer.rs b/lexer/src/lexer.rs index a3331fe..72ce9ae 100644 --- a/lexer/src/lexer.rs +++ b/lexer/src/lexer.rs @@ -46,34 +46,34 @@ pub fn lexer_parse_file(file_path: &String) -> LexerParseResult> let c: char = contents.chars().nth(i).unwrap(); if c == '\n' { - i += c.len_utf8(); + i += 1; last_line_break = i; line += 1; continue; } if c.is_numeric() { - let col = i - last_line_break + 1; + let col = i - last_line_break; tokens.push(parse_number_token(&contents, &mut i, Position::new(file_path.to_string(), line, col))?); continue; } if c == '"' { - let col = i - last_line_break + 1; + let col = i - last_line_break; tokens.push(parse_string_token(&contents, &mut i, Position::new(file_path.to_string(), line, col))); continue; } if c.is_alphabetic() { - let col = i - last_line_break + 1; + let col = i - last_line_break; tokens.push(parse_keyword(&contents, &mut i, Position::new(file_path.to_string(), line, col))); continue; } if c == '+' || c == '-' || c == '*' || c == '/' { - let col = i - last_line_break + 1; + let col = i - last_line_break; tokens.push(parse_math_operator(&contents, &mut i, Position::new(file_path.to_string(), line, col))?); @@ -81,7 +81,7 @@ pub fn lexer_parse_file(file_path: &String) -> LexerParseResult> } if c == '=' || c == '>' || c == '<' { - let col = i - last_line_break + 1; + let col = i - last_line_break; let parse = parse_comp_operator(&contents, &mut i, Position::new(file_path.to_string(), line, col)); @@ -93,10 +93,10 @@ pub fn lexer_parse_file(file_path: &String) -> LexerParseResult> i -= 2; // Try parsing operator as normal token. } - i += c.len_utf8(); + i += 1; - let col = i - last_line_break + 1; + let col = i - last_line_break; let pos = Position::new(file_path.to_string(), line, col); @@ -167,7 +167,7 @@ fn parse_comp_operator(contents: &String, ind: &mut usize, start_pos: Position) let end = start_pos.increment_by(2); - if second_char != '=' || second_char != ' ' { + if second_char != '=' && second_char != ' ' { return None; } diff --git a/lexer/src/token.rs b/lexer/src/token.rs index 3cdc390..b1a6387 100644 --- a/lexer/src/token.rs +++ b/lexer/src/token.rs @@ -61,6 +61,7 @@ pub enum LexerTokenType { EndOfFile } +#[derive(Debug)] pub struct LexerToken { pub tok_type: LexerTokenType, pub pos: Position, // Valid tokens require a position diff --git a/parser/src/ast/control/forloop.rs b/parser/src/ast/control/forloop.rs index 1d5ddce..e4a59de 100644 --- a/parser/src/ast/control/forloop.rs +++ b/parser/src/ast/control/forloop.rs @@ -1,7 +1,7 @@ use commons::err::PositionedResult; use lexer::token::{LexerToken, LexerTokenType}; -use crate::{ast::{func::parse_node_body, parse_ast_node, parse_ast_value, tree::ASTTreeNode, var::decl::parse_variable_declaration}}; +use crate::ast::{func::parse_node_body, parse_ast_node, parse_ast_node_in_body, parse_ast_value, tree::ASTTreeNode, var::decl::parse_variable_declaration}; pub fn parse_for_loop(tokens: &Vec, ind: &mut usize) -> PositionedResult> { *ind += 1; @@ -18,9 +18,7 @@ pub fn parse_for_loop(tokens: &Vec, ind: &mut usize) -> PositionedRe tokens[*ind].expects(LexerTokenType::Comma)?; *ind += 1; - let increment = parse_ast_node(tokens, ind)?; - - *ind += 1; + let increment = parse_ast_node_in_body(tokens, ind)?; tokens[*ind].expects(LexerTokenType::ParenClose)?; *ind += 1;