1
0

Implement transform loop

This commit is contained in:
Jeff 2023-10-17 16:40:07 -04:00
parent 686f7b435d
commit 2b882f1137
5 changed files with 66 additions and 5 deletions

View File

@ -0,0 +1,8 @@
list = [1 2 3]
new_list = transform i in list {
i + 1
}
(assert_equal [2 3 4] new_list)

View File

@ -19,12 +19,13 @@ pub mod r#match;
pub mod math; pub mod math;
pub mod statement; pub mod statement;
pub mod tool; pub mod tool;
pub mod transform;
pub mod value_node; pub mod value_node;
pub mod r#while; pub mod r#while;
pub use { pub use {
assignment::*, expression::*, function_call::*, identifier::*, if_else::*, item::*, logic::*, assignment::*, expression::*, function_call::*, identifier::*, if_else::*, item::*, logic::*,
math::*, r#async::*, r#for::*, r#match::*, r#while::*, statement::*, math::*, r#async::*, r#for::*, r#match::*, r#while::*, statement::*, transform::*,
}; };
use tree_sitter::Node; use tree_sitter::Node;

View File

@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Assignment, Async, Error, Expression, For, IfElse, Match, Result, Value, AbstractTree, Assignment, Async, Error, Expression, For, IfElse, Match, Result, Transform,
VariableMap, While, Value, VariableMap, While,
}; };
/// Abstract representation of a statement. /// Abstract representation of a statement.
@ -19,6 +19,7 @@ pub enum Statement {
While(Box<While>), While(Box<While>),
Async(Box<Async>), Async(Box<Async>),
For(Box<For>), For(Box<For>),
Transform(Box<Transform>),
} }
impl AbstractTree for Statement { impl AbstractTree for Statement {
@ -49,8 +50,11 @@ impl AbstractTree for Statement {
"for" => Ok(Statement::For(Box::new(For::from_syntax_node( "for" => Ok(Statement::For(Box::new(For::from_syntax_node(
source, child, source, child,
)?))), )?))),
"transform" => Ok(Statement::Transform(Box::new(Transform::from_syntax_node(
source, child,
)?))),
_ => Err(Error::UnexpectedSyntaxNode { _ => Err(Error::UnexpectedSyntaxNode {
expected: "assignment, expression, if...else, while, tool or async", expected: "assignment, expression, if...else, while, for, transform, tool or async",
actual: child.kind(), actual: child.kind(),
location: child.start_position(), location: child.start_position(),
relevant_source: source[child.byte_range()].to_string(), relevant_source: source[child.byte_range()].to_string(),
@ -67,6 +71,7 @@ impl AbstractTree for Statement {
Statement::While(r#while) => r#while.run(source, context), Statement::While(r#while) => r#while.run(source, context),
Statement::Async(run) => run.run(source, context), Statement::Async(run) => run.run(source, context),
Statement::For(r#for) => r#for.run(source, context), Statement::For(r#for) => r#for.run(source, context),
Statement::Transform(transform) => transform.run(source, context),
} }
} }
} }

View File

@ -0,0 +1,47 @@
use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Expression, Identifier, Item, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Transform {
identifier: Identifier,
expression: Expression,
item: Item,
}
impl AbstractTree for Transform {
fn from_syntax_node(source: &str, node: tree_sitter::Node) -> crate::Result<Self> {
let identifier_node = node.child(1).unwrap();
let identifier = Identifier::from_syntax_node(source, identifier_node)?;
let expression_node = node.child(3).unwrap();
let expression = Expression::from_syntax_node(source, expression_node)?;
let item_node = node.child(5).unwrap();
let item = Item::from_syntax_node(source, item_node)?;
Ok(Transform {
identifier,
expression,
item,
})
}
fn run(&self, source: &str, context: &mut crate::VariableMap) -> crate::Result<crate::Value> {
let value = self.expression.run(source, context)?;
let list = value.as_list()?;
let key = self.identifier.inner();
let mut context = context.clone();
let mut new_list = Vec::with_capacity(list.len());
for value in list {
context.set_value(key.clone(), value.clone())?;
let value = self.item.run(source, &mut context)?;
new_list.push(value);
}
Ok(Value::List(new_list))
}
}

@ -1 +1 @@
Subproject commit 9fb89fcd8b29386735de0bad42232ef3d72e7e91 Subproject commit 6aab8e8f65a29ada418b09c5cdae0583c37b7b23