SY-3177: Implement Function Calling #1803
Open
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.
Issue Pull Request
Linear Issue
SY-####
Description
Basic Readiness
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:
CollectDeclarations), enabling forward references before analyzing function bodies in the second passy i64 = 10), with validation that optional parameters must follow required onesadd(5, 10) * 2), with the compiler properly handling argument evaluation and type coercionImplementation Quality:
Confidence Score: 5/5
Important Files Changed
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 signatureContext used:
dashboard- When returning errors in Go functions, the first return values should be zero values (e.g., 0 for in... (source)