feat(mir-builder): add using imports support to json_v0_bridge
Phase 21.8 foundation for MatI64/IntArrayCore integration Changes: - Add `imports: HashMap<String, String>` to BridgeEnv - Extend MapVars::resolve() to check imports and create static box references - Add BridgeEnv::with_imports() to initialize with using imports map - Add parse_json_v0_to_module_with_imports() to json_v0_bridge - Add program_json_to_mir_json_with_imports() to mir_builder.rs - Maintain backward compatibility via empty HashMap defaults Next: Wire using extraction from pipeline and test with MatI64 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -1,11 +1,17 @@
|
||||
use crate::runner;
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
|
||||
/// Convert Program(JSON v0) to MIR(JSON v0) and return it as a String.
|
||||
/// Fail-Fast: prints stable tags on stderr and returns Err with the same tag text.
|
||||
pub fn program_json_to_mir_json(program_json: &str) -> Result<String, String> {
|
||||
program_json_to_mir_json_with_imports(program_json, HashMap::new())
|
||||
}
|
||||
|
||||
/// Convert Program(JSON v0) to MIR(JSON v0) with using imports support.
|
||||
pub fn program_json_to_mir_json_with_imports(program_json: &str, imports: HashMap<String, String>) -> Result<String, String> {
|
||||
// Basic header check
|
||||
if !program_json.contains("\"version\"") || !program_json.contains("\"kind\"") {
|
||||
let tag = "[mirbuilder/input/invalid] missing version/kind keys";
|
||||
@ -13,8 +19,8 @@ pub fn program_json_to_mir_json(program_json: &str) -> Result<String, String> {
|
||||
return Err(tag.into());
|
||||
}
|
||||
|
||||
// Parse Program(JSON v0) into a MIR Module
|
||||
let module = match runner::json_v0_bridge::parse_json_v0_to_module(program_json) {
|
||||
// Parse Program(JSON v0) into a MIR Module with imports
|
||||
let module = match runner::json_v0_bridge::parse_json_v0_to_module_with_imports(program_json, imports) {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
let tag = format!("[mirbuilder/parse/error] {}", e);
|
||||
|
||||
@ -98,10 +98,16 @@ pub(super) struct BridgeEnv {
|
||||
pub(super) allow_me_dummy: bool,
|
||||
pub(super) me_class: String,
|
||||
pub(super) try_result_mode: bool,
|
||||
// Phase 21.8: using imports map (alias -> box_type)
|
||||
pub(super) imports: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl BridgeEnv {
|
||||
pub(super) fn load() -> Self {
|
||||
Self::with_imports(HashMap::new())
|
||||
}
|
||||
|
||||
pub(super) fn with_imports(imports: HashMap<String, String>) -> Self {
|
||||
let trm = crate::config::env::try_result_mode();
|
||||
// フェーズM.2: no_phi変数削除
|
||||
if crate::config::env::cli_verbose() {
|
||||
@ -113,6 +119,7 @@ impl BridgeEnv {
|
||||
allow_me_dummy: std::env::var("NYASH_BRIDGE_ME_DUMMY").ok().as_deref() == Some("1"),
|
||||
me_class: std::env::var("NYASH_BRIDGE_ME_CLASS").unwrap_or_else(|_| "Main".to_string()),
|
||||
try_result_mode: trm,
|
||||
imports,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -250,11 +257,11 @@ pub(super) fn lower_stmt_list_with_vars(
|
||||
Ok(cur)
|
||||
}
|
||||
|
||||
pub(super) fn lower_program(prog: ProgramV0) -> Result<MirModule, String> {
|
||||
pub(super) fn lower_program(prog: ProgramV0, imports: std::collections::HashMap<String, String>) -> Result<MirModule, String> {
|
||||
if prog.body.is_empty() {
|
||||
return Err("empty body".into());
|
||||
}
|
||||
let env = BridgeEnv::load();
|
||||
let env = BridgeEnv::with_imports(imports);
|
||||
let mut module = MirModule::new("ny_json_v0".into());
|
||||
let sig = FunctionSignature {
|
||||
name: "main".into(),
|
||||
|
||||
@ -51,6 +51,20 @@ impl<'a> VarScope for MapVars<'a> {
|
||||
if let Some(&vid) = self.vars.get(name) {
|
||||
return Ok(Some(vid));
|
||||
}
|
||||
// Phase 21.8: Check using-imported modules/boxes
|
||||
if let Some(box_type) = env.imports.get(name) {
|
||||
let dst = f.next_value_id();
|
||||
if let Some(bb) = f.get_block_mut(cur_bb) {
|
||||
// Treat as static box reference - create a const string representing the box type
|
||||
bb.add_instruction(MirInstruction::Const {
|
||||
dst,
|
||||
value: ConstValue::String(box_type.clone()),
|
||||
});
|
||||
}
|
||||
// Cache the resolution for subsequent uses
|
||||
self.vars.insert(name.to_string(), dst);
|
||||
return Ok(Some(dst));
|
||||
}
|
||||
if name == "me" {
|
||||
if env.allow_me_dummy {
|
||||
let dst = f.next_value_id();
|
||||
|
||||
@ -4,8 +4,13 @@ mod lowering;
|
||||
|
||||
use ast::{ProgramV0, StmtV0};
|
||||
use lowering::lower_program;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn parse_json_v0_to_module(json: &str) -> Result<crate::mir::MirModule, String> {
|
||||
parse_json_v0_to_module_with_imports(json, HashMap::new())
|
||||
}
|
||||
|
||||
pub fn parse_json_v0_to_module_with_imports(json: &str, imports: HashMap<String, String>) -> Result<crate::mir::MirModule, String> {
|
||||
let prog: ProgramV0 =
|
||||
serde_json::from_str(json).map_err(|e| format!("invalid JSON v0: {}", e))?;
|
||||
if crate::config::env::cli_verbose() {
|
||||
@ -19,7 +24,7 @@ pub fn parse_json_v0_to_module(json: &str) -> Result<crate::mir::MirModule, Stri
|
||||
if prog.version != 0 || prog.kind != "Program" {
|
||||
return Err("unsupported IR: expected {version:0, kind:\"Program\"}".into());
|
||||
}
|
||||
lower_program(prog)
|
||||
lower_program(prog, imports)
|
||||
}
|
||||
|
||||
pub fn parse_source_v0_to_module(input: &str) -> Result<crate::mir::MirModule, String> {
|
||||
|
||||
Reference in New Issue
Block a user