dust/src/abstract_tree/expression.rs

73 lines
2.8 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-10-28 14:28:43 +00:00
use crate::{
2023-10-29 23:31:06 +00:00
value_node::ValueNode, AbstractTree, Error, Identifier, Index, Map, Result, Sublist, Tool,
Value,
2023-10-28 14:28:43 +00:00
};
2023-10-06 17:32:58 +00:00
use super::{function_call::FunctionCall, logic::Logic, math::Math};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum Expression {
2023-10-10 17:29:11 +00:00
Value(ValueNode),
2023-10-06 17:32:58 +00:00
Identifier(Identifier),
2023-10-28 14:28:43 +00:00
Sublist(Box<Sublist>),
2023-10-29 23:31:06 +00:00
Index(Box<Index>),
2023-10-06 17:32:58 +00:00
Math(Box<Math>),
Logic(Box<Logic>),
FunctionCall(FunctionCall),
2023-10-21 17:47:08 +00:00
Tool(Box<Tool>),
2023-10-06 17:32:58 +00:00
}
impl AbstractTree for Expression {
2023-10-10 17:29:11 +00:00
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
2023-10-06 17:32:58 +00:00
debug_assert_eq!("expression", node.kind());
2023-10-13 23:56:57 +00:00
for index in 0..node.child_count() {
let child = node.child(index).unwrap();
let expression = match child.kind() {
"value" => Expression::Value(ValueNode::from_syntax_node(source, child)?),
2023-10-28 14:28:43 +00:00
"identifier" => {
Expression::Identifier(Identifier::from_syntax_node(source, child)?)
}
"sublist" => {
Expression::Sublist(Box::new(Sublist::from_syntax_node(source, child)?))
}
2023-10-29 23:31:06 +00:00
"index" => Expression::Index(Box::new(Index::from_syntax_node(source, child)?)),
2023-10-13 23:56:57 +00:00
"math" => Expression::Math(Box::new(Math::from_syntax_node(source, child)?)),
"logic" => Expression::Logic(Box::new(Logic::from_syntax_node(source, child)?)),
"function_call" => {
Expression::FunctionCall(FunctionCall::from_syntax_node(source, child)?)
}
2023-10-21 17:47:08 +00:00
"tool" => Expression::Tool(Box::new(Tool::from_syntax_node(source, child)?)),
2023-10-13 23:56:57 +00:00
_ => continue,
};
return Ok(expression);
}
2023-10-06 17:32:58 +00:00
let child = node.child(0).unwrap();
2023-10-13 23:56:57 +00:00
Err(Error::UnexpectedSyntaxNode {
2023-10-29 23:31:06 +00:00
expected: "value, identifier, sublist, index, math or function_call",
2023-10-13 23:56:57 +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-10-25 20:44:50 +00:00
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
2023-10-06 17:32:58 +00:00
match self {
2023-10-10 17:29:11 +00:00
Expression::Value(value_node) => value_node.run(source, context),
Expression::Identifier(identifier) => identifier.run(source, context),
2023-10-28 14:28:43 +00:00
Expression::Sublist(sublist) => sublist.run(source, context),
2023-10-10 17:29:11 +00:00
Expression::Math(math) => math.run(source, context),
Expression::Logic(logic) => logic.run(source, context),
Expression::FunctionCall(function_call) => function_call.run(source, context),
2023-10-21 17:47:08 +00:00
Expression::Tool(tool) => tool.run(source, context),
2023-10-29 23:31:06 +00:00
Expression::Index(index) => index.run(source, context),
2023-10-06 17:32:58 +00:00
}
}
}