refactor: Break up oversized functions in codegen.rs #215

Closed
opened 2026-01-09 04:43:25 +00:00 by navicore · 1 comment
navicore commented 2026-01-09 04:43:25 +00:00 (Migrated from github.com)

Problem

crates/compiler/src/codegen.rs contains multiple functions exceeding 100 lines, making them difficult to understand, test, and maintain.

Oversized Functions

Function Lines Location Primary Issues
codegen_program_with_config ~324 820-1144 Mixed initialization, setup, and codegen
emit_runtime_declarations ~245 1298-1543 100+ sequential writeln! calls
codegen_match_statement ~117 5269-5386 Deep nesting, multiple responsibilities
codegen_inline_binary_op ~111 4079-4190 Fast/slow path mixing

codegen_program_with_config (324 lines → 4 functions)

fn codegen_program_with_config(&mut self, config: Config) -> Result<String> {
    let context = self.initialize_codegen_context(config)?;
    self.emit_preamble(&context)?;
    self.emit_word_definitions(&context)?;
    self.emit_main_entry(&context)?;
    self.finalize_output(&context)
}

fn initialize_codegen_context(&mut self, config: Config) -> Result<CodegenContext>;
fn emit_preamble(&mut self, ctx: &CodegenContext) -> Result<()>;
fn emit_word_definitions(&mut self, ctx: &CodegenContext) -> Result<()>;
fn emit_main_entry(&mut self, ctx: &CodegenContext) -> Result<()>;

emit_runtime_declarations (245 lines → data-driven)

See related issue for extracting to data-driven approach.

codegen_match_statement (117 lines → 3 functions)

fn codegen_match_statement(&mut self, stmt: &MatchStmt) -> Result<()> {
    let scrutinee = self.codegen_match_scrutinee(stmt)?;
    self.codegen_match_arms(stmt, scrutinee)?;
    self.codegen_match_epilogue(stmt)
}

codegen_inline_binary_op (111 lines → 2 functions)

fn codegen_inline_binary_op(&mut self, op: BinaryOp) -> Result<()> {
    if self.can_use_fast_path() {
        self.codegen_inline_binary_op_virtual(op)
    } else {
        self.codegen_inline_binary_op_memory(op)
    }
}

Guidelines for Function Size

  • Target: Functions should be < 50 lines
  • Maximum: No function should exceed 80 lines
  • Exception: Data definitions (tables, constants) can be longer

Acceptance Criteria

  • No function exceeds 80 lines
  • Each function has single clear purpose
  • Function names describe what they do
  • All tests pass
  • Code coverage maintained

Labels

refactor, technical-debt, high-priority

## Problem `crates/compiler/src/codegen.rs` contains multiple functions exceeding 100 lines, making them difficult to understand, test, and maintain. ## Oversized Functions | Function | Lines | Location | Primary Issues | |----------|-------|----------|----------------| | `codegen_program_with_config` | ~324 | 820-1144 | Mixed initialization, setup, and codegen | | `emit_runtime_declarations` | ~245 | 1298-1543 | 100+ sequential writeln! calls | | `codegen_match_statement` | ~117 | 5269-5386 | Deep nesting, multiple responsibilities | | `codegen_inline_binary_op` | ~111 | 4079-4190 | Fast/slow path mixing | ## Recommended Splits ### `codegen_program_with_config` (324 lines → 4 functions) ```rust fn codegen_program_with_config(&mut self, config: Config) -> Result<String> { let context = self.initialize_codegen_context(config)?; self.emit_preamble(&context)?; self.emit_word_definitions(&context)?; self.emit_main_entry(&context)?; self.finalize_output(&context) } fn initialize_codegen_context(&mut self, config: Config) -> Result<CodegenContext>; fn emit_preamble(&mut self, ctx: &CodegenContext) -> Result<()>; fn emit_word_definitions(&mut self, ctx: &CodegenContext) -> Result<()>; fn emit_main_entry(&mut self, ctx: &CodegenContext) -> Result<()>; ``` ### `emit_runtime_declarations` (245 lines → data-driven) See related issue for extracting to data-driven approach. ### `codegen_match_statement` (117 lines → 3 functions) ```rust fn codegen_match_statement(&mut self, stmt: &MatchStmt) -> Result<()> { let scrutinee = self.codegen_match_scrutinee(stmt)?; self.codegen_match_arms(stmt, scrutinee)?; self.codegen_match_epilogue(stmt) } ``` ### `codegen_inline_binary_op` (111 lines → 2 functions) ```rust fn codegen_inline_binary_op(&mut self, op: BinaryOp) -> Result<()> { if self.can_use_fast_path() { self.codegen_inline_binary_op_virtual(op) } else { self.codegen_inline_binary_op_memory(op) } } ``` ## Guidelines for Function Size - **Target**: Functions should be < 50 lines - **Maximum**: No function should exceed 80 lines - **Exception**: Data definitions (tables, constants) can be longer ## Acceptance Criteria - [ ] No function exceeds 80 lines - [ ] Each function has single clear purpose - [ ] Function names describe what they do - [ ] All tests pass - [ ] Code coverage maintained ## Labels refactor, technical-debt, high-priority
navicore commented 2026-01-10 18:32:47 +00:00 (Migrated from github.com)
https://github.com/navicore/patch-seq/pull/225
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
navicore/patch-seq#215
No description provided.