use std::fs; use std::path::Path; use prometeu_compiler::compiler::compile; use prometeu_bytecode::v0::BytecodeLoader; use prometeu_bytecode::disasm::disasm; use prometeu_compiler::frontends::pbs::parser::Parser; use prometeu_compiler::frontends::pbs::ast::Node; #[test] fn generate_canonical_goldens() { println!("CWD: {:?}", std::env::current_dir().unwrap()); let project_dir = Path::new("../../test-cartridges/canonical"); if !project_dir.exists() { // Fallback for when running from project root (some IDEs/environments) let project_dir = Path::new("test-cartridges/canonical"); if !project_dir.exists() { panic!("Could not find project directory at ../../test-cartridges/canonical or test-cartridges/canonical"); } } // We need a stable path for the actual compilation which might use relative paths internally let project_dir = if Path::new("../../test-cartridges/canonical").exists() { Path::new("../../test-cartridges/canonical") } else { Path::new("test-cartridges/canonical") }; let unit = compile(project_dir).map_err(|e| { println!("Compilation Error: {}", e); e }).expect("Failed to compile canonical cartridge"); let golden_dir = project_dir.join("golden"); fs::create_dir_all(&golden_dir).unwrap(); // 1. Bytecode (.pbc) fs::write(golden_dir.join("program.pbc"), &unit.rom).unwrap(); // 2. Disassembly let module = BytecodeLoader::load(&unit.rom).expect("Failed to load BytecodeModule"); let instrs = disasm(&module.code).expect("Failed to disassemble"); let mut disasm_text = String::new(); for instr in instrs { let operands_str = instr.operands.iter() .map(|o| format!("{:?}", o)) .collect::>() .join(" "); let line = if operands_str.is_empty() { format!("{:04X} {:?}\n", instr.pc, instr.opcode) } else { format!("{:04X} {:?} {}\n", instr.pc, instr.opcode, operands_str.trim()) }; disasm_text.push_str(&line); } fs::write(golden_dir.join("program.disasm.txt"), disasm_text).unwrap(); // 3. AST JSON let source = fs::read_to_string(project_dir.join("src/main/modules/main.pbs")).unwrap(); let mut parser = Parser::new(&source, 0); let ast = parser.parse_file().expect("Failed to parse AST"); let ast_node = Node::File(ast); let ast_json = serde_json::to_string_pretty(&ast_node).unwrap(); fs::write(golden_dir.join("ast.json"), ast_json).unwrap(); println!("Golden artifacts generated in test-cartridges/canonical/golden/"); }