Add function calls to IR Generator
This commit is contained in:
parent
2cf26ec39d
commit
b3a8188dfe
3 changed files with 30 additions and 3 deletions
|
@ -220,7 +220,31 @@ fn visit_ast_node<'source>(
|
||||||
|
|
||||||
symbols.get("unit").clone()
|
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) => {
|
Block(expressions) => {
|
||||||
let mut result_var = symbols.get("unit").clone();
|
let mut result_var = symbols.get("unit").clone();
|
||||||
for expression in expressions {
|
for expression in expressions {
|
||||||
|
|
|
@ -49,6 +49,9 @@ impl<'source> SymTab<'source, Type> {
|
||||||
pub fn new_type_table() -> SymTab<'source, Type> {
|
pub fn new_type_table() -> SymTab<'source, Type> {
|
||||||
use Type::*;
|
use Type::*;
|
||||||
let globals = HashMap::from([
|
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))),
|
("*", Func(vec![Int, Int], Box::new(Int))),
|
||||||
("-", Func(vec![Int, Int], Box::new(Int))),
|
("-", Func(vec![Int, Int], Box::new(Int))),
|
||||||
|
|
|
@ -122,8 +122,8 @@ fn get_type<'source>(ast: &mut AstNode<'source>, symbols: &mut SymTab<'source, T
|
||||||
}
|
}
|
||||||
FunCall(name, args) => {
|
FunCall(name, args) => {
|
||||||
let mut arg_types = Vec::new();
|
let mut arg_types = Vec::new();
|
||||||
for mut arg in args {
|
for arg in args {
|
||||||
arg_types.push(type_check(&mut arg, symbols));
|
arg_types.push(type_check(arg, symbols));
|
||||||
}
|
}
|
||||||
|
|
||||||
let Type::Func(sig_arg_types, sig_ret_type) = symbols.get(name) else {
|
let Type::Func(sig_arg_types, sig_ret_type) = symbols.get(name) else {
|
||||||
|
|
Reference in a new issue