diff --git a/src/abstract_tree/function_declaration.rs b/src/abstract_tree/function_declaration.rs deleted file mode 100644 index 8840904..0000000 --- a/src/abstract_tree/function_declaration.rs +++ /dev/null @@ -1,83 +0,0 @@ -use serde::{Deserialize, Serialize}; -use tree_sitter::Node; - -use crate::{AbstractTree, Block, Function, Identifier, Map, Result, Type, TypeDefinition, Value}; - -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct FunctionDeclaration { - name: Option, - r#type: Option, - parameters: Vec, - body: Block, -} - -impl AbstractTree for FunctionDeclaration { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { - let name_node = node.child_by_field_name("name"); - let name = if let Some(child) = name_node { - Some(Identifier::from_syntax_node(source, child, context)?) - } else { - None - }; - - let type_definition_node = node.child_by_field_name("type"); - let type_definition = if let Some(child) = type_definition_node { - Some(TypeDefinition::from_syntax_node(source, child, context)?) - } else { - None - }; - - let mut parameters = Vec::new(); - - if node.child_by_field_name("parameters").is_some() { - for index in 3..node.child_count() - 2 { - let child = node.child(index).unwrap(); - - if child.is_named() { - let parameter = Identifier::from_syntax_node(source, child, context)?; - - parameters.push(parameter); - } - } - } - - let body_node = node.child_by_field_name("body").unwrap(); - let body = Block::from_syntax_node(source, body_node, context)?; - - Ok(FunctionDeclaration { - name, - r#type: type_definition.map(|defintion| defintion.take_inner()), - parameters, - body, - }) - } - - fn run(&self, _source: &str, context: &Map) -> Result { - let value = Value::Function(Function::new( - self.parameters.clone(), - self.body.clone(), - self.r#type.clone(), - )); - - if let Some(name) = &self.name { - let key = name.inner().clone(); - - context.set(key, value, self.r#type.clone())?; - - Ok(Value::Empty) - } else { - Ok(value) - } - } - - fn expected_type(&self, _context: &Map) -> Result { - if self.name.is_some() { - Ok(Type::Empty) - } else { - Ok(self.r#type.clone().unwrap_or(Type::Function { - parameter_types: vec![Type::Any; self.parameters.len()], - return_type: Box::new(Type::Any), - })) - } - } -} diff --git a/src/abstract_tree/identifier_assignment.rs b/src/abstract_tree/identifier_assignment.rs deleted file mode 100644 index 6bb7d0c..0000000 --- a/src/abstract_tree/identifier_assignment.rs +++ /dev/null @@ -1,79 +0,0 @@ -use serde::{Deserialize, Serialize}; -use tree_sitter::Node; - -use crate::{AbstractTree, Error, Identifier, Map, Result, Statement, Value}; - -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct IdentifierAssignment { - identifier: Identifier, - operator: AssignmentOperator, - statement: Statement, -} - -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub enum AssignmentOperator { - Equal, - PlusEqual, - MinusEqual, -} - -impl AbstractTree for IdentifierAssignment { - fn from_syntax_node(source: &str, node: Node) -> Result { - let identifier_node = node.child(0).unwrap(); - let identifier = Identifier::from_syntax_node(source, identifier_node)?; - - let operator_node = node.child(1).unwrap().child(0).unwrap(); - let operator = match operator_node.kind() { - "=" => AssignmentOperator::Equal, - "+=" => AssignmentOperator::PlusEqual, - "-=" => AssignmentOperator::MinusEqual, - _ => { - return Err(Error::UnexpectedSyntaxNode { - expected: "=, += or -=", - actual: operator_node.kind(), - location: operator_node.start_position(), - relevant_source: source[operator_node.byte_range()].to_string(), - }) - } - }; - - let statement_node = node.child(2).unwrap(); - let statement = Statement::from_syntax_node(source, statement_node)?; - - Ok(IdentifierAssignment { - identifier, - operator, - statement, - }) - } - - fn run(&self, source: &str, context: &mut Map) -> Result { - let key = self.identifier.inner().clone(); - let value = self.statement.run(source, context)?; - let mut context = context.variables_mut(); - - let new_value = match self.operator { - AssignmentOperator::PlusEqual => { - if let Some(mut previous_value) = context.get(&key).cloned() { - previous_value += value; - previous_value - } else { - Value::Empty - } - } - AssignmentOperator::MinusEqual => { - if let Some(mut previous_value) = context.get(&key).cloned() { - previous_value -= value; - previous_value - } else { - Value::Empty - } - } - AssignmentOperator::Equal => value, - }; - - context.insert(key, new_value); - - Ok(Value::Empty) - } -} diff --git a/src/abstract_tree/insert.rs b/src/abstract_tree/insert.rs deleted file mode 100644 index 4a27639..0000000 --- a/src/abstract_tree/insert.rs +++ /dev/null @@ -1,44 +0,0 @@ -use serde::{Deserialize, Serialize}; -use tree_sitter::Node; - -use crate::{AbstractTree, Expression, Identifier, Map, Result, Value}; - -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct Insert { - identifier: Identifier, - expression: Expression, -} - -impl AbstractTree for Insert { - fn from_syntax_node(source: &str, node: Node) -> Result { - let identifier_node = node.child(2).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)?; - - Ok(Insert { - identifier, - expression, - }) - } - - fn run(&self, source: &str, context: &mut Map) -> Result { - let table_name = self.identifier.inner().clone(); - let mut table = self.identifier.run(source, context)?.as_table()?.clone(); - let new_rows = self.expression.run(source, context)?.into_inner_list()?; - let values = new_rows.items(); - - table.reserve(values.len()); - - for row in values.iter() { - let row_values = row.clone().into_inner_list()?; - table.insert(row_values.items().clone())?; - } - - context - .variables_mut()? - .insert(table_name, Value::Table(table)); - - Ok(Value::Empty) - } -} diff --git a/src/abstract_tree/remove.rs b/src/abstract_tree/remove.rs deleted file mode 100644 index e387463..0000000 --- a/src/abstract_tree/remove.rs +++ /dev/null @@ -1,75 +0,0 @@ -use std::sync::RwLock; - -use rayon::prelude::*; -use serde::{Deserialize, Serialize}; -use tree_sitter::Node; - -use crate::{AbstractTree, Block, Error, Expression, Identifier, Map, Result, Value}; - -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct Remove { - item_id: Identifier, - collection: Expression, - predicate: Block, -} - -impl AbstractTree for Remove { - fn from_syntax_node(source: &str, node: Node) -> Result { - let identifier_node = node.child(1).unwrap(); - let item_id = Identifier::from_syntax_node(source, identifier_node)?; - - let expression_node = node.child(3).unwrap(); - let collection = Expression::from_syntax_node(source, expression_node)?; - - let block_node = node.child(4).unwrap(); - let predicate = Block::from_syntax_node(source, block_node)?; - - Ok(Remove { - item_id, - collection, - predicate, - }) - } - - fn run(&self, source: &str, context: &mut Map) -> Result { - let value = self.collection.run(source, context)?; - let values = value.as_list()?; - let key = self.item_id.inner(); - let should_remove_index = RwLock::new(None); - - values - .items() - .par_iter() - .enumerate() - .try_for_each(|(index, value)| { - if should_remove_index.read()?.is_some() { - return Ok(()); - } - - let iter_context = Map::clone_from(context)?; - - iter_context - .variables_mut()? - .insert(key.clone(), value.clone()); - - let should_remove = self - .predicate - .run(source, &mut iter_context.clone())? - .as_boolean()?; - - if should_remove { - let _ = should_remove_index.write()?.insert(index); - } - - Ok::<(), Error>(()) - })?; - - let index = should_remove_index.read()?; - - if let Some(index) = *index { - Ok(values.items_mut().remove(index)) - } else { - Ok(Value::Empty) - } - } -} diff --git a/src/abstract_tree/select.rs b/src/abstract_tree/select.rs deleted file mode 100644 index 7cea7b2..0000000 --- a/src/abstract_tree/select.rs +++ /dev/null @@ -1,108 +0,0 @@ -use serde::{Deserialize, Serialize}; -use tree_sitter::Node; - -use crate::{AbstractTree, Block, Expression, Identifier, Map, Result, Table, Value}; - -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct Select { - identifiers: Vec, - expression: Expression, - predicate: Option, -} - -impl AbstractTree for Select { - fn from_syntax_node(source: &str, node: Node) -> Result { - let child_count = node.child_count(); - let mut identifiers = Vec::new(); - - let identifier_list = node.child(1).unwrap(); - - for index in 1..identifier_list.child_count() - 1 { - let node = identifier_list.child(index).unwrap(); - - if node.is_named() { - let identifier = Identifier::from_syntax_node(source, node)?; - identifiers.push(identifier); - } - } - - let expression_node = node.child(3).unwrap(); - let expression = Expression::from_syntax_node(source, expression_node)?; - - let final_node = node.child(child_count - 1).unwrap(); - - let predicate = if final_node.kind() == "block" { - Some(Block::from_syntax_node(source, final_node)?) - } else { - None - }; - - Ok(Select { - identifiers, - expression, - predicate, - }) - } - - fn run(&self, source: &str, context: &mut Map) -> Result { - let value = self.expression.run(source, context)?; - let old_table = value.as_table()?; - let column_names = if !self.identifiers.is_empty() { - self.identifiers - .iter() - .cloned() - .map(|identifier| identifier.take_inner()) - .collect() - } else { - old_table.headers().clone() - }; - let mut new_table = Table::new(column_names.to_vec()); - - for row in old_table.rows() { - let mut new_row = Vec::new(); - let row_context = Map::new(); - - for (i, value) in row.iter().enumerate() { - let column_name = old_table.headers().get(i).unwrap(); - - row_context - .variables_mut()? - .insert(column_name.clone(), value.clone()); - - let new_table_column_index = - new_table - .headers() - .iter() - .enumerate() - .find_map(|(index, new_column_name)| { - if new_column_name == column_name { - Some(index) - } else { - None - } - }); - - if let Some(index) = new_table_column_index { - while new_row.len() < index + 1 { - new_row.push(Value::Empty); - } - new_row[index] = value.clone(); - } - } - - if let Some(where_clause) = &self.predicate { - let should_include = where_clause - .run(source, &mut row_context.clone())? - .as_boolean()?; - - if should_include { - new_table.insert(new_row)?; - } - } else { - new_table.insert(new_row)?; - } - } - - Ok(Value::Table(new_table)) - } -} diff --git a/src/abstract_tree/transform.rs b/src/abstract_tree/transform.rs deleted file mode 100644 index df9d409..0000000 --- a/src/abstract_tree/transform.rs +++ /dev/null @@ -1,55 +0,0 @@ -use rayon::prelude::*; -use serde::{Deserialize, Serialize}; -use tree_sitter::Node; - -use crate::{AbstractTree, Block, Expression, Identifier, List, Map, Result, Value}; - -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct Transform { - identifier: Identifier, - expression: Expression, - item: Block, -} - -impl AbstractTree for Transform { - fn from_syntax_node(source: &str, node: Node) -> Result { - 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(4).unwrap(); - let item = Block::from_syntax_node(source, item_node)?; - - Ok(Transform { - identifier, - expression, - item, - }) - } - - fn run(&self, source: &str, context: &mut Map) -> Result { - let expression_run = self.expression.run(source, context)?; - let values = expression_run.as_list()?.items(); - let key = self.identifier.inner(); - let new_values = values - .par_iter() - .map(|value| { - let iter_context = Map::clone_from(context).unwrap(); - - iter_context - .variables_mut() - .unwrap() - .insert(key.clone(), value.clone()); - - self.item - .run(source, &mut iter_context.clone()) - .unwrap_or_default() - }) - .filter(|value| !value.is_empty()) - .collect(); - - Ok(Value::List(List::with_items(new_values))) - } -}