Skip to content

Conversation

@emilbon99
Copy link
Contributor

@emilbon99 emilbon99 commented Dec 24, 2025

Issue Pull Request

Linear Issue

SY-####

Description

Basic Readiness

  • I have performed a self-review of my code.
  • I have added relevant tests to cover the changes to CI.
  • I have added needed QA steps to the release candidate template that cover these changes.
  • I have updated in-code documentation to reflect the changes.
  • I have updated user-facing documentation to reflect the changes.

Greptile Summary

This PR implements function calling support in the Arc language compiler, enabling both forward and backward function references with support for optional parameters with default values.

Key Changes:

  • Two-phase analysis: Function signatures are now collected in the first pass (CollectDeclarations), enabling forward references before analyzing function bodies in the second pass
  • Optional parameters: Functions can declare parameters with default values (e.g., y i64 = 10), with validation that optional parameters must follow required ones
  • Expression-level calls: Functions can be called within expressions (e.g., add(5, 10) * 2), with the compiler properly handling argument evaluation and type coercion
  • Statement-level calls: Function calls as statements properly drop unused return values
  • Comprehensive validation: Both analyzer and compiler validate argument counts (respecting optional parameters) and types with helpful error messages

Implementation Quality:

  • Clean separation between signature collection and body analysis prevents forward reference issues
  • Proper WASM bytecode generation with correct function index resolution
  • Extensive test coverage including recursion, nested calls, type coercion, and optional parameters
  • Error handling follows the custom rule requiring zero values before errors in return statements

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is well-structured with clean separation of concerns, comprehensive test coverage (476 new test lines), and proper error handling. The two-phase analysis elegantly solves forward references, and the optional parameter implementation correctly validates ordering and emits default values. No bugs or issues were found during review.
  • No files require special attention

Important Files Changed

Filename Overview
arc/go/analyzer/function/function.go Refactored to collect function signatures in first pass, enabling forward references and function calls
arc/go/analyzer/expression/expression.go Added function call validation with argument count and type checking, including optional parameter support
arc/go/compiler/expression/compiler.go Implemented function call compilation with default value emission for optional parameters
arc/go/compiler/compiler.go Populates function indices map during compilation initialization

Sequence Diagram

sequenceDiagram
    participant User as User Code
    participant Parser as Parser
    participant Analyzer as Analyzer/Function
    participant ExprAnalyzer as Analyzer/Expression
    participant Compiler as Compiler
    participant ExprCompiler as Compiler/Expression
    participant WASM as WASM Module

    Note over User,WASM: Phase 1: First Pass - Collect Function Signatures
    User->>Parser: func add(x i64, y i64 = 10) i64
    Parser->>Analyzer: CollectDeclarations()
    Analyzer->>Analyzer: collectConfig(), collectInputs(), collectOutputs()
    Note right of Analyzer: Parse default values<br/>Validate optional params<br/>Build function signature
    Analyzer->>Analyzer: Add to symbol table with full signature
    
    Note over User,WASM: Phase 2: Second Pass - Analyze Function Bodies
    Analyzer->>Analyzer: Analyze()
    Analyzer->>Analyzer: addConfigToScope(), addInputsToScope(), addOutputsToScope()
    Note right of Analyzer: Add params to function scope
    
    Note over User,WASM: Phase 3: Analyze Function Calls
    User->>Parser: result := add(5)
    Parser->>ExprAnalyzer: analyzePostfix()
    ExprAnalyzer->>ExprAnalyzer: Detect function call pattern
    ExprAnalyzer->>ExprAnalyzer: validateFunctionCall()
    ExprAnalyzer->>ExprAnalyzer: Check argument count (1 vs required 1-2)
    ExprAnalyzer->>ExprAnalyzer: Type check arguments
    Note right of ExprAnalyzer: Validates args against params<br/>Supports optional params
    
    Note over User,WASM: Phase 4: Compile Functions
    Compiler->>Compiler: Build FunctionIndices map
    Note right of Compiler: Maps function names to WASM indices
    Compiler->>ExprCompiler: Compile function body
    
    Note over User,WASM: Phase 5: Compile Function Calls
    ExprCompiler->>ExprCompiler: compilePostfix()
    ExprCompiler->>ExprCompiler: compileFunctionCallExpr()
    ExprCompiler->>ExprCompiler: Compile provided arguments
    ExprCompiler->>ExprCompiler: emitDefaultValue() for missing optionals
    Note right of ExprCompiler: Emit WASM const instructions<br/>for default values
    ExprCompiler->>WASM: WriteCall(funcIdx)
    WASM-->>ExprCompiler: Return type from function signature
Loading

Context used:

  • Rule from dashboard - When returning errors in Go functions, the first return values should be zero values (e.g., 0 for in... (source)

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.

2 participants