This commit is contained in:
Nilton Constantino
2026-01-28 18:58:31 +00:00
parent 5bd26a2991
commit ced7ff0607
21 changed files with 934 additions and 37 deletions
@@ -23,14 +23,7 @@ fn test_project_root_and_entry_resolution() {
// Call compile
let result = compiler::compile(project_dir);
// It should fail with "Frontend 'pbs' not yet fully implemented (Parser OK)"
// but ONLY after successfully loading the config and resolving the entry.
match result {
Err(e) => {
let msg = e.to_string();
assert!(msg.contains("Frontend 'pbs' not yet fully implemented (Parser OK)"), "Unexpected error: {}", msg);
}
Ok(_) => panic!("Should have failed as pbs is not implemented yet"),
}
// It should now succeed or at least fail at a later stage,
// but the point of this test is config resolution.
assert!(result.is_ok(), "Failed to compile: {:?}", result.err());
}
@@ -13,6 +13,8 @@ fn test_ir_core_manual_construction() {
functions: vec![Function {
id: FunctionId(10),
name: "entry".to_string(),
params: vec![],
return_type: Type::Void,
blocks: vec![Block {
id: 0,
instrs: vec![
@@ -43,6 +45,8 @@ fn test_ir_core_manual_construction() {
{
"id": 10,
"name": "entry",
"params": [],
"return_type": "Void",
"blocks": [
{
"id": 0,
@@ -15,6 +15,8 @@ fn test_full_lowering() {
functions: vec![ir_core::Function {
id: FunctionId(1),
name: "main".to_string(),
params: vec![],
return_type: ir_core::Type::Void,
blocks: vec![
Block {
id: 0,
@@ -0,0 +1,58 @@
use prometeu_compiler::frontends::pbs::parser::Parser;
use prometeu_compiler::frontends::pbs::collector::SymbolCollector;
use prometeu_compiler::frontends::pbs::symbols::ModuleSymbols;
use prometeu_compiler::frontends::pbs::lowering::Lowerer;
use prometeu_compiler::ir_core;
#[test]
fn test_host_contract_call_lowering() {
let code = "
fn main() {
Gfx.clear(0);
Log.write(\"Hello\");
}
";
let mut parser = Parser::new(code, 0);
let ast = parser.parse_file().expect("Failed to parse");
let mut collector = SymbolCollector::new();
let (type_symbols, value_symbols) = collector.collect(&ast).expect("Failed to collect symbols");
let module_symbols = ModuleSymbols { type_symbols, value_symbols };
let lowerer = Lowerer::new(&module_symbols);
let program = lowerer.lower_file(&ast, "test");
let func = &program.modules[0].functions[0];
let instrs: Vec<_> = func.blocks.iter().flat_map(|b| b.instrs.iter()).collect();
// Gfx.clear -> 0x1001
assert!(instrs.iter().any(|i| matches!(i, ir_core::Instr::Syscall(0x1001))));
// Log.write -> 0x5001
assert!(instrs.iter().any(|i| matches!(i, ir_core::Instr::Syscall(0x5001))));
}
#[test]
fn test_invalid_contract_call_lowering() {
let code = "
fn main() {
Gfx.invalidMethod(0);
}
";
let mut parser = Parser::new(code, 0);
let ast = parser.parse_file().expect("Failed to parse");
let mut collector = SymbolCollector::new();
let (type_symbols, value_symbols) = collector.collect(&ast).expect("Failed to collect symbols");
let module_symbols = ModuleSymbols { type_symbols, value_symbols };
let lowerer = Lowerer::new(&module_symbols);
let program = lowerer.lower_file(&ast, "test");
let func = &program.modules[0].functions[0];
let instrs: Vec<_> = func.blocks.iter().flat_map(|b| b.instrs.iter()).collect();
// Should NOT be a syscall if invalid
assert!(!instrs.iter().any(|i| matches!(i, ir_core::Instr::Syscall(_))));
// Should be a regular call (which might fail later or be a dummy)
assert!(instrs.iter().any(|i| matches!(i, ir_core::Instr::Call(_, _))));
}
@@ -0,0 +1,95 @@
use prometeu_compiler::frontends::pbs::parser::Parser;
use prometeu_compiler::frontends::pbs::collector::SymbolCollector;
use prometeu_compiler::frontends::pbs::symbols::ModuleSymbols;
use prometeu_compiler::frontends::pbs::lowering::Lowerer;
use prometeu_compiler::ir_core;
#[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 mut parser = Parser::new(code, 0);
let ast = parser.parse_file().expect("Failed to parse");
let mut collector = SymbolCollector::new();
let (type_symbols, value_symbols) = collector.collect(&ast).expect("Failed to collect symbols");
let module_symbols = ModuleSymbols { type_symbols, value_symbols };
let lowerer = Lowerer::new(&module_symbols);
let program = lowerer.lower_file(&ast, "test");
// Verify program structure
assert_eq!(program.modules.len(), 1);
let module = &program.modules[0];
assert_eq!(module.functions.len(), 2);
let add_func = module.functions.iter().find(|f| f.name == "add").unwrap();
assert_eq!(add_func.params.len(), 2);
assert_eq!(add_func.return_type, ir_core::Type::Int);
// Verify blocks
assert!(add_func.blocks.len() >= 1);
let first_block = &add_func.blocks[0];
// Check for Add instruction
assert!(first_block.instrs.iter().any(|i| matches!(i, ir_core::Instr::Add)));
}
#[test]
fn test_control_flow_lowering() {
let code = "
fn max(a: int, b: int) -> int {
if (a > b) {
return a;
} else {
return b;
}
}
";
let mut parser = Parser::new(code, 0);
let ast = parser.parse_file().expect("Failed to parse");
let mut collector = SymbolCollector::new();
let (type_symbols, value_symbols) = collector.collect(&ast).expect("Failed to collect symbols");
let module_symbols = ModuleSymbols { type_symbols, value_symbols };
let lowerer = Lowerer::new(&module_symbols);
let program = lowerer.lower_file(&ast, "test");
let max_func = &program.modules[0].functions[0];
// Should have multiple blocks for if-else
assert!(max_func.blocks.len() >= 3);
}
#[test]
fn test_hip_lowering() {
let code = "
fn test_hip() {
let g = alloc int;
mutate g as x {
let y = x + 1;
}
}
";
let mut parser = Parser::new(code, 0);
let ast = parser.parse_file().expect("Failed to parse");
let mut collector = SymbolCollector::new();
let (type_symbols, value_symbols) = collector.collect(&ast).expect("Failed to collect symbols");
let module_symbols = ModuleSymbols { type_symbols, value_symbols };
let lowerer = Lowerer::new(&module_symbols);
let program = lowerer.lower_file(&ast, "test");
let func = &program.modules[0].functions[0];
let instrs: Vec<_> = func.blocks.iter().flat_map(|b| b.instrs.iter()).collect();
assert!(instrs.iter().any(|i| matches!(i, ir_core::Instr::Alloc)));
assert!(instrs.iter().any(|i| matches!(i, ir_core::Instr::ReadGate)));
assert!(instrs.iter().any(|i| matches!(i, ir_core::Instr::WriteGate)));
}
@@ -115,6 +115,7 @@ fn test_visibility_error() {
kind: SymbolKind::Struct,
namespace: Namespace::Type,
visibility: Visibility::FilePrivate,
ty: None,
span: Span::new(1, 0, 0),
}).unwrap();
@@ -158,6 +159,7 @@ fn test_import_resolution() {
kind: SymbolKind::Struct,
namespace: Namespace::Type,
visibility: Visibility::Pub,
ty: None,
span: Span::new(1, 0, 0),
}).unwrap();
@@ -86,6 +86,8 @@ fn test_lowering_smoke() {
functions: vec![ir_core::Function {
id: FunctionId(10),
name: "start".to_string(),
params: vec![],
return_type: ir_core::Type::Void,
blocks: vec![ir_core::Block {
id: 0,
instrs: vec![