dust/src/abstract_tree/yield.rs

46 lines
1.3 KiB
Rust
Raw Normal View History

2023-11-05 18:54:29 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-11-30 01:59:58 +00:00
use crate::{AbstractTree, Expression, FunctionCall, Map, Result, Type, Value};
2023-11-05 18:54:29 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Yield {
call: FunctionCall,
}
impl AbstractTree for Yield {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
let input_node = node.child(0).unwrap();
let input = Expression::from_syntax_node(source, input_node)?;
let function_node = node.child(3).unwrap();
2023-11-28 22:54:17 +00:00
let function = Expression::from_syntax_node(source, function_node)?;
let mut arguments = Vec::new();
2023-11-05 18:54:29 +00:00
arguments.push(input);
for index in 4..node.child_count() - 1 {
let node = node.child(index).unwrap();
if node.is_named() {
let expression = Expression::from_syntax_node(source, node)?;
arguments.push(expression);
}
}
2023-11-28 22:54:17 +00:00
let call = FunctionCall::new(function, arguments);
Ok(Yield { call })
2023-11-05 18:54:29 +00:00
}
2023-11-30 00:23:42 +00:00
fn run(&self, source: &str, context: &Map) -> Result<Value> {
self.call.run(source, context)
2023-11-05 18:54:29 +00:00
}
2023-11-30 00:23:42 +00:00
2023-11-30 01:59:58 +00:00
fn expected_type(&self, context: &Map) -> Result<Type> {
2023-11-30 00:23:42 +00:00
self.call.expected_type(context)
}
2023-11-05 18:54:29 +00:00
}