Summary
Implement a complete recursive descent parser in Gradient that converts tokens into an AST.
Background
With a working lexer (Phase 1), we can now build the parser. Currently compiler/parser.gr has AST type definitions and stub parsing functions (~706 lines). Expand to ~2,000 lines with actual parsing logic.
Current State
File: compiler/parser.gr
- ✅ AST type definitions (ExprKind, StmtKind, etc.)
- ✅ Parser state types
- ❌ Actual parsing logic (stubs return empty)
Target State
Full recursive descent parser with:
- Expression parsing with precedence
- Statement parsing
- Module/item parsing
- Error recovery
- Complete AST construction
Implementation Requirements
Core Functions
-
parse_module(tokens: TokenList) -> (AstModule, ParseErrors)
- Parse entire module
- Handle all item types
-
parse_function(tokens: TokenList) -> (AstFunction, TokenList)
- Parse function definitions
- Handle parameters, return type, effects, body
-
parse_expression(tokens: TokenList, min_precedence: Int) -> (AstExpr, TokenList)
- Pratt parser for expressions
- Handle precedence and associativity
- Binary, unary, call, field access, etc.
-
parse_statement(tokens: TokenList) -> (AstStmt, TokenList)
- Let, if, while, for, return, expression statements
-
parse_type(tokens: TokenList) -> (AstType, TokenList)
- Parse type annotations
- Handle generics, effects, function types
Precedence Levels (High to Low)
- Field access, indexing (left)
- Function calls (left)
- Unary operators (right)
- Multiplicative (*, /, %) (left)
- Additive (+, -) (left)
- Comparison (<, >, ==, etc.) (left)
- Logical AND (left)
- Logical OR (left)
- Assignment (right)
Error Recovery
- Synchronize on statement boundaries
- Report errors but continue parsing
- Collect all errors, not just first
Acceptance Criteria
Testing
Test parsing of:
- All expression types
- All statement types
- Module structure
- Error cases
Part Of
- Epic: Full Self-Hosting with Rust Kernel
- Phase: 2 of 7
- Blocks: Phase 3 (Type Checker)
Effort
~5 days, ~1,300 new lines of Gradient
Dependencies
Summary
Implement a complete recursive descent parser in Gradient that converts tokens into an AST.
Background
With a working lexer (Phase 1), we can now build the parser. Currently
compiler/parser.grhas AST type definitions and stub parsing functions (~706 lines). Expand to ~2,000 lines with actual parsing logic.Current State
File:
compiler/parser.grTarget State
Full recursive descent parser with:
Implementation Requirements
Core Functions
parse_module(tokens: TokenList) -> (AstModule, ParseErrors)
parse_function(tokens: TokenList) -> (AstFunction, TokenList)
parse_expression(tokens: TokenList, min_precedence: Int) -> (AstExpr, TokenList)
parse_statement(tokens: TokenList) -> (AstStmt, TokenList)
parse_type(tokens: TokenList) -> (AstType, TokenList)
Precedence Levels (High to Low)
Error Recovery
Acceptance Criteria
Testing
Test parsing of:
Part Of
Effort
~5 days, ~1,300 new lines of Gradient
Dependencies