it runs! gets the wrong answer, but runs!

This commit is contained in:
2024-02-17 09:38:12 -08:00
parent 9d41cf0da7
commit 7edaf747aa
8 changed files with 491 additions and 26 deletions

View File

@@ -482,7 +482,7 @@ impl<M: Module> Backend<M> {
let (arguments, _argument_types): (Vec<_>, Vec<_>) = args
.into_iter()
.map(|x| self.compile_value_or_ref(x, variables, builder))
.collect::<Result<Vec<(_,_)>,BackendError>>()?
.collect::<Result<Vec<(_, _)>, BackendError>>()?
.into_iter()
.unzip();
@@ -491,20 +491,29 @@ impl<M: Module> Backend<M> {
panic!("Can't use a value for a function")
}
ValueOrRef::Ref(_, result_type, name) => match self.defined_functions.get(&name) {
None => panic!("Couldn't find function {} to call", name),
Some(function) => {
let func_ref = self.module.declare_func_in_func(*function, builder.func);
let call = builder.ins().call(func_ref, &arguments);
let results = builder.inst_results(call);
match results {
[] => Ok((builder.ins().iconst(types::I64, 0), ConstantType::Void)),
[result] => match result_type {
Type::Primitive(ct) => Ok((*result, ct.into())),
Type::Function(_, _) => panic!("return value is a function?"),
ValueOrRef::Ref(_, result_type, name) => {
match self.defined_functions.get(&name) {
None => panic!("Couldn't find function {} to call", name),
Some(function) => {
let func_ref =
self.module.declare_func_in_func(*function, builder.func);
let call = builder.ins().call(func_ref, &arguments);
let results = builder.inst_results(call);
match results {
[] => Ok((
builder.ins().iconst(types::I64, 0),
ConstantType::Void,
)),
[result] => match result_type {
Type::Primitive(ct) => Ok((*result, ct.into())),
Type::Function(_, rt) => match *rt {
Type::Function(_, _) => panic!("function returns a function?"),
Type::Primitive(ct) => Ok((*result, ct.into())),
}
},
_ => panic!("don't support multi-value returns yet"),
}
_ => panic!("don't support multi-value returns yet"),
}
}
}