diff --git a/tools/hakorune_emit_mir.sh b/tools/hakorune_emit_mir.sh index 5efd3594..5fa04ef2 100644 --- a/tools/hakorune_emit_mir.sh +++ b/tools/hakorune_emit_mir.sh @@ -44,6 +44,26 @@ CODE_TMP=$(mktemp --suffix=.hako) trap 'rm -f "$CODE_TMP" || true' EXIT cp "$IN" "$CODE_TMP" +# Phase 21.8: Extract using imports and build JSON map for MirBuilder +# This enables MirBuilder to recognize static box references like MatI64, IntArrayCore +IMPORTS_JSON="{}" +if [ "${NYASH_ENABLE_USING:-1}" = "1" ] || [ "${HAKO_ENABLE_USING:-1}" = "1" ]; then + # Extract "using X as Y" lines and build JSON map {"Y":"Y"} + # Example: "using nyash.core.numeric.matrix_i64 as MatI64" -> "MatI64":"MatI64" + USING_LINES=$(grep -E '^\s*using\s+.*\s+as\s+' "$CODE_TMP" || true) + if [ -n "$USING_LINES" ]; then + IMPORTS_JSON=$(echo "$USING_LINES" | \ + sed -E 's/^\s*using\s+.*\s+as\s+([A-Za-z0-9_]+).*/"\1":"\1"/' | \ + paste -sd ',' | \ + sed 's/^/{/' | sed 's/$/}/') + fi + # Fallback to empty object if extraction failed or produced invalid JSON + if [ -z "$IMPORTS_JSON" ] || [ "$IMPORTS_JSON" = "{}" ]; then + IMPORTS_JSON="{}" + fi +fi +export HAKO_MIRBUILDER_IMPORTS="$IMPORTS_JSON" + # Check if FORCE jsonfrag mode is requested (bypasses Stage-B entirely) if [ "${HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG:-0}" = "1" ]; then # Extract limit from code using grep/awk