1
0
dust/src/abstract_tree/statement.rs

110 lines
4.9 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{
2023-11-30 14:30:25 +00:00
AbstractTree, Assignment, Block, Error, Expression, For, FunctionDeclaration, IfElse,
IndexAssignment, Map, Match, Result, TypeDefinition, Use, Value, While,
2023-10-06 17:32:58 +00:00
};
/// Abstract representation of a statement.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum Statement {
Assignment(Box<Assignment>),
Return(Expression),
2023-10-06 17:32:58 +00:00
Expression(Expression),
2023-11-30 14:30:25 +00:00
FunctionDeclaration(FunctionDeclaration),
2023-10-06 17:32:58 +00:00
IfElse(Box<IfElse>),
Match(Match),
2023-10-07 02:45:36 +00:00
While(Box<While>),
2023-11-11 01:44:03 +00:00
Block(Box<Block>),
2023-10-17 18:06:02 +00:00
For(Box<For>),
Use(Use),
2023-11-15 01:41:57 +00:00
IndexAssignment(Box<IndexAssignment>),
2023-10-06 17:32:58 +00:00
}
impl AbstractTree for Statement {
2023-11-30 03:54:46 +00:00
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
2023-11-15 02:03:52 +00:00
Error::expect_syntax_node(source, "statement", node)?;
2023-10-06 17:32:58 +00:00
let child = node.child(0).unwrap();
match child.kind() {
"assignment" => Ok(Statement::Assignment(Box::new(
2023-11-30 03:54:46 +00:00
Assignment::from_syntax_node(source, child, context)?,
2023-10-06 17:32:58 +00:00
))),
"return" => {
let expression_node = child.child(1).unwrap();
2023-11-30 03:54:46 +00:00
Ok(Statement::Return(Expression::from_syntax_node(source, expression_node, context)?))
},
2023-11-30 14:30:25 +00:00
"expression" => Ok(Statement::Expression(Expression::from_syntax_node(
source, child, context
)?)),
"function_declaration" => Ok(Statement::FunctionDeclaration(FunctionDeclaration::from_syntax_node(
2023-11-30 03:54:46 +00:00
source, child, context
2023-10-06 17:32:58 +00:00
)?)),
"if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax_node(
2023-11-30 03:54:46 +00:00
source, child, context
2023-10-06 17:32:58 +00:00
)?))),
"tool" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax_node(
2023-11-30 03:54:46 +00:00
source, child, context
2023-10-06 17:32:58 +00:00
)?))),
2023-10-07 02:45:36 +00:00
"while" => Ok(Statement::While(Box::new(While::from_syntax_node(
2023-11-30 03:54:46 +00:00
source, child, context
2023-10-07 02:45:36 +00:00
)?))),
2023-11-11 01:44:03 +00:00
"block" => Ok(Statement::Block(Box::new(Block::from_syntax_node(
2023-11-30 03:54:46 +00:00
source, child, context
2023-10-17 18:06:02 +00:00
)?))),
"for" => Ok(Statement::For(Box::new(For::from_syntax_node(
2023-11-30 03:54:46 +00:00
source, child, context
2023-10-16 20:48:02 +00:00
)?))),
2023-11-30 03:54:46 +00:00
"use" => Ok(Statement::Use(Use::from_syntax_node(source, child, context)?)),
2023-11-15 01:41:57 +00:00
"index_assignment" => Ok(Statement::IndexAssignment(Box::new(IndexAssignment::from_syntax_node(
2023-11-30 03:54:46 +00:00
source, child, context
2023-11-15 01:41:57 +00:00
)?))),
2023-11-15 02:03:52 +00:00
_ => Err(Error::UnexpectedSyntaxNode {
expected: "assignment, expression, if...else, while, for, transform, filter, tool, async, find, remove, select, insert, index_assignment or yield",
2023-11-15 02:03:52 +00:00
actual: child.kind(),
location: child.start_position(),
relevant_source: source[child.byte_range()].to_string(),
}),
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-06 17:32:58 +00:00
match self {
2023-10-10 17:29:11 +00:00
Statement::Assignment(assignment) => assignment.run(source, context),
Statement::Return(expression) => expression.run(source, context),
2023-10-10 17:29:11 +00:00
Statement::Expression(expression) => expression.run(source, context),
2023-11-30 14:30:25 +00:00
Statement::FunctionDeclaration(function_declaration) => {
function_declaration.run(source, context)
}
2023-10-10 17:29:11 +00:00
Statement::IfElse(if_else) => if_else.run(source, context),
Statement::Match(r#match) => r#match.run(source, context),
Statement::While(r#while) => r#while.run(source, context),
2023-11-11 01:44:03 +00:00
Statement::Block(block) => block.run(source, context),
2023-10-17 18:06:02 +00:00
Statement::For(r#for) => r#for.run(source, context),
Statement::Use(run) => run.run(source, context),
2023-11-15 01:41:57 +00:00
Statement::IndexAssignment(index_assignment) => index_assignment.run(source, context),
2023-10-06 17:32:58 +00:00
}
}
2023-11-30 00:23:42 +00:00
2023-11-30 05:57:15 +00:00
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> {
2023-11-30 00:23:42 +00:00
match self {
Statement::Assignment(assignment) => assignment.expected_type(context),
Statement::Return(expression) => expression.expected_type(context),
Statement::Expression(expression) => expression.expected_type(context),
2023-11-30 14:30:25 +00:00
Statement::FunctionDeclaration(function_declaration) => {
function_declaration.expected_type(context)
}
2023-11-30 00:23:42 +00:00
Statement::IfElse(if_else) => if_else.expected_type(context),
Statement::Match(r#match) => r#match.expected_type(context),
Statement::While(r#while) => r#while.expected_type(context),
Statement::Block(block) => block.expected_type(context),
Statement::For(r#for) => r#for.expected_type(context),
Statement::Use(r#use) => r#use.expected_type(context),
Statement::IndexAssignment(index_assignment) => index_assignment.expected_type(context),
}
}
2023-10-06 17:32:58 +00:00
}