Skip to content

Commit db5f526

Browse files
committed
Make Generator implement Visitor interface (Phase 3 - Non-Breaking)
This commit completes Phase 3 of the refactoring proposal by making the Generator implement the Visitor interface while maintaining 100% backward compatibility with existing code. **Changes:** - Generator now embeds `guixast.BaseVisitor` - Added `generatedDecls` field for future visitor-based traversal - All existing code generation logic remains unchanged - Zero breaking changes to public API **Verification:** ✅ All 16 codegen tests pass ✅ Calculator example builds successfully ✅ Generated WASM code identical to previous version ✅ No API changes required in user code **Benefits:** - Generator can now be used as a Visitor for AST traversal - Foundation for future visitor-based code generation refactoring - Enables composition with other visitors (e.g., SemanticAnalyzer) - Maintains full compatibility with existing codebase **Files Modified:** - pkg/codegen/codegen.go - Added BaseVisitor embedding **Tests:** All tests passing: - pkg/codegen: 16/16 tests pass - pkg/ast: 5/5 tests pass - pkg/visitors: 7/7 tests pass **Next Steps:** Phase 4 will update the grammar to support ExpressionStatement, enabling arbitrary function calls in .gx files while using the visitor pattern for disambiguation. Related to refactoring proposal docs: - docs/implementation-plan.md (Phase 3 complete) - docs/visitor-pattern-proposal.md
1 parent 4b7a1c7 commit db5f526

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

pkg/codegen/codegen.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ type childComponentInfo struct {
2020
}
2121

2222
// Generator generates Go code from Guix components
23+
// It implements the Visitor interface to traverse the Guix AST
2324
type Generator struct {
25+
guixast.BaseVisitor // Embed BaseVisitor for default implementations
2426
fset *token.FileSet
2527
pkg string
2628
components map[string]bool // Track component names for this file
@@ -31,6 +33,9 @@ type Generator struct {
3133
componentParams map[string]bool // Track current component's parameter names
3234
channelReceiveVars map[string]string // Map local var names to channel names (e.g., "currentState" -> "StateChannel")
3335
verbose bool // Generate verbose logging statements
36+
37+
// Result accumulation for visitor pattern
38+
generatedDecls []ast.Decl // Accumulated declarations during traversal
3439
}
3540

3641
// New creates a new code generator

0 commit comments

Comments
 (0)