pr 03.C.1

This commit is contained in:
bQUARKz 2026-02-04 18:07:06 +00:00
parent 4c97430ea4
commit 5629f76f74
Signed by: bquarkz
SSH Key Fingerprint: SHA256:Z7dgqoglWwoK6j6u4QC87OveEq74WOhFN+gitsxtkf8
2 changed files with 51 additions and 11 deletions

View File

@ -1263,16 +1263,7 @@ mod tests {
use crate::ir_core;
use prometeu_analysis::NameInterner;
#[test]
fn test_basic_lowering() {
let code = "
fn add(a: int, b: int): int {
return a + b;
}
fn main() {
let x = add(10, 20);
}
";
fn lower_program(code: &str) -> ir_core::Program {
let mut interner = NameInterner::new();
let mut parser = Parser::new(code, 0, &mut interner);
let parsed = parser.parse_file().expect("Failed to parse");
@ -1285,7 +1276,20 @@ mod tests {
let imported = ModuleSymbols::new();
let lowerer = Lowerer::new(&parsed.arena, &module_symbols, &imported, &interner);
let program = lowerer.lower_file(parsed.root, "test").expect("Lowering failed");
lowerer.lower_file(parsed.root, "test").expect("Lowering failed")
}
#[test]
fn test_basic_lowering() {
let code = "
fn add(a: int, b: int): int {
return a + b;
}
fn main() {
let x = add(10, 20);
}
";
let program = lower_program(code);
// Verify program structure
assert_eq!(program.modules.len(), 1);
@ -1303,6 +1307,42 @@ mod tests {
assert!(first_block.instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Add)));
}
#[test]
fn test_binary_ops_lowering() {
let code = "
fn main() {
let a = 1 + 2;
let b = 3 - 1;
let c = 2 * 3;
let d = 8 / 2;
let e = 1 == 1;
let f = 1 < 2;
let g = 2 > 1;
let h = true && false;
let i = true || false;
}
";
let program = lower_program(code);
let module = &program.modules[0];
let main_func = module.functions.iter().find(|f| f.name == "main").unwrap();
let instrs: Vec<_> = main_func
.blocks
.iter()
.flat_map(|b| b.instrs.iter())
.collect();
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Add)));
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Sub)));
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Mul)));
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Div)));
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Eq)));
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Lt)));
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Gt)));
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::And)));
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Or)));
}
#[test]
fn test_control_flow_lowering() {
let code = "