2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tree_sitter::Node;
|
|
|
|
|
|
|
|
use crate::{AbstractTree, Expression, Result, Statement, Value, VariableMap};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct IfElse {
|
|
|
|
if_expression: Expression,
|
|
|
|
then_statement: Statement,
|
2023-10-09 21:01:30 +00:00
|
|
|
else_if_expressions: Vec<Expression>,
|
|
|
|
else_if_statements: Vec<Statement>,
|
2023-10-06 17:32:58 +00:00
|
|
|
else_statement: Option<Statement>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for IfElse {
|
2023-10-10 17:29:11 +00:00
|
|
|
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
2023-10-09 21:01:30 +00:00
|
|
|
let if_node = node.child(0).unwrap().child(1).unwrap();
|
2023-10-10 17:29:11 +00:00
|
|
|
let if_expression = Expression::from_syntax_node(source, if_node)?;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-10-09 21:01:30 +00:00
|
|
|
let then_node = node.child(0).unwrap().child(3).unwrap();
|
2023-10-10 17:29:11 +00:00
|
|
|
let then_statement = Statement::from_syntax_node(source, then_node)?;
|
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();
|
|
|
|
let mut else_if_statements = Vec::new();
|
|
|
|
let mut else_statement = None;
|
|
|
|
|
|
|
|
for index in 1..child_count {
|
|
|
|
let child = node.child(index);
|
|
|
|
|
|
|
|
if let Some(node) = child {
|
|
|
|
if node.kind() == "else_if" {
|
|
|
|
let expression_node = node.child(1).unwrap();
|
2023-10-10 17:29:11 +00:00
|
|
|
let expression = Expression::from_syntax_node(source, expression_node)?;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-10-09 21:01:30 +00:00
|
|
|
else_if_expressions.push(expression);
|
|
|
|
|
|
|
|
let statement_node = node.child(3).unwrap();
|
2023-10-10 17:29:11 +00:00
|
|
|
let statement = Statement::from_syntax_node(source, statement_node)?;
|
2023-10-09 21:01:30 +00:00
|
|
|
|
|
|
|
else_if_statements.push(statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.kind() == "else" {
|
|
|
|
let else_node = node.child(2).unwrap();
|
2023-10-10 17:29:11 +00:00
|
|
|
else_statement = Some(Statement::from_syntax_node(source, else_node)?);
|
2023-10-09 21:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
|
|
|
|
Ok(IfElse {
|
|
|
|
if_expression,
|
|
|
|
then_statement,
|
2023-10-09 21:01:30 +00:00
|
|
|
else_if_expressions,
|
|
|
|
else_if_statements,
|
2023-10-06 17:32:58 +00:00
|
|
|
else_statement,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-10 17:29:11 +00:00
|
|
|
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> {
|
|
|
|
let if_boolean = self.if_expression.run(source, context)?.as_boolean()?;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
|
|
|
if if_boolean {
|
2023-10-10 17:29:11 +00:00
|
|
|
self.then_statement.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;
|
|
|
|
|
|
|
|
for (index, expression) in expressions.into_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 {
|
|
|
|
let statement = self.else_if_statements.get(index).unwrap();
|
|
|
|
|
2023-10-10 17:29:11 +00:00
|
|
|
return statement.run(source, context);
|
2023-10-09 21:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(statement) = &self.else_statement {
|
2023-10-10 17:29:11 +00:00
|
|
|
statement.run(source, context)
|
2023-10-09 21:01:30 +00:00
|
|
|
} else {
|
|
|
|
Ok(Value::Empty)
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|