Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 21, 2025

Separate type-checking from ParsingModule to new TypeCheckModule trait

Summary

This PR introduces a TypeCheckModule trait that separates type-checking logic from parsing across all modules in the Amber compiler, establishing a clean compilation pipeline: tokenize → parse → typecheck → translate.

Key Changes

New TypeCheckModule Trait

pub trait TypeCheckModule {
    fn typecheck(&mut self, meta: &mut ParserMetadata) -> SyntaxResult;
}

Compiler Pipeline Integration

  • Added dedicated typecheck() phase in compiler.rs between parsing and translation
  • Includes debug timing output for performance monitoring

Operation Traits Updated

All operation traits now require TypeCheckModule as a supertrait:

  • BinOp, UnOp, TypeOp, TernOp

Implementation Coverage

40+ modules updated with complete phase separation:

Operators (17 modules)

  • Binary ops (13): Add, Sub, Mul, Div, Modulo, And, Or, Gt, Ge, Lt, Le, Eq, Neq, Range
  • Unary ops (2): Not, Neg
  • Type ops (2): Cast, Is
  • All include recursive typecheck() calls on child expressions before validation

Core Modules

  • Block, Statement, Expr: Dispatch type checking to appropriate components
  • Parentheses, Array: Move type assignment from parse() to typecheck()

Variable System

  • VariableInit, VariableGet, VariableSet: Move handle_variable_reference() to typecheck()
  • Shorthand ops (5): Add, Sub, Mul, Div, Modulo - move all semantic validation to typecheck()
  • Token positions stored during parsing for accurate error reporting

Control Flow & Functions

  • Builtin commands: Echo, Cd, Mv, Exit
  • Function ops: Return, Fail, FunctionInvocation, Command
  • Loops: IfCondition, IfChain, IterLoop, WhileLoop, Failed
  • All recursively type-check nested expressions and blocks

Architecture Benefits

  1. Clean separation: Parse handles syntax only, typecheck handles all semantic validation
  2. Proper phase ordering: Types resolved after expressions are fully parsed
  3. Recursive validation: All nested expressions validated bottom-up
  4. Maintainability: Clear responsibility boundaries between compilation phases
  5. Error reporting: Token positions preserved for accurate error messages

Example

Before:

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
    // Parsing AND type checking mixed
    self.kind = Self::typecheck_allowed_types(meta, ...)?;
    Ok(())
}

After:

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
    // Pure syntax parsing
    Ok(())
}

impl TypeCheckModule for Add {
    fn typecheck(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
        self.left.typecheck(meta)?;  // Recursive validation
        self.right.typecheck(meta)?;
        self.kind = Self::typecheck_allowed_types(meta, ...)?;
        Ok(())
    }
}

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Separate type-checking from ParsingModule to a new TypeCheckModule trait Separate type-checking from ParsingModule to new TypeCheckModule trait Sep 21, 2025
Copilot AI requested a review from Ph0enixKM September 21, 2025 15:28
@Ph0enixKM
Copy link
Member

@copilot In the compiler.rs this PR fails to handle the type checking phase and also it doesn't update all syntax modules

This comment was marked as resolved.

Copilot AI changed the title Separate type-checking from ParsingModule to new TypeCheckModule trait Separate type-checking from ParsingModule to new TypeCheckModule trait with integrated compiler pipeline Sep 21, 2025
Copilot finished work on behalf of Ph0enixKM September 21, 2025 16:40
@Ph0enixKM
Copy link
Member

@copilot Resolve clippy errors

This comment was marked as resolved.

Copilot finished work on behalf of Ph0enixKM September 26, 2025 13:05
@Ph0enixKM
Copy link
Member

@copilot this PR is almost ready. The problem now why the tests are failing is because it doesn't recursively type-checks nested syntax modules. For instance:

echo "12" - 12

In this example echo is parsed first and since in this module a noop type check is introduced, the nested expression is not parsed at all.

Copy link
Contributor Author

Copilot AI commented Sep 26, 2025

@copilot this PR is almost ready. The problem now why the tests are failing is because it doesn't recursively type-checks nested syntax modules. For instance:

echo "12" - 12

...

Fixed the recursive type checking issue in commit ad6e9ac.

Problem: Modules with impl_noop_typecheck! weren't type-checking nested expressions.

Solution: Replaced no-op implementations with proper recursive type checking for 15+ modules including:

  • Echo, Cd, Mv - now type-check nested expressions
  • VariableInit, VariableSet, Return - type-check value expressions
  • IfCondition, WhileLoop, IterLoop - type-check condition expressions and blocks
  • FunctionInvocation, Command - type-check argument expressions

Verification:

  • echo "12" - 12 now correctly reports type error
  • echo 12 - 5 works and outputs 7
  • ✅ All nested expressions are properly validated

Copilot AI changed the title Separate type-checking from ParsingModule to new TypeCheckModule trait with integrated compiler pipeline Separate type-checking from ParsingModule to new TypeCheckModule trait with integrated compiler pipeline and recursive validation Sep 26, 2025
Copilot finished work on behalf of Ph0enixKM September 26, 2025 14:16
Copy link
Member

@Ph0enixKM Ph0enixKM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot some minor issues

Copy link
Contributor Author

Copilot AI commented Sep 27, 2025

@copilot resolve compilation errors

Fixed compilation errors in commit c1061be. The issue was that the VariableGet structure was missing the tok: Option<Token> field that was being referenced in the typecheck implementation. Added the field definition and corrected the token usage to match the pattern used in other modules.

@lens0021 lens0021 changed the title Separate type-checking from ParsingModule to new TypeCheckModule trait with integrated compiler pipeline, complete recursive validation, proper phase separation, pure parsing architecture, and token position management Separate type-checking from ParsingModule to new TypeCheckModule trait Sep 27, 2025
lens0021

This comment was marked as resolved.

@Ph0enixKM
Copy link
Member

I'll probably get back to it this sunday. I have too much going on right now

@Ph0enixKM
Copy link
Member

@copilot rewrite this PR's description from scratch.

Copilot finished work on behalf of Ph0enixKM October 22, 2025 14:14
@amber-lang amber-lang deleted a comment from Copilot AI Oct 22, 2025
@Ph0enixKM Ph0enixKM requested a review from lens0021 November 17, 2025 19:47
@Ph0enixKM Ph0enixKM marked this pull request as ready for review November 17, 2025 19:47
@Ph0enixKM
Copy link
Member

@copilot rewrite the PR description from scratch and make it concise

This comment was marked as resolved.

Copilot finished work on behalf of Ph0enixKM November 17, 2025 20:19
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.

3 participants