Arrays

Array literals and indexing in AHA! — homogeneous integer arrays with LLVM-backed storage

Arrays

AHA! supports array literals and indexing at the code-generation level. Arrays are homogeneous — every element shares one type — and the current implementation stores Int elements.

Array Literals

Write an array with square brackets and comma-separated elements:

let nums = [1, 2, 3];

An empty array is valid too:

let empty = [];

Indexing

Read an element with arr[index]. Indices are zero-based, so the first element is arr[0]:

let nums = [10, 20, 30];
print(nums[0]); // 10
print(nums[2]); // 30

Using Arrays with Loops

Because a for loop walks an integer range, it pairs naturally with indexing:

let data = [5, 15, 25, 35];
let sum = 0;
for i in 0..4 {
    sum = sum + data[i];
}
print(sum); // 80

How Arrays Work

Under the hood, an array literal allocates stack space (alloca) for an LLVM array type, then stores each element through a getelementptr (GEP). Indexing casts the array back to a pointer and loads the element at the computed offset.

AspectDetail
Element typeInt (64-bit)
Type display[Int]
StorageLLVM array_type via alloca + GEP
Indexingint_to_ptr + GEP + load

Notes

  • Arrays are homogeneous — the type system tracks the element type as AhaType::Array(Int).
  • Indexing returns an Int, so results compose with arithmetic and the numeric builtins directly.
  • Array literals and indexing are implemented in codegen (compile_array_literal, compile_index_expression).

See the Language Tour for the full set of language constructs.