Begin adding yield; Clean up
This commit is contained in:
parent
2d85a3ee2b
commit
f0cb4631ab
9
examples/yield.ds
Normal file
9
examples/yield.ds
Normal file
@ -0,0 +1,9 @@
|
||||
1 -> output
|
||||
|
||||
add_one = |list| {
|
||||
transform number in list {
|
||||
number + 1
|
||||
}
|
||||
}
|
||||
|
||||
[1, 2, 3] -> add_one
|
@ -25,7 +25,6 @@ pub mod math;
|
||||
pub mod remove;
|
||||
pub mod select;
|
||||
pub mod statement;
|
||||
pub mod sublist;
|
||||
pub mod transform;
|
||||
pub mod value_node;
|
||||
pub mod r#while;
|
||||
@ -33,8 +32,8 @@ pub mod r#while;
|
||||
pub use {
|
||||
assignment::*, block::*, built_in_function::*, expression::*, filter::*, find::*,
|
||||
function_call::*, identifier::*, if_else::*, index::*, insert::*, logic::*, math::*,
|
||||
r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*, statement::*, sublist::*,
|
||||
transform::*, value_node::*,
|
||||
r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*, statement::*, transform::*,
|
||||
value_node::*,
|
||||
};
|
||||
|
||||
use tree_sitter::Node;
|
||||
|
@ -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())))
|
||||
}
|
||||
}
|
62
tree-sitter-dust/corpus/yield.txt
Normal file
62
tree-sitter-dust/corpus/yield.txt
Normal 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)))))))
|
Loading…
Reference in New Issue
Block a user