fix: dependency hoisting for cyclic functions

Add more tests
This commit is contained in:
microproofs 2023-09-24 21:15:12 -04:00 committed by Kasey
parent 2f80d07132
commit 1cab479b81
2 changed files with 55 additions and 17 deletions

View File

@ -3100,24 +3100,11 @@ impl<'a> CodeGenerator<'a> {
}
HoistableFunction::Link(_) => todo!("Deal with Link later"),
HoistableFunction::CyclicLink(cyclic_func) => {
let (_, HoistableFunction::CyclicFunction { deps, .. }) = functions_to_hoist
.get(cyclic_func)
.unwrap()
.get("")
.unwrap()
else {
unreachable!()
};
for (dep_generic_func, dep_variant) in deps.iter() {
if !(dep_generic_func == &dep.0 && dep_variant == &dep.1) {
sorted_dep_vec.retain(|(generic_func, variant)| {
!(generic_func == dep_generic_func && variant == dep_variant)
!(generic_func == cyclic_func && variant.is_empty())
});
deps_vec.insert(0, (dep_generic_func.clone(), dep_variant.clone()));
}
}
deps_vec.insert(0, (cyclic_func.clone(), "".to_string()));
}
}
}

View File

@ -0,0 +1,51 @@
type Schema {
Integer(Int)
List(List<Schema>)
Constr(Int, Schema)
}
fn sum_constr(tag: Int, fields: Schema) -> Int {
tag + sum(fields)
}
fn sum(schema: Schema) -> Int {
when schema is {
Integer(i) -> i
List(xs) -> sum_list(xs)
Constr(tag, fields) -> sum_constr(tag, fields)
}
}
fn sum_list(list: List<Schema>) -> Int {
when list is {
[] -> 0
[x, ..xs] -> sum(x) + sum_list(xs)
}
}
test bar() {
sum(List([List([Integer(1), Integer(2)]), Integer(3), Integer(4)])) == 10
}
fn prod(schema: Schema) -> Int {
when schema is {
Integer(i) -> i
List(xs) -> prod_list(xs)
Constr(tag, fields) -> prod_constr(tag, fields)
}
}
fn prod_constr(tag: Int, fields: Schema) -> Int {
tag * prod(fields) + sum(fields)
}
fn prod_list(list: List<Schema>) -> Int {
when list is {
[] -> 1
[x, ..xs] -> prod(x) * prod_list(xs)
}
}
test sum_prod() {
prod(List([List([Integer(1), Integer(2)]), Integer(3), Integer(4)])) == 24
}