|
|
|
@ -10,9 +10,7 @@ pub(super) fn infer(program: &mut Program) -> Result<(), String> {
|
|
|
|
|
Statement::Declare(var, expr) => { |
|
|
|
|
if let None = &var.ty { |
|
|
|
|
if let Some(e) = expr { |
|
|
|
|
dbg!(&var); |
|
|
|
|
var.ty = infer_expression(&e); |
|
|
|
|
dbg!(&var); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -29,6 +27,18 @@ fn infer_expression(expr: &Expression) -> Option<Type> {
|
|
|
|
|
Expression::Int(_) => Some(Type::Int), |
|
|
|
|
Expression::Bool(_) => Some(Type::Bool), |
|
|
|
|
Expression::Str(_) => Some(Type::Str), |
|
|
|
|
Expression::Array(els) => infer_array(els), |
|
|
|
|
_ => None, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn infer_array(elements: &Vec<Expression>) -> Option<Type> { |
|
|
|
|
let types: Vec<Option<Type>> = elements.iter().map(|el| infer_expression(el)).collect(); |
|
|
|
|
|
|
|
|
|
// TODO: This approach only relies on the first element.
|
|
|
|
|
// It will not catch that types are possibly inconsistent.
|
|
|
|
|
match types.first().and_then(|ty| ty.to_owned()) { |
|
|
|
|
Some(ty) => Some(Type::Array(Box::new(ty))), |
|
|
|
|
None => None, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|