macro(if normalize): add return-if golden; llvm: extend PHI hygiene smoke to if-return and type_is cases

This commit is contained in:
Selfhosting Dev
2025-09-20 01:55:45 +09:00
parent 9b9080d0a3
commit a5a57e3d8b
4 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,4 @@
function main() {
return if (1 < 2) { 10 } else { 20 }
}

View File

@ -0,0 +1,10 @@
{"kind":"Program","statements":[
{"kind":"FunctionDeclaration","name":"main","params":[],"body":[
{"kind":"If","condition":{"kind":"BinaryOp","op":"<","left":{"kind":"Literal","value":{"type":"int","value":1}},"right":{"kind":"Literal","value":{"type":"int","value":2}}},"then":[
{"kind":"Return","value":{"kind":"Literal","value":{"type":"int","value":10}}}
],"else":[
{"kind":"Return","value":{"kind":"Literal","value":{"type":"int","value":20}}}
]}
],"static":false,"override":false}
]}

View File

@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -euo pipefail
root=$(cd "$(dirname "$0")"/../../../.. && pwd)
bin="$root/target/release/nyash"
src="apps/tests/macro_golden_if_return.nyash"
golden="$root/tools/test/golden/macro/if_return.expanded.json"
if [ ! -x "$bin" ]; then
echo "nyash binary not found at $bin; build first (cargo build --release)" >&2
exit 1
fi
export NYASH_MACRO_ENABLE=1
export NYASH_MACRO_PATHS="apps/macros/examples/if_match_normalize_macro.nyash"
normalize_json() {
python3 -c 'import sys,json; print(json.dumps(json.loads(sys.stdin.read()), sort_keys=True, separators=(",",":")))'
}
out_raw=$("$bin" --dump-expanded-ast-json "$src")
out_norm=$(printf '%s' "$out_raw" | normalize_json)
gold_norm=$(normalize_json < "$golden")
if [ "$out_norm" != "$gold_norm" ]; then
echo "Golden mismatch (if-return normalization)" >&2
diff -u <(echo "$out_norm") <(echo "$gold_norm") || true
exit 2
fi
echo "[OK] golden if-return normalization matched"

View File

@ -42,10 +42,11 @@ check_case() {
check_case "apps/tests/macro_golden_if_assign.nyash"
check_case "apps/tests/macro_golden_if_print.nyash"
check_case "apps/tests/macro_golden_if_return.nyash"
check_case "apps/tests/macro_golden_type_is_basic.nyash"
if [ "$fails" -ne 0 ]; then
exit 2
fi
echo "[OK] LLVM PHI hygiene for If-cases passed"
exit 0