mirror of https://git.sr.ht/~garritfra/sabre
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.0 KiB
34 lines
1.0 KiB
use super::node_type::*; |
|
|
|
/// Try to infer types of variables |
|
pub(super) fn infer(program: &mut Program) -> Result<(), String> { |
|
// TODO: Fix aweful nesting |
|
for func in &mut program.func { |
|
if let Statement::Block(statements) = &mut func.body { |
|
for statement in statements { |
|
match statement { |
|
Statement::Declare(var, expr) => { |
|
if let None = &var.ty { |
|
if let Some(e) = expr { |
|
dbg!(&var); |
|
var.ty = infer_expression(&e); |
|
dbg!(&var); |
|
} |
|
} |
|
} |
|
_ => {} |
|
} |
|
} |
|
} |
|
} |
|
Ok(()) |
|
} |
|
|
|
fn infer_expression(expr: &Expression) -> Option<Type> { |
|
match expr { |
|
Expression::Int(_) => Some(Type::Int), |
|
Expression::Bool(_) => Some(Type::Bool), |
|
Expression::Str(_) => Some(Type::Str), |
|
_ => None, |
|
} |
|
}
|
|
|