Add return type for index and array expressions

This commit is contained in:
Sofia 2025-07-13 18:41:14 +03:00
parent ad20fefabc
commit 10cf9341c5

View File

@ -7,6 +7,7 @@ pub enum ReturnTypeOther {
Set(Metadata), Set(Metadata),
EmptyBlock(Metadata), EmptyBlock(Metadata),
NoBlockReturn(Metadata), NoBlockReturn(Metadata),
IndexingNonArray(Metadata),
} }
impl TypeKind { impl TypeKind {
@ -86,8 +87,25 @@ impl ReturnType for Expression {
Block(block) => block.return_type(), Block(block) => block.return_type(),
FunctionCall(fcall) => fcall.return_type(), FunctionCall(fcall) => fcall.return_type(),
If(expr) => expr.return_type(), If(expr) => expr.return_type(),
Index(expression, _) => todo!("return type for index"), Index(expression, _) => {
Array(expressions) => todo!("return type for array expression"), let expr_type = expression.return_type()?;
if let (_, TypeKind::Array(elem_ty, _)) = expr_type {
Ok((ReturnKind::Soft, *elem_ty))
} else {
Err(ReturnTypeOther::IndexingNonArray(expression.1))
}
}
Array(expressions) => {
let first = expressions
.iter()
.next()
.map(|e| e.return_type())
.unwrap_or(Ok((ReturnKind::Soft, TypeKind::Void)))?;
Ok((
ReturnKind::Soft,
TypeKind::Array(Box::new(first.1), expressions.len() as u64),
))
}
} }
} }
} }