Skip to content

Commit

Permalink
figured it out, it was using the wrong function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
sezna committed Jul 25, 2024
1 parent e910703 commit 2fdbd70
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
4 changes: 1 addition & 3 deletions petr-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ impl Lowerer {

let func_def = self.type_checker.get_monomorphized_function(&func).clone();

let function_definition_body = self.type_checker.get_function(&func.0).body.clone();

let mut buf = vec![];
self.with_variable_context(|ctx| -> Result<_> {
// Pop parameters off the stack in reverse order -- the last parameter for the function
Expand Down Expand Up @@ -141,7 +139,7 @@ impl Lowerer {

let return_reg = ctx.fresh_reg();
let return_dest = ReturnDestination::Reg(return_reg);
let mut expr_body = ctx.lower_expr(&function_definition_body, return_dest).map_err(|e| e)?;
let mut expr_body = ctx.lower_expr(&func_def.body, return_dest).map_err(|e| e)?;
buf.append(&mut expr_body);
// load return value into func return register

Expand Down
45 changes: 44 additions & 1 deletion petr-typecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ impl TypeChecker {
&mut self,
id: &FunctionId,
) -> Function {
println!("looking for function {:?}", id);
if let Some(func) = self.typed_functions.get(id) {
return func.clone();
}
Expand Down Expand Up @@ -1011,6 +1010,14 @@ impl TypeCheck for petr_resolve::FunctionCall {
param.1 = param_ty;
}

// if there are any variable exprs in the body, update those ref types
let mut num_replacements = 0;
replace_var_reference_types(
&mut monomorphized_func_decl.body.kind,
&monomorphized_func_decl.params,
&mut num_replacements,
);

ctx.monomorphized_functions.insert(signature, monomorphized_func_decl);

TypedExprKind::FunctionCall {
Expand All @@ -1021,6 +1028,42 @@ impl TypeCheck for petr_resolve::FunctionCall {
}
}

fn replace_var_reference_types(
expr: &mut TypedExprKind,
params: &Vec<(Identifier, TypeVariable)>,
num_replacements: &mut usize,
) {
match expr {
TypedExprKind::Variable { ref mut ty, name } => {
if let Some((_param_name, ty_var)) = params.iter().find(|(param_name, _)| param_name.id == name.id) {
*num_replacements += 1;
*ty = *ty_var;
}
},
TypedExprKind::FunctionCall { args, .. } => {
for (_, arg) in args {
replace_var_reference_types(&mut arg.kind, params, num_replacements);
}
},
TypedExprKind::Intrinsic { intrinsic, .. } => {
use Intrinsic::*;
match intrinsic {
// intrinsics which take one arg, grouped for convenience
Puts(a) | Malloc(a) | SizeOf(a) => {
replace_var_reference_types(&mut a.kind, params, num_replacements);
},
// intrinsics which take two args, grouped for convenience
Add(a, b) | Subtract(a, b) | Multiply(a, b) | Divide(a, b) => {
replace_var_reference_types(&mut a.kind, params, num_replacements);
replace_var_reference_types(&mut b.kind, params, num_replacements);
},
}
},
// TODO other expr kinds like bindings
_ => (),
}
}

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
Expand Down

0 comments on commit 2fdbd70

Please sign in to comment.