From 3378f556ec251721f3d2471461d7fe4d9e28d7d5 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 21 Jul 2025 21:28:39 +0300 Subject: [PATCH] Add AST -> MIR for typecasting --- reid/src/ast/process.rs | 4 +++- reid/src/codegen.rs | 1 + reid/src/mir/fmt.rs | 1 + reid/src/mir/implement.rs | 9 +++++++++ reid/src/mir/mod.rs | 1 + reid/src/mir/typecheck.rs | 1 + reid/src/mir/typeinference.rs | 1 + 7 files changed, 17 insertions(+), 1 deletion(-) diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 58498b0..e8f34d9 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -256,7 +256,9 @@ impl ast::Expression { 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)) diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 142557f..9df8827 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -1028,6 +1028,7 @@ impl mir::Expression { } }) } + mir::ExprKind::CastTo(expression, type_kind) => todo!(), }; if let Some(value) = &value { value.instr().maybe_location(&mut scope.block, location); diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index 6e00663..b79ecb1 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -211,6 +211,7 @@ impl Display for ExprKind { ExprKind::Borrow(var_ref, false) => write!(f, "&{}", var_ref), ExprKind::Borrow(var_ref, true) => write!(f, "&mut {}", var_ref), ExprKind::Deref(var_ref) => write!(f, "*{}", var_ref), + ExprKind::CastTo(expression, type_kind) => write!(f, "{} as {}", expression, type_kind), } } } diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index aab7ba1..989acc8 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -319,6 +319,13 @@ impl Expression { _ => 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::FunctionCall(_) => None, ExprKind::If(_) => None, + ExprKind::CastTo(expression, _) => expression.backing_var(), } } @@ -363,6 +371,7 @@ impl Expression { ExprKind::Block(_) => None, ExprKind::Borrow(_, _) => None, ExprKind::Deref(_) => None, + ExprKind::CastTo(expression, _) => expression.num_value(), } } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 18da67a..b734c7e 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -254,6 +254,7 @@ pub enum ExprKind { Block(Block), Borrow(NamedVariableRef, bool), Deref(NamedVariableRef), + CastTo(Box, TypeKind), } #[derive(Debug)] diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 630e904..28f4e4a 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -712,6 +712,7 @@ impl Expression { Ok(*inner) } + ExprKind::CastTo(expression, type_kind) => todo!(), } } } diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index efb79d3..580167d 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -397,6 +397,7 @@ impl Expression { _ => Err(ErrorKind::AttemptedDerefNonBorrow(var.1.clone())), } } + ExprKind::CastTo(expression, type_kind) => todo!(), } } }