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 | 21x 21x 21x 7x 7x 7x 4x 4x 7x 15x 7x 11x 1x 10x 10x 10x 11x 1x 9x 9x 1x 8x 8x 1x 7x 1x 6x 5x 1x 6x 1x 5x 1x 4x 2x 2x 2x 14x | /**
* Array assignment handlers (ADR-109).
*
* Handles assignments to array elements:
* - ARRAY_ELEMENT: arr[i] <- value
* - MULTI_DIM_ARRAY_ELEMENT: matrix[i][j] <- value
* - ARRAY_SLICE: buffer[0, 10] <- source
*/
import AssignmentKind from "../AssignmentKind";
import IAssignmentContext from "../IAssignmentContext";
import IHandlerDeps from "./IHandlerDeps";
import TAssignmentHandler from "./TAssignmentHandler";
/**
* Handle simple array element: arr[i] <- value
*/
function handleArrayElement(
ctx: IAssignmentContext,
deps: IHandlerDeps,
): string {
const name = ctx.identifiers[0];
const index = deps.generateExpression(ctx.subscripts[0]);
return `${name}[${index}] ${ctx.cOp} ${ctx.generatedValue};`;
}
/**
* Handle multi-dimensional array element: matrix[i][j] <- value
*/
function handleMultiDimArrayElement(
ctx: IAssignmentContext,
deps: IHandlerDeps,
): string {
const name = ctx.identifiers[0];
const typeInfo = deps.typeRegistry.get(name);
// ADR-036: Compile-time bounds checking for constant indices
if (typeInfo?.arrayDimensions) {
const line = ctx.subscripts[0]?.start?.line ?? 0;
deps.checkArrayBounds(name, typeInfo.arrayDimensions, ctx.subscripts, line);
}
const indices = ctx.subscripts
.map((e) => deps.generateExpression(e))
.join("][");
return `${name}[${indices}] ${ctx.cOp} ${ctx.generatedValue};`;
}
/**
* Handle array slice assignment: buffer[0, 10] <- source
*
* Validates:
* - Offset and length must be compile-time constants
* - Only valid on 1D arrays
* - Bounds checking at compile time
*/
function handleArraySlice(ctx: IAssignmentContext, deps: IHandlerDeps): string {
if (ctx.isCompound) {
throw new Error(
`Compound assignment operators not supported for slice assignment: ${ctx.cnextOp}`,
);
}
const name = ctx.identifiers[0];
const typeInfo = deps.typeRegistry.get(name);
// Get line number for error messages
const line = ctx.subscripts[0].start?.line ?? 0;
// Validate 1D array only
if (typeInfo?.arrayDimensions && typeInfo.arrayDimensions.length > 1) {
throw new Error(
`${line}:0 Error: Slice assignment is only valid on one-dimensional arrays. ` +
`'${name}' has ${typeInfo.arrayDimensions.length} dimensions. ` +
`Access the innermost dimension first (e.g., ${name}[index][offset, length]).`,
);
}
// Validate offset is compile-time constant
const offsetValue = deps.tryEvaluateConstant(ctx.subscripts[0]);
if (offsetValue === undefined) {
throw new Error(
`${line}:0 Error: Slice assignment offset must be a compile-time constant. ` +
`Runtime offsets are not allowed to ensure bounds safety.`,
);
}
// Validate length is compile-time constant
const lengthValue = deps.tryEvaluateConstant(ctx.subscripts[1]);
if (lengthValue === undefined) {
throw new Error(
`${line}:0 Error: Slice assignment length must be a compile-time constant. ` +
`Runtime lengths are not allowed to ensure bounds safety.`,
);
}
// Determine buffer capacity
let capacity: number;
if (typeInfo?.isString && typeInfo.stringCapacity && !typeInfo.isArray) {
capacity = typeInfo.stringCapacity + 1;
} else if (typeInfo?.arrayDimensions?.[0]) {
capacity = typeInfo.arrayDimensions[0];
} else {
throw new Error(
`${line}:0 Error: Cannot determine buffer size for '${name}' at compile time.`,
);
}
// Bounds validation
if (offsetValue + lengthValue > capacity) {
throw new Error(
`${line}:0 Error: Slice assignment out of bounds: ` +
`offset(${offsetValue}) + length(${lengthValue}) = ${offsetValue + lengthValue} ` +
`exceeds buffer capacity(${capacity}) for '${name}'.`,
);
}
if (offsetValue < 0) {
throw new Error(
`${line}:0 Error: Slice assignment offset cannot be negative: ${offsetValue}`,
);
}
if (lengthValue <= 0) {
throw new Error(
`${line}:0 Error: Slice assignment length must be positive: ${lengthValue}`,
);
}
// Mark that we need string.h for memcpy
deps.markNeedsString();
return `memcpy(&${name}[${offsetValue}], &${ctx.generatedValue}, ${lengthValue});`;
}
/**
* All array handlers for registration.
*/
const arrayHandlers: ReadonlyArray<[AssignmentKind, TAssignmentHandler]> = [
[AssignmentKind.ARRAY_ELEMENT, handleArrayElement],
[AssignmentKind.MULTI_DIM_ARRAY_ELEMENT, handleMultiDimArrayElement],
[AssignmentKind.ARRAY_SLICE, handleArraySlice],
];
export default arrayHandlers;
|