Start working on if-expression hands being expressions

This commit is contained in:
Sofia 2025-07-22 20:58:32 +03:00
parent 5c5c9c5f7b
commit 29d790e583
4 changed files with 17 additions and 9 deletions

View File

@ -115,8 +115,8 @@ pub struct FunctionCallExpression(pub String, pub Vec<Expression>, pub TokenRang
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct IfExpression( pub struct IfExpression(
pub Expression, pub Expression,
pub Block, pub Expression,
pub Option<Block>, pub Option<Expression>,
#[allow(dead_code)] pub TokenRange, #[allow(dead_code)] pub TokenRange,
); );

View File

@ -3,8 +3,8 @@ use std::path::PathBuf;
use crate::{ use crate::{
ast::{self}, ast::{self},
mir::{ mir::{
self, ModuleMap, NamedVariableRef, SourceModuleId, StmtKind, StructField, StructType, self, CustomTypeKey, ModuleMap, NamedVariableRef, SourceModuleId, StmtKind, StructField,
CustomTypeKey, StructType,
}, },
}; };
@ -205,13 +205,17 @@ impl ast::Expression {
} }
ast::ExpressionKind::IfExpr(if_expression) => { ast::ExpressionKind::IfExpr(if_expression) => {
let cond = if_expression.0.process(module_id); let cond = if_expression.0.process(module_id);
let then_block = if_expression.1.into_mir(module_id); let then_block = if_expression.1.process(module_id);
let else_block = if let Some(el) = &if_expression.2 { let else_block = if let Some(el) = &if_expression.2 {
Some(el.into_mir(module_id)) Some(el.process(module_id))
} else { } else {
None None
}; };
mir::ExprKind::If(mir::IfExpression(Box::new(cond), then_block, else_block)) mir::ExprKind::If(mir::IfExpression(
Box::new(cond),
Box::new(then_block),
Box::new(else_block),
))
} }
ast::ExpressionKind::Array(expressions) => { ast::ExpressionKind::Array(expressions) => {
mir::ExprKind::Array(expressions.iter().map(|e| e.process(module_id)).collect()) mir::ExprKind::Array(expressions.iter().map(|e| e.process(module_id)).collect())

View File

@ -240,7 +240,7 @@ impl Display for IfExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "if {} ", self.0)?; write!(f, "if {} ", self.0)?;
Display::fmt(&self.1, f)?; Display::fmt(&self.1, f)?;
if let Some(e) = &self.2 { if let Some(e) = *self.2 {
Display::fmt(&e, f)?; Display::fmt(&e, f)?;
} }
Ok(()) Ok(())

View File

@ -267,7 +267,11 @@ pub struct Expression(pub ExprKind, pub Metadata);
/// Condition, Then, Else /// Condition, Then, Else
#[derive(Debug)] #[derive(Debug)]
pub struct IfExpression(pub Box<Expression>, pub Block, pub Option<Block>); pub struct IfExpression(
pub Box<Expression>,
pub Box<Expression>,
pub Box<Option<Expression>>,
);
#[derive(Debug)] #[derive(Debug)]
pub struct FunctionCall { pub struct FunctionCall {