refactor: reorganize expression parser with proper precedence hierarchy #254
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR significantly refactors the expression parsing logic to implement a standard C-style operator precedence hierarchy. It replaces the previous parsing structure with a dedicated recursive descent approach, ensuring that complex expressions are evaluated in the correct order. Additionally, it introduces support for binary literals and fixes several type-related issues in the LLVM code generation for logical and bitwise operations.
Key Changes
1. Expression Parsing & Precedence
!,~,&,deref) into a dedicated recursive function, allowing for nested unary expressions (e.g.,!!x).Expression::Groupedto properly handle and preserve parenthesized sub-expressions.2. Lexer Improvements
0bprefix (e.g.,0b1010), usingfrom_str_radixfor accurate conversion toi64.<<and>>from keyword-based matching to character-level matching in the lexer. Fixed the collision where<was matched before<<.3. LLVM Code Generation & Type Safety
to_bool()helper to handle integer-to-boolean coercion, ensuring logicalAND/ORoperations work correctly with multi-bit integers.BitwiseAnd,BitwiseOr, andBitwiseXorusing native LLVM instructions.LogicalNot(!) to correctly compare integers with zero.BitwiseNot(~) using the LLVMnot(XOR with -1) logic.4. Code Quality & Consistency
Operator::Notfor consistency across unary operations.std::iter::Peekablefor better readability and performance.Operator Precedence (Highest to Lowest)
()!,~,&,deref*,/,%+,-<<,>><,<=,>,>===,!=&^|&&||=,+=,-=,*=,/=,%=Examples of Improved Behavior
Benefits