1
0
dust/dust-lang/src/abstract_tree/if_else.rs

176 lines
5.9 KiB
Rust
Raw Normal View History

2024-03-08 19:01:05 +00:00
use crate::{
context::Context,
error::{RuntimeError, ValidationError},
2024-03-17 20:59:52 +00:00
value::ValueInner,
2024-03-08 19:01:05 +00:00
};
use super::{AbstractNode, Action, Block, Expression, Type, WithPosition};
2024-03-08 19:01:05 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
2024-03-08 21:14:47 +00:00
pub struct IfElse {
2024-03-25 04:16:55 +00:00
if_expression: Expression,
if_block: WithPosition<Block>,
else_ifs: Vec<(Expression, WithPosition<Block>)>,
else_block: Option<WithPosition<Block>>,
2024-03-08 19:01:05 +00:00
}
2024-03-08 21:14:47 +00:00
impl IfElse {
2024-03-17 04:49:01 +00:00
pub fn new(
2024-03-25 04:16:55 +00:00
if_expression: Expression,
if_block: WithPosition<Block>,
else_ifs: Vec<(Expression, WithPosition<Block>)>,
else_block: Option<WithPosition<Block>>,
2024-03-17 04:49:01 +00:00
) -> Self {
2024-03-08 19:01:05 +00:00
Self {
if_expression,
2024-03-09 20:17:19 +00:00
if_block,
2024-03-25 04:16:55 +00:00
else_ifs,
2024-03-09 20:17:19 +00:00
else_block,
2024-03-08 19:01:05 +00:00
}
}
}
impl AbstractNode for IfElse {
2024-03-08 19:01:05 +00:00
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
2024-04-22 07:41:21 +00:00
self.if_block.item.expected_type(_context)
2024-03-08 19:01:05 +00:00
}
fn validate(&self, context: &Context) -> Result<(), ValidationError> {
2024-03-25 04:16:55 +00:00
self.if_expression.validate(context)?;
2024-04-22 07:41:21 +00:00
self.if_block.item.validate(context)?;
2024-03-17 17:36:31 +00:00
2024-04-22 07:41:21 +00:00
let expected_type = self.if_block.item.expected_type(context)?;
2024-03-25 04:16:55 +00:00
let if_expression_type = self.if_expression.expected_type(context)?;
2024-03-17 20:59:52 +00:00
if let Type::Boolean = if_expression_type {
if let Some(else_block) = &self.else_block {
2024-04-22 07:41:21 +00:00
else_block.item.validate(context)?;
2024-03-17 17:36:31 +00:00
2024-04-22 07:41:21 +00:00
let actual = else_block.item.expected_type(context)?;
2024-03-25 05:56:06 +00:00
expected_type
2024-03-17 04:49:01 +00:00
.check(&actual)
.map_err(|conflict| ValidationError::TypeCheck {
conflict,
2024-04-22 07:41:21 +00:00
actual_position: self.if_block.item.last_statement().position(),
2024-03-25 04:16:55 +00:00
expected_position: self.if_expression.position(),
2024-03-17 04:49:01 +00:00
})?;
}
2024-03-08 19:29:53 +00:00
} else {
2024-03-25 05:56:06 +00:00
return Err(ValidationError::ExpectedBoolean {
2024-03-17 20:59:52 +00:00
actual: if_expression_type,
2024-03-25 04:16:55 +00:00
position: self.if_expression.position(),
2024-03-25 05:56:06 +00:00
});
2024-03-08 19:29:53 +00:00
}
2024-03-25 05:56:06 +00:00
for (expression, block) in &self.else_ifs {
let expression_type = expression.expected_type(context)?;
if let Type::Boolean = expression_type {
2024-04-22 07:41:21 +00:00
block.item.validate(context)?;
2024-03-25 05:56:06 +00:00
2024-04-22 07:41:21 +00:00
let actual = block.item.expected_type(context)?;
2024-03-25 05:56:06 +00:00
expected_type
.check(&actual)
.map_err(|conflict| ValidationError::TypeCheck {
conflict,
2024-04-22 07:41:21 +00:00
actual_position: self.if_block.item.last_statement().position(),
2024-03-25 05:56:06 +00:00
expected_position: self.if_expression.position(),
})?;
} else {
return Err(ValidationError::ExpectedBoolean {
actual: if_expression_type,
position: self.if_expression.position(),
});
}
}
Ok(())
2024-03-08 19:01:05 +00:00
}
fn run(self, context: &mut Context, _clear_variables: bool) -> Result<Action, RuntimeError> {
2024-03-25 04:16:55 +00:00
let if_position = self.if_expression.position();
let action = self.if_expression.run(context, _clear_variables)?;
2024-03-18 09:39:09 +00:00
let value = if let Action::Return(value) = action {
value
} else {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::InterpreterExpectedReturn(if_position),
2024-03-18 09:39:09 +00:00
));
};
2024-03-08 19:01:05 +00:00
2024-03-17 20:59:52 +00:00
if let ValueInner::Boolean(if_boolean) = value.inner().as_ref() {
if *if_boolean {
2024-04-22 07:41:21 +00:00
self.if_block.item.run(context, _clear_variables)
2024-03-17 20:59:52 +00:00
} else {
2024-03-25 05:56:06 +00:00
for (expression, block) in self.else_ifs {
let expression_position = expression.position();
let action = expression.run(context, _clear_variables)?;
2024-03-25 05:56:06 +00:00
let value = if let Action::Return(value) = action {
value
} else {
return Err(RuntimeError::ValidationFailure(
ValidationError::InterpreterExpectedReturn(expression_position),
));
};
if let ValueInner::Boolean(else_if_boolean) = value.inner().as_ref() {
if *else_if_boolean {
2024-04-22 07:41:21 +00:00
return block.item.run(context, _clear_variables);
2024-03-25 05:56:06 +00:00
}
} else {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedBoolean {
actual: value.r#type(context)?,
position: expression_position,
},
));
}
}
if let Some(else_statement) = self.else_block {
2024-04-22 07:41:21 +00:00
else_statement.item.run(context, _clear_variables)
2024-03-25 05:56:06 +00:00
} else {
Ok(Action::None)
}
2024-03-17 20:59:52 +00:00
}
2024-03-08 19:01:05 +00:00
} else {
2024-03-17 20:59:52 +00:00
Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedBoolean {
2024-03-19 22:31:52 +00:00
actual: value.r#type(context)?,
2024-03-25 04:16:55 +00:00
position: if_position,
2024-03-17 20:59:52 +00:00
},
))
2024-03-08 19:01:05 +00:00
}
}
}
#[cfg(test)]
mod tests {
use crate::{
2024-03-24 16:21:08 +00:00
abstract_tree::{Statement, ValueNode, WithPos},
2024-03-08 19:01:05 +00:00
Value,
};
use super::*;
#[test]
fn simple_if() {
assert_eq!(
IfElse::new(
2024-03-25 04:16:55 +00:00
Expression::Value(ValueNode::Boolean(true).with_position((0, 0))),
2024-03-17 11:31:45 +00:00
Block::new(vec![Statement::Expression(Expression::Value(
2024-03-25 04:16:55 +00:00
ValueNode::String("foo".to_string()).with_position((0, 0))
))])
.with_position((0, 0)),
Vec::with_capacity(0),
2024-03-08 19:01:05 +00:00
None
)
.run(&mut Context::new(), true)
2024-03-17 21:39:39 +00:00
.unwrap(),
Action::Return(Value::string("foo".to_string()))
2024-03-08 19:01:05 +00:00
)
}
}