This commit is contained in:
Nilton Constantino
2026-01-28 19:06:42 +00:00
parent ced7ff0607
commit f37c15f3d6
9 changed files with 191 additions and 35 deletions
@@ -7,6 +7,8 @@ use prometeu_compiler::ir_core;
#[test]
fn test_host_contract_call_lowering() {
let code = "
declare contract Gfx host {}
declare contract Log host {}
fn main() {
Gfx.clear(0);
Log.write(\"Hello\");
@@ -31,9 +33,61 @@ fn test_host_contract_call_lowering() {
assert!(instrs.iter().any(|i| matches!(i, ir_core::Instr::Syscall(0x5001))));
}
#[test]
fn test_contract_call_without_host_lowering() {
let code = "
declare contract Gfx {}
fn main() {
Gfx.clear(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 not declared as host
assert!(!instrs.iter().any(|i| matches!(i, ir_core::Instr::Syscall(_))));
}
#[test]
fn test_shadowed_contract_call_lowering() {
let code = "
declare contract Gfx host {}
fn main() {
let Gfx = 10;
Gfx.clear(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 because Gfx is shadowed by a local
assert!(!instrs.iter().any(|i| matches!(i, ir_core::Instr::Syscall(_))));
}
#[test]
fn test_invalid_contract_call_lowering() {
let code = "
declare contract Gfx host {}
fn main() {
Gfx.invalidMethod(0);
}
@@ -116,6 +116,7 @@ fn test_visibility_error() {
namespace: Namespace::Type,
visibility: Visibility::FilePrivate,
ty: None,
is_host: false,
span: Span::new(1, 0, 0),
}).unwrap();
@@ -160,6 +161,7 @@ fn test_import_resolution() {
namespace: Namespace::Type,
visibility: Visibility::Pub,
ty: None,
is_host: false,
span: Span::new(1, 0, 0),
}).unwrap();
@@ -18,7 +18,9 @@ fn check_code(code: &str) -> Result<(), String> {
let code = diag.code.unwrap_or_else(|| "NO_CODE".to_string());
errors.push(format!("{}: {}", code, diag.message));
}
Err(errors.join(", "))
let err_msg = errors.join(", ");
println!("Compilation failed: {}", err_msg);
Err(err_msg)
}
}
}
@@ -101,6 +103,31 @@ fn test_unknown_type() {
assert!(res.unwrap_err().contains("E_TYPE_UNKNOWN_TYPE"));
}
#[test]
fn test_invalid_host_method() {
let code = "
declare contract Gfx host {}
fn main() {
Gfx.invalidMethod();
}
";
let res = check_code(code);
assert!(res.is_err());
assert!(res.unwrap_err().contains("E_RESOLVE_UNDEFINED"));
}
#[test]
fn test_valid_host_method() {
let code = "
declare contract Gfx host {}
fn main() {
Gfx.clear(0);
}
";
let res = check_code(code);
assert!(res.is_ok());
}
#[test]
fn test_void_return_ok() {
let code = "fn main() { return; }";