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:
Selfhosting Dev
2025-09-20 01:51:55 +09:00
parent 126cf18e82
commit 9b9080d0a3
4 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,7 @@
local x = 1
if (x.is("Integer")) {
print(1)
} else {
print(0)
}

View File

@ -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":<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

View 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}}}
]}
]}

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_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"