macro(if normalize): add print-if normalization golden and runner

This commit is contained in:
Selfhosting Dev
2025-09-20 01:29:02 +09:00
parent b7975c80db
commit 4229e4259e
3 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,2 @@
print(if (1 < 2) { 10 } else { 20 })

View File

@ -0,0 +1,8 @@
{"kind":"Program","statements":[
{"kind":"If","condition":{"kind":"BinaryOp","op":"<","left":{"kind":"Literal","value":{"type":"int","value":1}},"right":{"kind":"Literal","value":{"type":"int","value":2}}},"then":[
{"kind":"Print","expression":{"kind":"Literal","value":{"type":"int","value":10}}}
],"else":[
{"kind":"Print","expression":{"kind":"Literal","value":{"type":"int","value":20}}}
]}
]}

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_print.nyash"
golden="$root/tools/test/golden/macro/if_print.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-print normalization)" >&2
diff -u <(echo "$out_norm") <(echo "$gold_norm") || true
exit 2
fi
echo "[OK] golden if-print normalization matched"