1
0

Begin adding yield; Clean up

This commit is contained in:
Jeff 2023-11-06 19:10:12 -05:00
parent 2d85a3ee2b
commit f0cb4631ab
4 changed files with 73 additions and 38 deletions

9
examples/yield.ds Normal file
View File

@ -0,0 +1,9 @@
1 -> output
add_one = |list| {
transform number in list {
number + 1
}
}
[1, 2, 3] -> add_one

View File

@ -25,7 +25,6 @@ pub mod math;
pub mod remove; pub mod remove;
pub mod select; pub mod select;
pub mod statement; pub mod statement;
pub mod sublist;
pub mod transform; pub mod transform;
pub mod value_node; pub mod value_node;
pub mod r#while; pub mod r#while;
@ -33,8 +32,8 @@ pub mod r#while;
pub use { pub use {
assignment::*, block::*, built_in_function::*, expression::*, filter::*, find::*, assignment::*, block::*, built_in_function::*, expression::*, filter::*, find::*,
function_call::*, identifier::*, if_else::*, index::*, insert::*, logic::*, math::*, function_call::*, identifier::*, if_else::*, index::*, insert::*, logic::*, math::*,
r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*, statement::*, sublist::*, r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*, statement::*, transform::*,
transform::*, value_node::*, value_node::*,
}; };
use tree_sitter::Node; use tree_sitter::Node;

View File

@ -1,35 +0,0 @@
use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Expression, List, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Sublist {
list: Expression,
start: Expression,
end: Expression,
}
impl AbstractTree for Sublist {
fn from_syntax_node(source: &str, node: tree_sitter::Node) -> crate::Result<Self> {
let list_node = node.child(0).unwrap();
let list = Expression::from_syntax_node(source, list_node)?;
let start_node = node.child(2).unwrap();
let start = Expression::from_syntax_node(source, start_node)?;
let end_node = node.child(4).unwrap();
let end = Expression::from_syntax_node(source, end_node)?;
Ok(Sublist { list, start, end })
}
fn run(&self, source: &str, context: &mut crate::Map) -> crate::Result<crate::Value> {
let value = self.list.run(source, context)?;
let list = value.as_list()?.items();
let start = self.start.run(source, context)?.as_integer()? as usize;
let end = self.end.run(source, context)?.as_integer()? as usize;
let sublist = &list[start..=end];
Ok(Value::List(List::with_items(sublist.to_vec())))
}
}

View File

@ -0,0 +1,62 @@
================================================================================
Simple Yield
================================================================================
1 -> output
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(yield
(expression
(value
(integer)))
(function_call
(built_in_function)))))))
================================================================================
Long Yield
================================================================================
[1, 2, 3]
-> add_one
-> subtract_one
-> foobar
-> output
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(yield
(expression
(yield
(expression
(yield
(expression
(yield
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(function_call
(identifier))))
(function_call
(identifier))))
(function_call
(identifier))))
(function_call
(built_in_function)))))))