Refactoring, block scoping

This commit is contained in:
2023-11-08 23:12:53 -06:00
parent 9e38ddac5d
commit 34c953c727
2 changed files with 190 additions and 72 deletions
+30
View File
@@ -16,6 +16,13 @@ fn main_return(src: &str) -> i32 {
cpu.pop_data().into()
}
fn error_message(src: &str) -> Option<String> {
match forge_core::compiler::build_boot(src) {
Ok(_) => None,
Err(forge_core::compiler::CompileError(_, _, s)) => Some(s)
}
}
#[test]
fn basic_forge_test() {
assert_eq!(
@@ -50,4 +57,27 @@ fn arg_test() {
fn main() { return sub(5, 3); }"),
2
)
}
#[test]
fn scope_test() {
// Refer to a var that's now out of scope, to ensure block scoping works
assert_eq!(
error_message(
"fn main() {
if (1) { var a = 3; } // scoped to the block!
return a; // should error!
}"),
Some("Unknown name a".into())
);
// Refer to a var that's still in scope, to ensure we're not wiping too much out
assert_eq!(
error_message(
"fn main(a) {
if (1) { var b = 3; } // scoped to the block!
return a; // should work, we didn't remove the thing that dropped
}"),
None
)
}