diff --git a/apps/tests/macro_golden_type_is_basic.nyash b/apps/tests/macro_golden_type_is_basic.nyash new file mode 100644 index 00000000..a0a0aff0 --- /dev/null +++ b/apps/tests/macro_golden_type_is_basic.nyash @@ -0,0 +1,7 @@ +local x = 1 +if (x.is("Integer")) { + print(1) +} else { + print(0) +} + diff --git a/docs/reference/ir/ast-json-v0.md b/docs/reference/ir/ast-json-v0.md index af967d44..d49855ee 100644 --- a/docs/reference/ir/ast-json-v0.md +++ b/docs/reference/ir/ast-json-v0.md @@ -44,6 +44,11 @@ Binary operators Notes - The schema is intentionally minimal; it covers nodes needed for Phase 2 samples. - Future: add `span`, `attrs`, typed annotations as needed. +- Type checks (is/as) mapping + - AST JSON v0 does not introduce a dedicated TypeOp node. Instead, write MethodCall with + method "is" or "as" and a single string literal type argument: + {"kind":"MethodCall","object":,"method":"is","arguments":[{"kind":"Literal","value":{"type":"string","value":"Integer"}}]} + - Lowering maps this to MIR::TypeOp(Check/ Cast) with the target type resolved by name. ## Example diff --git a/tools/test/golden/macro/type_is_basic.expanded.json b/tools/test/golden/macro/type_is_basic.expanded.json new file mode 100644 index 00000000..0f250336 --- /dev/null +++ b/tools/test/golden/macro/type_is_basic.expanded.json @@ -0,0 +1,9 @@ +{"kind":"Program","statements":[ + {"kind":"Local","variables":["x"],"inits":[{"kind":"Literal","value":{"type":"int","value":1}}]}, + {"kind":"If","condition":{"kind":"MethodCall","object":{"kind":"Variable","name":"x"},"method":"is","arguments":[{"kind":"Literal","value":{"type":"string","value":"Integer"}}]},"then":[ + {"kind":"Print","expression":{"kind":"Literal","value":{"type":"int","value":1}}} + ],"else":[ + {"kind":"Print","expression":{"kind":"Literal","value":{"type":"int","value":0}}} + ]} +]} + diff --git a/tools/test/golden/macro/type_is_basic_user_macro_golden.sh b/tools/test/golden/macro/type_is_basic_user_macro_golden.sh new file mode 100644 index 00000000..9e792df7 --- /dev/null +++ b/tools/test/golden/macro/type_is_basic_user_macro_golden.sh @@ -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_type_is_basic.nyash" +golden="$root/tools/test/golden/macro/type_is_basic.expanded.json" + +if [ ! -x "$bin" ]; then + echo "nyash binary not found at $bin; build first (cargo build --release)" >&2 + exit 1 +fi + +# Macro engine may be on or off; result is identical for this case. +export NYASH_MACRO_ENABLE=1 + +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 (type_is basic)" >&2 + diff -u <(echo "$out_norm") <(echo "$gold_norm") || true + exit 2 +fi + +echo "[OK] golden type_is basic matched" +