Files
sx/examples/10-generic-struct.sx
agra 29784c22a8 mem: implicit-context foundation + many compiler fixes
The session-long set of changes that lay the groundwork for the
Jai-literal implicit-Context-parameter refactor. Lots of accumulated
work; the new arrival is the implicit-ctx foundation (steps 1+2 of
the plan in current/CHECKPOINT-MEM.md):

  Step 1 — `CAllocator :: struct {}` stateless allocator in
    library/modules/allocators.sx, delegating directly to
    libc_malloc/libc_free. `ConstantValue` in src/ir/inst.zig gains a
    `func_ref: FuncId` leaf so nested aggregates can carry function
    pointers (the inline Allocator value's fn-ptr fields). Switch
    sites updated in emit_llvm.zig, print.zig, interp.zig.

  Step 2 — `emitDefaultContextGlobal` in src/ir/lower.zig synthesises
    a static `__sx_default_context` global with a nested-aggregate
    init_val pointing at the CAllocator → Allocator thunks. The
    second-pass `initVtableGlobals` in emit_llvm.zig is generalised
    to handle `.aggregate` init_vals (re-emits after func_map is
    populated so func_ref leaves resolve to real symbols).

Also folded in from earlier work this session:

  - Phase 1.1: `xx value` heap-copy in `buildProtocolValue` routes
    through `context.allocator` via the new `allocViaContext` helper.
  - interp.zig: `marshalForeignArg` double-offset bug fixed —
    `heapSlice` already adds `hp.offset` to the slice ptr, so the
    extra `+ hp.offset` was scribbling memcpy/memset into adjacent
    heap state, corrupting `heap.items[0]`. Symptom: `build_format`
    at comptime produced zero bytes, all `print` calls failed.
  - Lazy lowering: `lazyLowerFunction` now declares foreign-body
    functions as extern stubs in the local (comptime) module so
    cross-module foreign calls resolve.
  - Allocator API: all stdlib allocators on one-line `init() -> *T`
    (CAllocator/GPA: libc-backed; Arena/TrackingAllocator: parent-
    backed; BufAlloc: embeds state at head of user buffer).
  - issues 0038 (transitive #import), 0039 (chess + stdlib migration
    fallout), 0040 (generic struct method dot-dispatch), 0041
    (pointer types as type-arg), 0042 (alias name resolution) — all
    fixed; regression tests in examples/.
  - Diagnostic: `emitError` now embeds the lowering's
    `current_source_file` and enclosing function in the literal
    message; SX_TRACE_UNRESOLVED=1 dumps a Zig stack trace at the
    emit site so misattributed spans can't hide where the failure
    is.
  - tools/verify-step.sh (all-platforms gate) and tools/scratch.sh
    (interp/codegen parity tester) added.

Test suite: 152 example tests pass; chess builds + screenshots on
macOS / iOS sim / Android.
2026-05-24 22:59:20 +03:00

88 lines
1.6 KiB
Plaintext

#import "modules/std.sx";
#import "modules/math";
Vec :: struct($N: u32, $T:Type) {
// <N x T> (LLVM Vector)
// Vector is a Builtin Type
data: Vector(N,T);
}
Complex :: ($T:Type) -> Type {
return struct {
value: T;
//..inject
count: u32;
};
}
Vec3 :: Vec(3, f32);
vec3 :: (x:f32, y:f32, z:f32) -> Vector(3,f32) {
.[x, y, z];
}
Foo :: Complex(u32);
main :: () {
v1 := Vec3.{data = .[1,3,2]};
print("v1: {}\n", v1);
//stdout: Vec(3,f32){data: [1.0, 3.0, 2.0]}
//
v2 := vec3(1,3,2);
print("v2: {}\n", v2);
//stdout: [1.0, 3.0, 2.0]
//
// [N x T] (LLVM Array)
buffer : [5]f32 = .[0, 2, 3.5, 4, 0];
print("buff: {}\n", buffer);
//stdout: [0.0, 2.0, 3.5, 4.0, 0.0]
//
comp : Foo = .{value = 42, count = 1};
print("comp: {}\n", comp);
//stdout: Foo{value: 42, count: 1}
//
// Vector arithmetic
v3 := vec3(3,2,1);
add := v2 + v3;
print("add: {}\n", add);
// Element access
v2x := v2.x;
print("v2.x: {}\n", v2x);
// Index access
v2i := v2[1];
print("v2[1]: {}\n", v2i);
// Scalar broadcast
scaled := v2 * 2.0;
print("scaled: {}\n", scaled);
// Negation
neg := -v2;
print("neg: {}\n", neg);
// sqrt
s := sqrt(9.0);
print("sqrt(9): {}\n", s);
// inline generic type
Sx :: (user: $T) -> Type {
return enum {
counter: s32;
user: T;
};
}
sx := Sx(f32).user(0.5);
print("{}\n", sx);
print("{}\n", size_of(f32));
print("{}\n", size_of(Sx(f32)));
print("{}\n", size_of(Foo));
}