Files
sx/examples/169-pack-value-dispatch.sx
agra fd03b5812f ffi M5.A.next.4.3: $args[$i] in expression position — source construction
Final slice of the .type_tag activation. Sx code can now
construct Type values through the `$<pack>[<int_literal>]`
syntax in expression position. Lowering emits the new
`const_type(TypeId)` opcode; the interp materialises
`Value.type_tag(TypeId)`; reflection intrinsics + cmp_eq
read it kind-honestly.

Plumbing:

- src/parser.zig: `parsePrimary` accepts `$<ident>[<int_literal>]`
  at the front of every expression. Emits a `pack_index_type_expr`
  AST node — same node already used in TYPE positions in step 3,
  now extended to expression positions.

- src/ir/lower.zig: two places teach the new node.
  - `lowerExpr` arm: looks up `pack_arg_types[name][index]`, emits
    `builder.constType(arg_tys[index])`. OOB / no-binding paths
    emit a focused diagnostic + a `constType(.void)` placeholder
    (loud failure preserves silent-error budget).
  - `resolveTypeArg` arm: the same lookup, but returns the
    TypeId directly. Used by the lower-time fast paths in
    `tryLowerReflectionCall` + `tryConstBoolCondition` so
    `type_name($args[0])`, `type_eq($args[0], s64)`, and
    `has_impl(...)` all see the bound TypeId rather than
    falling through to the `.s64` default that the silent-arm
    rule forbids.

The two arms ensure both runtime AND compile-time paths use
the same source-of-truth (`pack_arg_types`), so per-mono
dispatch via `inline if type_eq($args[0], s64) { ... }` folds
at compile time as expected.

`examples/169-pack-value-dispatch.sx` exercises both shapes:
- `type_name($args[0])` returns the per-mono concrete type
  name ("s64", "string", "f64").
- `inline if type_eq($args[0], s64) { ... }` ladder dispatches
  per-mono ("got s64", "got string", "got bool", "got other").

209/209 example tests + `zig build test` green.

What's now possible end-to-end:

  show :: (..$args) -> string => type_name($args[0]);
  show(42)    // "s64"
  show("hi")  // "string"

  describe :: (..$args) -> string {
      inline if type_eq($args[0], s64) { return "got s64"; }
      ...
  }

The "by the book" activation is complete:
- foundation (const_type opcode, interp variant, helpers) — 4.0
- interp reflection arms (type_name / type_eq / has_impl) — 4.1
- box_any/display audit + bitcast guard — 4.2
- source-language construction via $args[$i] — 4.3

Step 5 (generic Into(Block) impl in stdlib) is now fully
unblocked — its trampoline body can interpolate per-mono types
both in type positions AND in expression positions.
2026-05-27 18:52:41 +03:00

45 lines
1.7 KiB
Plaintext

// Variadic heterogeneous type packs — step 4 source-construction
// path: `$args[$i]` in expression position yields a comptime Type
// VALUE (`Value.type_tag(TypeId)` in the interp). Lets builders +
// pack-fn bodies dispatch on the i-th pack type at compile time.
//
// Two usage shapes covered here:
//
// 1. `type_name($args[0])` — the value-form Type goes through the
// reflection intrinsic, picking up the per-mono concrete type.
//
// 2. `inline if type_eq($args[0], s64) { ... }` — compile-time
// branch over the pack element's type. The fold via
// `tryConstBoolCondition` reads $args[0] via `resolveTypeArg`,
// which the step-4 work taught to walk `pack_arg_types`.
//
// Lowering: `$args[$i]` in expression position emits a
// `const_type(arg_types[i])` IR op. The interp materialises a
// `Value.type_tag(TypeId)`. LLVM emit bails (Type is comptime-only).
#import "modules/std.sx";
show :: (..$args) -> string => type_name($args[0]);
describe :: (..$args) -> string {
inline if type_eq($args[0], s64) { return "got s64"; }
inline if type_eq($args[0], string) { return "got string"; }
inline if type_eq($args[0], bool) { return "got bool"; }
return "got other";
}
main :: () -> s32 {
// type_name picks up the per-mono first-arg type.
print("{}\n", show(42)); // s64
print("{}\n", show("hi")); // string
print("{}\n", show(3.14)); // f64
// inline-if + type_eq picks the right branch per mono.
print("{}\n", describe(42)); // got s64
print("{}\n", describe("hello")); // got string
print("{}\n", describe(true)); // got bool
print("{}\n", describe(3.14)); // got other
return 0;
}