Add AST -> MIR for typecasting

This commit is contained in:
Sofia 2025-07-21 21:28:39 +03:00
parent c4ab4ac0b3
commit 3378f556ec
7 changed files with 17 additions and 1 deletions

View File

@ -256,7 +256,9 @@ impl ast::Expression {
Box::new(expr.process(module_id)), Box::new(expr.process(module_id)),
), ),
}, },
ast::ExpressionKind::CastTo(expression, _) => todo!(), ast::ExpressionKind::CastTo(expression, ty) => {
mir::ExprKind::CastTo(Box::new(expression.process(module_id)), ty.0.clone().into())
}
}; };
mir::Expression(kind, self.1.as_meta(module_id)) mir::Expression(kind, self.1.as_meta(module_id))

View File

@ -1028,6 +1028,7 @@ impl mir::Expression {
} }
}) })
} }
mir::ExprKind::CastTo(expression, type_kind) => todo!(),
}; };
if let Some(value) = &value { if let Some(value) = &value {
value.instr().maybe_location(&mut scope.block, location); value.instr().maybe_location(&mut scope.block, location);

View File

@ -211,6 +211,7 @@ impl Display for ExprKind {
ExprKind::Borrow(var_ref, false) => write!(f, "&{}", var_ref), ExprKind::Borrow(var_ref, false) => write!(f, "&{}", var_ref),
ExprKind::Borrow(var_ref, true) => write!(f, "&mut {}", var_ref), ExprKind::Borrow(var_ref, true) => write!(f, "&mut {}", var_ref),
ExprKind::Deref(var_ref) => write!(f, "*{}", var_ref), ExprKind::Deref(var_ref) => write!(f, "*{}", var_ref),
ExprKind::CastTo(expression, type_kind) => write!(f, "{} as {}", expression, type_kind),
} }
} }
} }

View File

@ -319,6 +319,13 @@ impl Expression {
_ => Err(ReturnTypeOther::DerefNonBorrow(var.2)), _ => Err(ReturnTypeOther::DerefNonBorrow(var.2)),
} }
} }
CastTo(expr, type_kind) => match expr.return_type(refs) {
Ok(ret_type) => match ret_type {
(ReturnKind::Hard, ty) => Ok((ReturnKind::Hard, ty)),
_ => Ok((ReturnKind::Soft, type_kind.clone())),
},
Err(_) => Ok((ReturnKind::Soft, type_kind.clone())),
},
} }
} }
@ -336,6 +343,7 @@ impl Expression {
ExprKind::BinOp(_, _, _) => None, ExprKind::BinOp(_, _, _) => None,
ExprKind::FunctionCall(_) => None, ExprKind::FunctionCall(_) => None,
ExprKind::If(_) => None, ExprKind::If(_) => None,
ExprKind::CastTo(expression, _) => expression.backing_var(),
} }
} }
@ -363,6 +371,7 @@ impl Expression {
ExprKind::Block(_) => None, ExprKind::Block(_) => None,
ExprKind::Borrow(_, _) => None, ExprKind::Borrow(_, _) => None,
ExprKind::Deref(_) => None, ExprKind::Deref(_) => None,
ExprKind::CastTo(expression, _) => expression.num_value(),
} }
} }
} }

View File

@ -254,6 +254,7 @@ pub enum ExprKind {
Block(Block), Block(Block),
Borrow(NamedVariableRef, bool), Borrow(NamedVariableRef, bool),
Deref(NamedVariableRef), Deref(NamedVariableRef),
CastTo(Box<Expression>, TypeKind),
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -712,6 +712,7 @@ impl Expression {
Ok(*inner) Ok(*inner)
} }
ExprKind::CastTo(expression, type_kind) => todo!(),
} }
} }
} }

View File

@ -397,6 +397,7 @@ impl Expression {
_ => Err(ErrorKind::AttemptedDerefNonBorrow(var.1.clone())), _ => Err(ErrorKind::AttemptedDerefNonBorrow(var.1.clone())),
} }
} }
ExprKind::CastTo(expression, type_kind) => todo!(),
} }
} }
} }