macro/pattern: add type_is basic golden (MethodCall .is → MIR TypeOp mapping); docs: AST JSON v0 note for is/as mapping
This commit is contained in:
7
apps/tests/macro_golden_type_is_basic.nyash
Normal file
7
apps/tests/macro_golden_type_is_basic.nyash
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
local x = 1
|
||||||
|
if (x.is("Integer")) {
|
||||||
|
print(1)
|
||||||
|
} else {
|
||||||
|
print(0)
|
||||||
|
}
|
||||||
|
|
||||||
@ -44,6 +44,11 @@ Binary operators
|
|||||||
Notes
|
Notes
|
||||||
- The schema is intentionally minimal; it covers nodes needed for Phase 2 samples.
|
- The schema is intentionally minimal; it covers nodes needed for Phase 2 samples.
|
||||||
- Future: add `span`, `attrs`, typed annotations as needed.
|
- 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":<expr>,"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
|
## Example
|
||||||
|
|
||||||
|
|||||||
9
tools/test/golden/macro/type_is_basic.expanded.json
Normal file
9
tools/test/golden/macro/type_is_basic.expanded.json
Normal file
@ -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}}}
|
||||||
|
]}
|
||||||
|
]}
|
||||||
|
|
||||||
32
tools/test/golden/macro/type_is_basic_user_macro_golden.sh
Normal file
32
tools/test/golden/macro/type_is_basic_user_macro_golden.sh
Normal 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_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"
|
||||||
|
|
||||||
Reference in New Issue
Block a user