dust/src/abstract_tree/if_else.rs

113 lines
3.5 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
2024-01-10 20:03:52 +00:00
use crate::{AbstractTree, Block, Expression, Format, Map, Result, SyntaxNode, Type, Value};
2023-10-06 17:32:58 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct IfElse {
if_expression: Expression,
2023-10-31 17:04:22 +00:00
if_block: Block,
2023-10-09 21:01:30 +00:00
else_if_expressions: Vec<Expression>,
2023-10-31 17:04:22 +00:00
else_if_blocks: Vec<Block>,
else_block: Option<Block>,
2023-10-06 17:32:58 +00:00
}
impl AbstractTree for IfElse {
2024-01-10 20:03:52 +00:00
fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
2023-10-31 17:04:22 +00:00
let if_expression_node = node.child(0).unwrap().child(1).unwrap();
2024-01-10 20:03:52 +00:00
let if_expression = Expression::from_syntax(if_expression_node, source, context)?;
2023-10-06 17:32:58 +00:00
2023-10-31 17:04:22 +00:00
let if_block_node = node.child(0).unwrap().child(2).unwrap();
2024-01-10 20:03:52 +00:00
let if_block = Block::from_syntax(if_block_node, source, context)?;
2023-10-06 17:32:58 +00:00
2023-10-09 21:01:30 +00:00
let child_count = node.child_count();
let mut else_if_expressions = Vec::new();
2023-10-31 17:04:22 +00:00
let mut else_if_blocks = Vec::new();
let mut else_block = None;
2023-10-09 21:01:30 +00:00
for index in 1..child_count {
2023-10-31 17:04:22 +00:00
let child = node.child(index).unwrap();
2023-10-09 21:01:30 +00:00
2023-10-31 17:04:22 +00:00
if child.kind() == "else_if" {
let expression_node = child.child(1).unwrap();
2024-01-10 20:03:52 +00:00
let expression = Expression::from_syntax(expression_node, source, context)?;
2023-10-06 17:32:58 +00:00
2023-10-31 17:04:22 +00:00
else_if_expressions.push(expression);
2023-10-09 21:01:30 +00:00
2023-10-31 17:04:22 +00:00
let block_node = child.child(2).unwrap();
2024-01-10 20:03:52 +00:00
let block = Block::from_syntax(block_node, source, context)?;
2023-10-09 21:01:30 +00:00
2023-10-31 17:04:22 +00:00
else_if_blocks.push(block);
}
2023-10-09 21:01:30 +00:00
2023-10-31 17:04:22 +00:00
if child.kind() == "else" {
let else_node = child.child(1).unwrap();
2024-01-10 20:03:52 +00:00
else_block = Some(Block::from_syntax(else_node, source, context)?);
2023-10-09 21:01:30 +00:00
}
}
2023-10-06 17:32:58 +00:00
Ok(IfElse {
if_expression,
2023-10-31 17:04:22 +00:00
if_block,
2023-10-09 21:01:30 +00:00
else_if_expressions,
2023-10-31 17:04:22 +00:00
else_if_blocks,
else_block,
2023-10-06 17:32:58 +00:00
})
}
2023-11-30 00:23:42 +00:00
fn run(&self, source: &str, context: &Map) -> Result<Value> {
2023-10-10 17:29:11 +00:00
let if_boolean = self.if_expression.run(source, context)?.as_boolean()?;
2023-10-06 17:32:58 +00:00
if if_boolean {
2023-10-31 17:04:22 +00:00
self.if_block.run(source, context)
2023-10-06 17:32:58 +00:00
} else {
2023-10-09 21:01:30 +00:00
let expressions = &self.else_if_expressions;
2023-10-30 19:48:43 +00:00
for (index, expression) in expressions.iter().enumerate() {
2023-10-10 17:29:11 +00:00
let if_boolean = expression.run(source, context)?.as_boolean()?;
2023-10-09 21:01:30 +00:00
if if_boolean {
2023-10-31 17:04:22 +00:00
let block = self.else_if_blocks.get(index).unwrap();
2023-10-09 21:01:30 +00:00
2023-10-31 17:04:22 +00:00
return block.run(source, context);
2023-10-09 21:01:30 +00:00
}
}
2023-10-31 17:04:22 +00:00
if let Some(block) = &self.else_block {
block.run(source, context)
2023-10-09 21:01:30 +00:00
} else {
2023-12-31 14:14:43 +00:00
Ok(Value::none())
2023-10-09 21:01:30 +00:00
}
2023-10-06 17:32:58 +00:00
}
}
2023-11-30 00:23:42 +00:00
2023-12-05 22:08:22 +00:00
fn expected_type(&self, context: &Map) -> Result<Type> {
2023-11-30 00:23:42 +00:00
self.if_block.expected_type(context)
}
2023-10-06 17:32:58 +00:00
}
2024-01-06 10:00:36 +00:00
2024-01-06 13:11:09 +00:00
impl Format for IfElse {
fn format(&self, output: &mut String, indent_level: u8) {
output.push_str("if ");
self.if_expression.format(output, indent_level);
output.push(' ');
self.if_block.format(output, indent_level);
let else_ifs = self
.else_if_expressions
.iter()
.zip(self.else_if_blocks.iter());
for (expression, block) in else_ifs {
output.push_str("else if ");
expression.format(output, indent_level);
output.push(' ');
block.format(output, indent_level);
2024-01-06 10:00:36 +00:00
}
2024-01-06 13:11:09 +00:00
if let Some(block) = &self.else_block {
output.push_str("else ");
block.format(output, indent_level);
2024-01-06 10:00:36 +00:00
}
}
}