From b3a8188dfe851f734313a8d07b0488a4f520fb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vili=20Sinerv=C3=A4?= Date: Wed, 5 Feb 2025 23:28:00 +0200 Subject: [PATCH] Add function calls to IR Generator --- src/compiler/ir_generator.rs | 26 +++++++++++++++++++++++++- src/compiler/symtab.rs | 3 +++ src/compiler/type_checker.rs | 4 ++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/compiler/ir_generator.rs b/src/compiler/ir_generator.rs index 7e75cdd..e017e60 100644 --- a/src/compiler/ir_generator.rs +++ b/src/compiler/ir_generator.rs @@ -220,7 +220,31 @@ fn visit_ast_node<'source>( symbols.get("unit").clone() } - FunCall(_, _) => todo!(), + FunCall(name, expressions) => { + let fn_var = symbols.get(name).clone(); + let Type::Func(_, result_type) = types.get(&fn_var).unwrap().clone() else { + panic!("Function call does not have entry in types table!"); + }; + + let mut args = Vec::new(); + for expression in expressions { + args.push(visit_ast_node( + expression, + types, + symbols, + instructions, + labels, + )); + } + let result_var = add_var(&result_type, types); + + instructions.push(IrInstruction::new( + ast.loc, + Call(fn_var, args, result_var.clone()), + )); + + result_var + } Block(expressions) => { let mut result_var = symbols.get("unit").clone(); for expression in expressions { diff --git a/src/compiler/symtab.rs b/src/compiler/symtab.rs index b83bfd1..68fd192 100644 --- a/src/compiler/symtab.rs +++ b/src/compiler/symtab.rs @@ -49,6 +49,9 @@ impl<'source> SymTab<'source, Type> { pub fn new_type_table() -> SymTab<'source, Type> { use Type::*; let globals = HashMap::from([ + ("print_bool", Func(vec![Bool], Box::new(Unit))), + ("print_int", Func(vec![Int], Box::new(Unit))), + ("read_int", Func(vec![], Box::new(Int))), ("+", Func(vec![Int, Int], Box::new(Int))), ("*", Func(vec![Int, Int], Box::new(Int))), ("-", Func(vec![Int, Int], Box::new(Int))), diff --git a/src/compiler/type_checker.rs b/src/compiler/type_checker.rs index cdef99c..62c8906 100644 --- a/src/compiler/type_checker.rs +++ b/src/compiler/type_checker.rs @@ -122,8 +122,8 @@ fn get_type<'source>(ast: &mut AstNode<'source>, symbols: &mut SymTab<'source, T } FunCall(name, args) => { let mut arg_types = Vec::new(); - for mut arg in args { - arg_types.push(type_check(&mut arg, symbols)); + for arg in args { + arg_types.push(type_check(arg, symbols)); } let Type::Func(sig_arg_types, sig_ret_type) = symbols.get(name) else {