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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | 24x 24x 24x 24x 1x 23x 23x 24x 24x 24x 24x 24x 24x 6x 1x 5x 5x 5x 5x 5x 17x 17x 1x 16x 21x 17x 4x 4x 1x 3x 3x | /**
* ArrayInitHelper - Handles array initialization with size inference and fill-all syntax
*
* Issue #644: Extracted from CodeGenerator to reduce file size.
*
* Handles:
* - Array initializers with size inference: u8 data[] <- [1, 2, 3]
* - Fill-all syntax: u8 data[10] <- [0*]
* - Array size validation
*
* Migrated to use CodeGenState instead of constructor DI.
*/
import * as Parser from "../../../logic/parser/grammar/CNextParser.js";
import CodeGenState from "../../../state/CodeGenState.js";
/**
* Result from processing array initialization.
*/
interface IArrayInitResult {
/** Whether this was an array initializer (vs regular expression) */
isArrayInit: boolean;
/** The dimension suffix to add to declaration (e.g., "[3]") */
dimensionSuffix: string;
/** The final initializer value */
initValue: string;
}
/**
* Callbacks required for array initialization.
* These need CodeGenerator context and cannot be replaced with static state.
*/
interface IArrayInitCallbacks {
/** Generate expression code */
generateExpression: (ctx: Parser.ExpressionContext) => string;
/** Get type name from type context */
getTypeName: (ctx: Parser.TypeContext) => string;
/** Generate array dimensions */
generateArrayDimensions: (dims: Parser.ArrayDimensionContext[]) => string;
}
/**
* Handles array initialization with size inference and fill-all syntax.
*/
class ArrayInitHelper {
/**
* Process array initialization expression.
* Returns null if not an array initializer pattern.
*
* @param name - Variable name
* @param typeCtx - Type context
* @param expression - Initializer expression
* @param arrayDims - Array dimension contexts
* @param hasEmptyArrayDim - Whether any dimension is empty (for inference)
* @param declaredSize - First dimension size if explicit, null otherwise
* @param callbacks - Callbacks to CodeGenerator methods
*/
static processArrayInit(
name: string,
typeCtx: Parser.TypeContext,
expression: Parser.ExpressionContext,
arrayDims: Parser.ArrayDimensionContext[],
hasEmptyArrayDim: boolean,
declaredSize: number | null,
callbacks: IArrayInitCallbacks,
): IArrayInitResult | null {
// Reset and generate initializer
CodeGenState.lastArrayInitCount = 0;
CodeGenState.lastArrayFillValue = undefined;
const initValue = ArrayInitHelper._generateArrayInitValue(
typeCtx,
expression,
callbacks,
);
// Check if it was an array initializer
if (!ArrayInitHelper._isArrayInitializer()) {
return null;
}
CodeGenState.localArrays.add(name);
const dimensionSuffix = hasEmptyArrayDim
? ArrayInitHelper._processSizeInference(name)
: ArrayInitHelper._processExplicitSize(
arrayDims,
declaredSize,
callbacks,
);
const finalInitValue = ArrayInitHelper._expandFillAllSyntax(
initValue,
declaredSize,
);
return { isArrayInit: true, dimensionSuffix, initValue: finalInitValue };
}
/**
* Generate the array initializer value with proper expected type
*/
private static _generateArrayInitValue(
typeCtx: Parser.TypeContext,
expression: Parser.ExpressionContext,
callbacks: IArrayInitCallbacks,
): string {
const typeName = callbacks.getTypeName(typeCtx);
return CodeGenState.withExpectedType(typeName, () =>
callbacks.generateExpression(expression),
);
}
/**
* Check if the last expression was an array initializer
*/
private static _isArrayInitializer(): boolean {
return (
CodeGenState.lastArrayInitCount > 0 ||
CodeGenState.lastArrayFillValue !== undefined
);
}
/**
* Process size inference for empty array dimension (u8 data[] <- [1, 2, 3])
*/
private static _processSizeInference(name: string): string {
if (CodeGenState.lastArrayFillValue !== undefined) {
throw new Error(
`Error: Fill-all syntax [${CodeGenState.lastArrayFillValue}*] requires explicit array size`,
);
}
// Update type registry with inferred size for .length support
const existingType = CodeGenState.getVariableTypeInfo(name);
Eif (existingType) {
existingType.arrayDimensions = [CodeGenState.lastArrayInitCount];
CodeGenState.setVariableTypeInfo(name, existingType);
}
return `[${CodeGenState.lastArrayInitCount}]`;
}
/**
* Process explicit array size with validation
*/
private static _processExplicitSize(
arrayDims: Parser.ArrayDimensionContext[],
declaredSize: number | null,
callbacks: IArrayInitCallbacks,
): string {
const dimensionSuffix = callbacks.generateArrayDimensions(arrayDims);
// Validate size matches if not using fill-all
if (
declaredSize !== null &&
CodeGenState.lastArrayFillValue === undefined &&
CodeGenState.lastArrayInitCount !== declaredSize
) {
throw new Error(
`Error: Array size mismatch - declared [${declaredSize}] but got ${CodeGenState.lastArrayInitCount} elements`,
);
}
return dimensionSuffix;
}
/**
* Expand fill-all syntax (e.g., [0*] with size 5 -> {0, 0, 0, 0, 0})
*/
private static _expandFillAllSyntax(
initValue: string,
declaredSize: number | null,
): string {
if (
CodeGenState.lastArrayFillValue === undefined ||
declaredSize === null
) {
return initValue;
}
const fillVal = CodeGenState.lastArrayFillValue;
// C handles {0} correctly, no need to expand
if (fillVal === "0") {
return initValue;
}
const elements = new Array<string>(declaredSize).fill(fillVal);
return `{${elements.join(", ")}}`;
}
}
export default ArrayInitHelper;
|