macro(if normalize): switch to ControlFlowBuilder for If stmt construction; llvm: add PHI hygiene smoke for If cases

This commit is contained in:
Selfhosting Dev
2025-09-20 01:30:15 +09:00
parent 4229e4259e
commit 126cf18e82
2 changed files with 55 additions and 3 deletions

View File

@ -8,6 +8,7 @@ static box MacroBoxSpec {
expand(json, ctx) { expand(json, ctx) {
local JB = include "apps/lib/json_builder.nyash" local JB = include "apps/lib/json_builder.nyash"
local CF = include "apps/lib/cf_builder.nyash"
// --- helpers copied/adapted from loop_normalize --- // --- helpers copied/adapted from loop_normalize ---
function parse_value(s, i) { function parse_value(s, i) {
@ -225,7 +226,7 @@ static box MacroBoxSpec {
local then_s = [ JB.assignment(tgt, th_e) ] local then_s = [ JB.assignment(tgt, th_e) ]
local else_s = null local else_s = null
if el_e != null { else_s = [ JB.assignment(tgt, el_e) ] } if el_e != null { else_s = [ JB.assignment(tgt, el_e) ] }
return JB.if_(cond, then_s, else_s) return CF.if_stmt(cond, then_s, else_s)
} }
} }
// Return with If value → If(Return ...) // Return with If value → If(Return ...)
@ -242,7 +243,7 @@ static box MacroBoxSpec {
local then_s = [ JB.return_(th_e) ] local then_s = [ JB.return_(th_e) ]
local else_s = null local else_s = null
if el_e != null { else_s = [ JB.return_(el_e) ] } if el_e != null { else_s = [ JB.return_(el_e) ] }
return JB.if_(cond, then_s, else_s) return CF.if_stmt(cond, then_s, else_s)
} }
} }
// Print with If expression → If(Print ...) // Print with If expression → If(Print ...)
@ -259,7 +260,7 @@ static box MacroBoxSpec {
local then_s = [ JB.print_(th_e) ] local then_s = [ JB.print_(th_e) ]
local else_s = null local else_s = null
if el_e != null { else_s = [ JB.print_(el_e) ] } if el_e != null { else_s = [ JB.print_(el_e) ] }
return JB.if_(cond, then_s, else_s) return CF.if_stmt(cond, then_s, else_s)
} }
} }
// Recurse for If/Loop nodes // Recurse for If/Loop nodes

View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
set -euo pipefail
root=$(cd "$(dirname "$0")"/../../../.. && pwd)
bin="$root/target/release/nyash"
if [ ! -x "$bin" ]; then
echo "nyash binary not found at $bin; build first (cargo build --release --features llvm)" >&2
exit 1
fi
export NYASH_MACRO_ENABLE=1
export NYASH_MACRO_PATHS="apps/macros/examples/if_match_normalize_macro.nyash"
export NYASH_LLVM_USE_HARNESS=1
fails=0
check_case() {
local src="$1"
local irfile="$root/tmp/$(basename "$src" .nyash)_llvm.ll"
mkdir -p "$root/tmp"
NYASH_LLVM_DUMP_IR="$irfile" "$bin" --backend llvm "$src" >/dev/null 2>&1 || {
echo "[FAIL] LLVM run failed for $src" >&2
fails=$((fails+1))
return
}
if [ ! -s "$irfile" ]; then
echo "[FAIL] IR not dumped for $src" >&2
fails=$((fails+1))
return
}
local empty_cnt
empty_cnt=$(rg -n "\\bphi\\b" "$irfile" | rg -v "\\[" | wc -l | tr -d ' ')
if [ "${empty_cnt:-0}" != "0" ]; then
echo "[FAIL] Empty PHI detected in $irfile" >&2
rg -n "\\bphi\\b" "$irfile" | rg -v "\\[" || true
fails=$((fails+1))
return
fi
echo "[OK] PHI hygiene (no empty PHI): $(basename "$irfile")"
}
check_case "apps/tests/macro_golden_if_assign.nyash"
check_case "apps/tests/macro_golden_if_print.nyash"
if [ "$fails" -ne 0 ]; then
exit 2
fi
echo "[OK] LLVM PHI hygiene for If-cases passed"
exit 0