Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 881x 881x 881x 4983x 5811x 11621x 4x 6x 2x 2x 996x 161x 4840x | /**
* Generator Registry
*
* Registry for extracted code generators using the "strangler fig" pattern.
* Generators are registered here as they're extracted from CodeGenerator.ts.
*
* During migration:
* - The registry starts empty
* - As generators are extracted (A2-A5), they register here
* - CodeGenerator checks the registry before falling back to inline methods
* - Tests pass throughout the migration
*/
import { ParserRuleContext } from "antlr4ng";
import TGeneratorFn from "./TGeneratorFn";
/**
* Registry for extracted generators.
*
* Enables gradual migration from monolithic CodeGenerator to modular generators.
* Each generator category (declarations, statements, expressions) has its own map
* keyed by the specific kind of node it handles.
*
* Example usage (in future A2-A5 issues):
* ```typescript
* // In expressionGenerators/ternary.ts
* export const generateTernary: TGeneratorFn<TernaryExprContext> = (node, input, state, orch) => {
* const condition = orch.generateExpression(node.condition());
* // ...
* return { code: `${condition} ? ${trueBranch} : ${falseBranch}`, effects: [] };
* };
*
* // In CodeGenerator initialization
* registry.registerExpression('ternary', generateTernary);
* ```
*/
export default class GeneratorRegistry {
/**
* Declaration generators indexed by declaration kind.
* Example kinds: 'scope', 'struct', 'enum', 'function', 'variable'
*/
private readonly declarations = new Map<
string,
TGeneratorFn<ParserRuleContext>
>();
/**
* Statement generators indexed by statement kind.
* Example kinds: 'if', 'while', 'for', 'assignment', 'return', 'switch'
*/
private readonly statements = new Map<
string,
TGeneratorFn<ParserRuleContext>
>();
/**
* Expression generators indexed by expression kind.
* Example kinds: 'ternary', 'binary', 'unary', 'call', 'member', 'literal'
*/
private readonly expressions = new Map<
string,
TGeneratorFn<ParserRuleContext>
>();
// =========================================================================
// Registration Methods
// =========================================================================
/**
* Register a declaration generator.
* @param kind - Declaration kind (e.g., 'struct', 'enum', 'function')
* @param fn - Generator function
*/
registerDeclaration<T extends ParserRuleContext>(
kind: string,
fn: TGeneratorFn<T>,
): void {
this.declarations.set(kind, fn as TGeneratorFn<ParserRuleContext>);
}
/**
* Register a statement generator.
* @param kind - Statement kind (e.g., 'if', 'while', 'assignment')
* @param fn - Generator function
*/
registerStatement<T extends ParserRuleContext>(
kind: string,
fn: TGeneratorFn<T>,
): void {
this.statements.set(kind, fn as TGeneratorFn<ParserRuleContext>);
}
/**
* Register an expression generator.
* @param kind - Expression kind (e.g., 'ternary', 'binary', 'call')
* @param fn - Generator function
*/
registerExpression<T extends ParserRuleContext>(
kind: string,
fn: TGeneratorFn<T>,
): void {
this.expressions.set(kind, fn as TGeneratorFn<ParserRuleContext>);
}
// =========================================================================
// Deregistration Methods (for testing error paths)
// =========================================================================
/**
* Unregister a declaration generator.
* Primarily used for testing error paths when generators are missing.
*/
unregisterDeclaration(kind: string): void {
this.declarations.delete(kind);
}
// =========================================================================
// Query Methods
// =========================================================================
/**
* Check if a declaration generator is registered.
*/
hasDeclaration(kind: string): boolean {
return this.declarations.has(kind);
}
/**
* Check if a statement generator is registered.
*/
hasStatement(kind: string): boolean {
return this.statements.has(kind);
}
/**
* Check if an expression generator is registered.
*/
hasExpression(kind: string): boolean {
return this.expressions.has(kind);
}
// =========================================================================
// Getter Methods
// =========================================================================
/**
* Get a declaration generator by kind.
* @returns The generator function, or undefined if not registered
*/
getDeclaration(kind: string): TGeneratorFn<ParserRuleContext> | undefined {
return this.declarations.get(kind);
}
/**
* Get a statement generator by kind.
* @returns The generator function, or undefined if not registered
*/
getStatement(kind: string): TGeneratorFn<ParserRuleContext> | undefined {
return this.statements.get(kind);
}
/**
* Get an expression generator by kind.
* @returns The generator function, or undefined if not registered
*/
getExpression(kind: string): TGeneratorFn<ParserRuleContext> | undefined {
return this.expressions.get(kind);
}
}
|