Files
hakorune/tools/selfhost
nyash-codex 701f1fd650 feat(joinir): Phase 164 Pattern3 (If-Else PHI) validation complete
- Created 4 representative test cases for Pattern3 patterns:
  * test_pattern3_if_phi_no_break.hako - Core Pattern3 (if-else PHI, no break/continue)
  * test_pattern3_skip_whitespace.hako - Pattern3+break style (routed to Pattern2)
  * test_pattern3_trim_leading.hako - Pattern3+break style (routed to Pattern2)
  * test_pattern3_trim_trailing.hako - Pattern3+break style (routed to Pattern2)

- Validated Pattern3_WithIfPhi detection:
  * Pattern routing: Pattern3_WithIfPhi MATCHED confirmed
  * JoinIR lowering: 3 functions, 20 blocks → 8 blocks (successful)
  * [joinir/freeze] elimination: Complete (no errors on any test)

- Clarified pattern classification:
  * Pattern3_WithIfPhi handles if-else PHI without break/continue
  * Loops with "if-else PHI + break" are routed to Pattern2_WithBreak
  * Break takes priority over if-else PHI in pattern detection

- Cumulative achievement (Phase 162-164):
  * Pattern1: 6 loops working 
  * Pattern2: 5 loops working 
  * Pattern3 (no break): 1 loop working 
  * Pattern3+break (as Pattern2): 3 loops working 
  * Total: 15 loops covered, zero [joinir/freeze] errors

- Updated CURRENT_TASK.md with Phase 164 section and findings

Next: Phase 165 Pattern4 (continue) validation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 16:22:38 +09:00
..

Hybrid Selfhost Build (80/20)

Purpose

  • Provide a minimal, fast path to compile Hako source via Hakorune StageB to Program(JSON v0), and optionally run it via CoreDirect (inproc).
  • Future: add MIR emit and ny-llvmc EXE build in small increments.

Script

  • tools/selfhost/selfhost_build.sh
    • --in <file.hako>: input Hako source
    • --json <out.json>: write Program(JSON v0); default: /tmp/hako_stageb_$$.json
    • --mir <out.json>: emit MIR(JSON) from source (runner path)
    • --exe : build native executable via ny-llvmc (llvmlite harness)
    • --run: run via GateC/Core Direct (inproc). Exit code mirrors program return.
    • Env:
      • NYASH_BIN: path to hakorune/nyash binary (auto-detected)
      • NYASH_ROOT: repo root (auto-detected)
      • HAKO_USE_BUILDBOX=1: use BuildBox for emit-only (no run/exe)

Examples

# Emit JSON only (StageB)
tools/selfhost/selfhost_build.sh --in apps/demo/main.hako --json /tmp/demo.json

# Run and use exit code
tools/selfhost/selfhost_build.sh --in apps/demo/return7.hako --run; echo $?

Notes

  • StageB emit uses either the StageB entry or BuildBoxHAKO_USE_BUILDBOX=1 for emit-only
  • Runner executes CoreDirect in-proc under HAKO_CORE_DIRECT_INPROC=1.
  • For heavier cases (bundles/alias/require), keep StageB canaries optin in quick profile.

Stage1 Selfhost Binary (Phase 25.1 — initial wiring)

Purpose

  • Provide a concrete Stage1 binary (hakorune) built from Hako sources.
  • Separate the Rust bootstrap binary (Stage0, nyash) from the Hakorune selfhost binary at the build-script level.

Script

  • tools/selfhost/build_stage1.sh
    • Builds a Stage1 selfhost executable from a Nyash/Hako entry point.
    • Current entry (default):
      • lang/src/runner/launcher.hako — Stage1 CLI launcher (commands: emit program-json/mir-json, etc.).
    • Output:
      • target/selfhost/hakorune by default.

Usage

# Build Stage1 selfhost binary with defaults
tools/selfhost/build_stage1.sh

# Custom output path
tools/selfhost/build_stage1.sh --out /tmp/hakorune-dev

# Custom entry (experimental)
tools/selfhost/build_stage1.sh --entry apps/selfhost-minimal/main.hako --out /tmp/hako_min

How it works

  • Pipeline:
    1. StageB + MirBuilder:
      • tools/hakorune_emit_mir.sh <entry.hako> <mir.json>
    2. LLVM EXE build:
      • tools/ny_mir_builder.sh --in <mir.json> --emit exe -o <exe>
  • The Rust binary (Stage0) is resolved via the existing helpers inside hakorune_emit_mir.sh / ny_mir_builder.sh:
    • Prefers target/release/hakorune when present.
    • Falls back to target/release/nyash otherwise.

Notes

  • This Stage1 binary is a minimal Ny Executor and not yet a full CLI replacement.
  • Full CLI / mode selection for Stage1 will be implemented in later phases; this script focuses on establishing the binary layout and build wiring.

Helper — Stage1 CLI Runner

  • tools/selfhost/run_stage1_cli.sh
    • Wraps a Stage1 binary (default target/selfhost/hakorune) with the required runtime env:
      • NYASH_NYRT_SILENT_RESULT=1Result 行を抑止して JSON stdout を維持)
      • NYASH_DISABLE_PLUGINS=1, NYASH_FILEBOX_MODE=core-roFileBox などのコア実装を強制)
    • Pass arguments verbatim to the Stage1 CLI:
      tools/selfhost/run_stage1_cli.sh emit program-json apps/tests/minimal.hako
      tools/selfhost/run_stage1_cli.sh --bin /tmp/hakorune-dev emit mir-json apps/tests/minimal.hako
      
    • Use this helper (or set the env vars manually) whenever CLI output is consumed by scripts, so that stdout matches llvmlite harness expectations.