2023-11-05 18:54:29 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tree_sitter::Node;
|
|
|
|
|
2023-11-30 05:57:15 +00:00
|
|
|
use crate::{AbstractTree, Expression, FunctionCall, Map, Result, TypeDefinition, 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 {
|
2023-11-30 03:54:46 +00:00
|
|
|
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
|
2023-11-05 18:54:29 +00:00
|
|
|
let input_node = node.child(0).unwrap();
|
2023-11-30 03:54:46 +00:00
|
|
|
let input = Expression::from_syntax_node(source, input_node, context)?;
|
2023-11-05 18:54:29 +00:00
|
|
|
|
2023-11-16 02:13:14 +00:00
|
|
|
let function_node = node.child(3).unwrap();
|
2023-11-30 03:54:46 +00:00
|
|
|
let function = Expression::from_syntax_node(source, function_node, context)?;
|
2023-11-28 22:54:17 +00:00
|
|
|
|
2023-11-16 02:13:14 +00:00
|
|
|
let mut arguments = Vec::new();
|
2023-11-05 18:54:29 +00:00
|
|
|
|
2023-11-16 02:13:14 +00:00
|
|
|
arguments.push(input);
|
|
|
|
|
|
|
|
for index in 4..node.child_count() - 1 {
|
|
|
|
let node = node.child(index).unwrap();
|
|
|
|
|
|
|
|
if node.is_named() {
|
2023-11-30 03:54:46 +00:00
|
|
|
let expression = Expression::from_syntax_node(source, node, context)?;
|
2023-11-16 02:13:14 +00:00
|
|
|
|
|
|
|
arguments.push(expression);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
let call = FunctionCall::new(function, arguments);
|
2023-11-16 02:13:14 +00:00
|
|
|
|
|
|
|
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> {
|
2023-11-16 02:13:14 +00:00
|
|
|
self.call.run(source, context)
|
2023-11-05 18:54:29 +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
|
|
|
self.call.expected_type(context)
|
|
|
|
}
|
2023-11-05 18:54:29 +00:00
|
|
|
}
|