1
0
Fork 0

Fix issue with unit types

This commit is contained in:
Vili Sinervä 2025-02-26 19:14:42 +02:00
parent a8cef15331
commit 3b4e3d92fb
No known key found for this signature in database
GPG key ID: DF8FEAF54EFAC996
3 changed files with 25 additions and 17 deletions

View file

@ -236,9 +236,9 @@ impl Locals {
}
pub fn get_ref(&self, var: &IrVar) -> &str {
self.var_to_location
.get(var)
.expect("Tried to use non-existant var in assembly generation!")
self.var_to_location.get(var).expect(&format!(
"Tried to use non-existant var '{var}' in assembly generation!"
))
}
pub fn stack_used(&self) -> i64 {

View file

@ -29,7 +29,6 @@ impl IrVar {
pub fn new_global_types() -> HashMap<IrVar, Type> {
use Type::*;
HashMap::from([
(IrVar::new("unit"), Unit),
(IrVar::new("print_bool"), Func(vec![Bool], Box::new(Unit))),
(IrVar::new("print_int"), Func(vec![Int], Box::new(Unit))),
(IrVar::new("read_int"), Func(vec![], Box::new(Int))),

View file

@ -59,16 +59,25 @@ pub fn generate_ir(ast: &AstNode) -> Vec<IrInstruction> {
}
fn add_var(var_type: &Type, types: &mut HashMap<IrVar, Type>) -> IrVar {
let mut i = 1;
let mut var = IrVar::new(&format!("x{}", i));
match var_type {
Type::Unit => {
let var = IrVar::new("unit");
types.insert(var.clone(), var_type.clone());
var
}
_ => {
let mut i = 1;
let mut var = IrVar::new(&format!("x{}", i));
while types.contains_key(&var) {
i += 1;
var = IrVar::new(&format!("x{}", i));
while types.contains_key(&var) {
i += 1;
var = IrVar::new(&format!("x{}", i));
}
types.insert(var.clone(), var_type.clone());
var
}
}
types.insert(var.clone(), var_type.clone());
var
}
fn add_label(
@ -97,7 +106,7 @@ fn visit_ast_node<'source>(
labels: &mut HashSet<IrInstructionType>,
) -> IrVar {
match &ast.expr {
EmptyLiteral() => symbols.get("unit").clone(),
EmptyLiteral() => add_var(&Type::Unit, types),
IntLiteral(val) => {
let var = add_var(&Type::Int, types);
instructions.push(IrInstruction::new(ast.loc, LoadIntConst(*val, var.clone())));
@ -223,7 +232,7 @@ fn visit_ast_node<'source>(
let result_var = add_var(&expr.node_type, types);
symbols.insert(name, result_var.clone());
instructions.push(IrInstruction::new(expr.loc, Copy(expr_var, result_var)));
symbols.get("unit").clone()
add_var(&Type::Unit, types)
}
Conditional(condition_expr, then_expr, else_expr) => match else_expr {
Some(else_expr) => {
@ -275,7 +284,7 @@ fn visit_ast_node<'source>(
visit_ast_node(then_expr, types, symbols, instructions, labels);
instructions.push(l_end);
symbols.get("unit").clone()
add_var(&Type::Unit, types)
}
},
While(condition_expr, do_expr) => {
@ -294,7 +303,7 @@ fn visit_ast_node<'source>(
instructions.push(IrInstruction::new(do_expr.loc, Jump(Box::new(l_start))));
instructions.push(l_end);
symbols.get("unit").clone()
add_var(&Type::Unit, types)
}
FunCall(name, expressions) => {
let fn_var = symbols.get(name).clone();
@ -322,7 +331,7 @@ fn visit_ast_node<'source>(
result_var
}
Block(expressions) => {
let mut result_var = symbols.get("unit").clone();
let mut result_var = add_var(&Type::Unit, types);
for expression in expressions {
result_var = visit_ast_node(expression, types, symbols, instructions, labels);
}