|
| 1 | +# Guided ternary examples |
| 2 | + |
| 3 | +The goal for these examples is to teach both humans and AI agents how the plugin lowers C/C++ to the helper ABI and what capabilities the ternary runtime exposes. Each example builds on the previous one, linking a code pattern to a helper set, plugin flags, and the roadmap phase it exercises. |
| 4 | + |
| 5 | +| Tier | Example | Source | Plugin flags | Key helpers / semantics | Roadmap focus | |
| 6 | +|------|---------|--------|--------------|-------------------------|---------------| |
| 7 | +| 1 | **Hello ternary select** — write a ternary condition and select between two `int` values so new users see how `TERNARY_COND_T` flows into `__ternary_select_i32`. | `examples/example1_select.c` | `-fplugin-arg-ternary_plugin-lower` | `__ternary_create_cond` (implicit), `__ternary_select_i32` | Phase 0/2 (C surface) | |
| 8 | +| 2 | **Binary-to-ternary conversion** — call `__ternary_tb2t_t32`/`__ternary_tt2b_t32` plus `tb2t` literal helper to show how binary ints become packed trits and back. | `examples/example2_conversion.c` | `-fplugin-arg-ternary_plugin-conv` | `__ternary_tb2t_t32`, `__ternary_tt2b_t32`, `__ternary_bt_str_t32` | Phase 1 (runtime conversions) | |
| 9 | +| 3 | **Arithmetic chain** — add/mul/sub/muladd a few ternary values so the lowered helpers (`__ternary_add_t32`, `__ternary_mul_t32`, `__ternary_tmuladd_t32`) appear in sequence. | `examples/example3_arithmetic.c` | `-fplugin-arg-ternary_plugin-arith` | math helpers + `__ternary_tround_t32` for rounding | Phase 3 (lowering coverage) | |
| 10 | +| 4 | **Logic/min/max** — compute `tmin`, `tmax`, `tequiv`, `txor` on packed inputs to illustrate ternary logic semantics. | `examples/example4_logic.c` | `-fplugin-arg-ternary_plugin-logic` | `__ternary_tmin_t32`, `__ternary_tequiv_t32`, `__ternary_txor_t32` | Phase 3 | |
| 11 | +| 5 | **Conditional branch pipeline** — use `if (cond == 1)`, `else if` etc. to trigger `__ternary_tbranch` steps, showing how select/branch helpers pair with `tsignjmp`. | `examples/example5_branch.c` | `-fplugin-arg-ternary_plugin-lower -fplugin-arg-ternary_plugin-shift` | `__ternary_tbranch`, `__ternary_tsignjmp_t32`, `__ternary_shl_t32` | Phase 3/4 (control flow) | |
| 12 | +| 6 | **Vector arithmetic** — operate on `tv32_t` (or `tv64_t`) values so `__ternary_add_tv32`/`__ternary_cmp_tv32` appear; mention how the runtime currently folds them to scalar helpers. | `examples/example6_vector.c` | `-fplugin-arg-ternary_plugin-vector` | `__ternary_add_tv32`, `__ternary_cmp_tv32`, fallback to `__ternary_cmplt_t32` | Phase 7 (SIMD exploration) | |
| 13 | +| 7 | **Shift/rotate pipeline** — combine `__ternary_shl_t32`, `__ternary_shr_t32`, `__ternary_rol_t32`, `__ternary_ror_t32` to demonstrate trit-level shifts. | `examples/example7_shift.c` | `-fplugin-arg-ternary_plugin-shift` | shift/rotate helpers | Phase 4 (validation) | |
| 14 | +| 8 | **Memory load/store** — show how `t32_t` values persist through `__ternary_load_t32`/`__ternary_store_t32`, so the ABI matches binary-compatible storage. | `examples/example8_memory.c` | `-fplugin-arg-ternary_plugin-mem` | `__ternary_load_t32`, `__ternary_store_t32` | Phase 3 | |
| 15 | +| 9 | **TMUX/TNET routing** — an AI-relevant kernel that gathers three paths (neg/zero/pos) via `__ternary_tmux_t32` and summarizes with `__ternary_tnet_t32`. | `examples/example9_tmux.c` | `-fplugin-arg-ternary_plugin-logic -fplugin-arg-ternary_plugin-arith` | `__ternary_tmux_t32`, `__ternary_tnet_t32`, `__ternary_tequiv_t32` | Phase 4 (ternary-specific ISA) | |
| 16 | +| 10 | **Coverage-driven diagnostics** — run the quickstart script to build the above examples and capture the `tests/run_phase34_coverage.sh` trace log so AI/engineers know which helpers emit diagnostics. | `tools/quickstart.sh` | n/a (script) | coverage log, helper list audit (`tools/check_helper_docs.py`) | Phase 3/4 regression | |
| 17 | + |
| 18 | +Each example should include a minimal C/C++ snippet (see `examples/`), the plugin command line, and the helpers it pushes into the runtime. Adding these to the documentation (and ideally as runnable snippets in `examples/`) helps both humans and agents understand the progression from simple selects to complex routing/diagnostics. |
0 commit comments