Skip to content

Conversation

@LunaStev
Copy link
Member

@LunaStev LunaStev commented Dec 25, 2025

This PR introduces support for standard C-style increment (++) and decrement (--) operators in both prefix and postfix forms. Additionally, it implements a new semantic verification layer to enforce mutability rules, ensuring that variables are only modified when explicitly declared as mutable.

Key Changes

1. AST & Parser Enhancements

  • Increment/Decrement: Added IncDec expression variant and IncDecKind (PreInc, PreDec, PostInc, PostDec) to the AST.
  • Operator Parsing:
    • Updated parse_unary_expression to handle prefix operators.
    • Enhanced parse_expression with lookahead logic to identify postfix operators.
  • L-Value Validation: Implemented is_assignable checks to ensure ++/-- are only applied to valid targets (variables, array indices, struct fields, and dereferenced pointers).

2. New Semantic Verification Module

  • Mutability Pass: Introduced a verification module that performs a full AST traversal before code generation.
  • Scope Tracking: The validator tracks variable bindings and their Mutability status.
  • Safety Enforcement: The compiler now produces errors if a modification (assignment or inc/dec) is attempted on an immutable binding (e.g., let x = 5; x++; will now fail during semantic analysis).

3. LLVM Code Generation

  • Inc/Dec Logic: Implemented the load -> add/sub -> store pattern for all supported types:
    • Integers: Standard integer arithmetic.
    • Floats: Floating-point arithmetic.
    • Pointers: Pointer arithmetic (incrementing by the size of the underlying type).
  • Pre/Post Behavior: Correctly handles the return value based on timing (prefix returns the modified value; postfix returns the original value).
  • Codegen Fixes: Improved generate_address_ir to robustly handle grouped expressions and complex dereferences.

4. Testing & Documentation

  • Integration Tests: Added test72.wave and test73.wave.
  • Coverage: Tests verify increment/decrement behavior across basic types and pointer dereferences, as well as the newly implemented mutability constraints.

Example Usage

var count: i32 = 10;
count++;       // Post-increment: returns 10, count becomes 11
++count;       // Pre-increment: returns 12, count becomes 12

let mut p: ptr<i32> = &count;
deref p += 1;  // Dereference modification
(deref p)++;   // Incrementing the value pointed to

Benefits

  • Expressiveness: Adds common shorthand operators expected in C-family languages.
  • Safety: Strengthens type safety by preventing accidental modifications of immutable data.
  • Consistency: Ensures pointer arithmetic behaves consistently with standard integer types.

Add support for pre/post-increment (`++`) and pre/post-decrement (`--`)
operators, along with a new semantic validation pass.

Changes:
- AST:
  - Add `IncDec` expression variant with `IncDecKind` (PreInc, PreDec,
    PostInc, PostDec).
  - Add `Mutability` to AST for better tracking.
- Parser:
  - Parse prefix `++`/`--` in `parse_unary_expression`.
  - Parse postfix `++`/`--` in `parse_expression` (using lookahead).
  - Add `is_assignable` check to ensure operators are applied to valid
    L-values (variables, derefs, fields, indices).
  - Integrate a new `verification` module for semantic checks.
- Verification (New Module):
  - Implement `validate_program` to check mutability rules.
  - Ensure modifications (assignments, inc/dec) target mutable variables.
  - Traverse AST nodes and scopes to track variable mutability.
  - Error on immutable binding modifications.
- LLVM Codegen:
  - Generate IR for `IncDec` expressions:
    - Support integers, floats, and pointers.
    - Handle pre/post logic (return new vs old value).
    - Use `load` -> `add/sub` -> `store` pattern.
  - Fix `generate_address_ir` to handle grouped expressions and dereferences properly.
- Tests:
  - Add `test72.wave` and `test73.wave` covering:
    - Basic variable types.
    - Increment/decrement on variables and dereferenced pointers.

This update enhances the language with standard C-style inc/dec operators
and strengthens type safety through mutability verification.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev self-assigned this Dec 25, 2025
@LunaStev LunaStev merged commit 78c2179 into wavefnd:master Dec 25, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant