Skip to content

Commit

Permalink
fixed excess free
Browse files Browse the repository at this point in the history
  • Loading branch information
CreggEgg committed Jul 23, 2024
1 parent 8a63191 commit 6891da4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 45 deletions.
66 changes: 34 additions & 32 deletions parsing.ct
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
type struct = {
hello: int,
world: bool
alias string = [int];
extern printstr: fn(string): unit;
extern printint: fn(int): unit;

let sum_to = fn(a: int): int {
let ugly_hack = a + 1;
let num = a * ugly_hack;
num/2
};
type enum = [
hello,
world
];
type alias = enum;
let const = 5;
let use_alias = fn(a: alias): alias {
@a
};
let ts = fn(b: int): bool {
b > 5
};
let add = fn(a: int, b: int): int {
a
};
let println = fn(string: str) {
string
};
let greeting = fn(): str {
"Hello world"

let three_five_sum = fn(a: int): int {
let n = a - 1;

let num_threes = n/3;
let num_fives = n/5;
let num_fifteen = n/15;

let a = sum_to(num_threes);
let b = sum_to(num_fives);
let c = sum_to(num_fifteen);

let a_ = a * 3;
let b_ = b * 5;
let c_ = c * 15;

printint(a_);
printint(b_);

printstr("29");

a_ + b_ - c_
};

let main = fn() {
let local message = greeting();
println("hi");
if const < 3 {
println("math broke")
} else {
println("Its fine")
};
println(message);
message = "hi there";
printstr("result: ");
let val = three_five_sum(1000);
printint(val)
};
43 changes: 32 additions & 11 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,11 @@ fn compile_block(
for (name, value) in scope {
if let CitrusValue::Value {
cranelift_value,
r#type,
ref r#type,
heap: true,
} = value
{
println!("freeing: {name}, {:?}", value);
function_builder.ins().call(free, &[cranelift_value]);
}
}
Expand Down Expand Up @@ -553,8 +554,11 @@ fn move_to_heap(
} else {
Ok(CitrusValue::Value {
cranelift_value,
r#type,
heap: true,
r#type: r#type.clone(),
heap: match r#type {
Type::Int | Type::Bool => false,
_ => true,
},
})
}
} else {
Expand Down Expand Up @@ -896,14 +900,31 @@ fn compile_expr(
let ret = builder.ins().call(func, &args);
let ret = builder.inst_results(ret).get(0);
Ok(match ret {
Some(ret) => (
CitrusValue::Value {
cranelift_value: *ret,
r#type: r#type.clone(),
heap: true,
},
scope,
),
Some(ret) => {
if let Type::Function(args, r#type) = r#type {
(
CitrusValue::Value {
cranelift_value: *ret,
r#type: *r#type.clone(),
heap: match **r#type {
Type::Int | Type::Bool => false,
_ => true,
},
},
scope,
)
} else {
let unit = builder.ins().iconst(I64, 0);
(
CitrusValue::Value {
cranelift_value: unit,
r#type: Type::Unit,
heap: false,
},
scope,
)
}
}
None => {
let unit = builder.ins().iconst(I64, 0);
(
Expand Down
6 changes: 4 additions & 2 deletions src/types/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub fn type_file(ast: File) -> Result<TypedFile, TypeError> {
}
}
}
#[cfg(debug_assertions)]
dbg!(scope);
Ok(TypedFile { declarations })
}

Expand Down Expand Up @@ -473,8 +475,6 @@ fn type_literal(
),
);
};
#[cfg(debug_assertions)]
dbg!(&body_scope);
for arg in &args {
body_scope.insert(arg.name.clone(), arg.r#type.clone());
}
Expand All @@ -485,6 +485,8 @@ fn type_literal(
body_scope = scope;
typed_body.push(expr);
}
#[cfg(debug_assertions)]
dbg!(&body_scope);
TypedLiteral::Function {
args,
body: typed_body,
Expand Down

0 comments on commit 6891da4

Please sign in to comment.