-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
MastForest
merging (#1534)
- Loading branch information
1 parent
dc41735
commit 9f9cc63
Showing
10 changed files
with
2,036 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use miette::{IntoDiagnostic, Report}; | ||
use vm_core::mast::{MastForest, MastForestRootMap}; | ||
|
||
use crate::{testing::TestContext, Assembler}; | ||
|
||
#[allow(clippy::type_complexity)] | ||
fn merge_programs( | ||
program_a: &str, | ||
program_b: &str, | ||
) -> Result<(MastForest, MastForest, MastForest, MastForestRootMap), Report> { | ||
let context = TestContext::new(); | ||
let module = context.parse_module_with_path("lib::mod".parse().unwrap(), program_a)?; | ||
|
||
let lib_a = Assembler::new(context.source_manager()).assemble_library([module])?; | ||
|
||
let mut assembler = Assembler::new(context.source_manager()); | ||
assembler.add_library(lib_a.clone())?; | ||
let lib_b = assembler.assemble_library([program_b])?.mast_forest().as_ref().clone(); | ||
let lib_a = lib_a.mast_forest().as_ref().clone(); | ||
|
||
let (merged, root_maps) = MastForest::merge([&lib_a, &lib_b]).into_diagnostic()?; | ||
|
||
Ok((lib_a, lib_b, merged, root_maps)) | ||
} | ||
|
||
/// Tests that an assembler-produced library's forests can be merged and that external nodes are | ||
/// replaced by their referenced procedures. | ||
#[test] | ||
fn mast_forest_merge_assembler() { | ||
let lib_a = r#" | ||
export.foo | ||
push.19 | ||
end | ||
export.qux | ||
swap drop | ||
end | ||
"#; | ||
|
||
let lib_b = r#" | ||
use.lib::mod | ||
export.qux_duplicate | ||
swap drop | ||
end | ||
export.bar | ||
push.2 | ||
if.true | ||
push.3 | ||
else | ||
while.true | ||
add | ||
push.23 | ||
end | ||
end | ||
exec.mod::foo | ||
end"#; | ||
|
||
let (forest_a, forest_b, merged, root_maps) = merge_programs(lib_a, lib_b).unwrap(); | ||
|
||
for (forest_idx, forest) in [forest_a, forest_b].into_iter().enumerate() { | ||
for root in forest.procedure_roots() { | ||
let original_digest = forest.nodes()[root.as_usize()].digest(); | ||
let new_root = root_maps.map_root(forest_idx, root).unwrap(); | ||
let new_digest = merged.nodes()[new_root.as_usize()].digest(); | ||
assert_eq!(original_digest, new_digest); | ||
} | ||
} | ||
|
||
// Assert that the external node for the import was removed during merging. | ||
merged.nodes().iter().for_each(|node| assert!(!node.is_external())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.