From 75f16a3afe43da60fcc3b3375aa6cae2f04bb396 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 22 Oct 2023 13:55:56 -0400 Subject: [PATCH] Fix README example; Begin implementing insert --- README.md | 4 +- examples/table.ds | 23 +- src/abstract_tree/insert.rs | 40 + src/abstract_tree/mod.rs | 5 +- src/abstract_tree/statement.rs | 11 +- tree-sitter-dust/corpus/tables.txt | 17 +- tree-sitter-dust/grammar.js | 5 +- tree-sitter-dust/src/grammar.json | 32 +- tree-sitter-dust/src/node-types.json | 20 +- tree-sitter-dust/src/parser.c | 7017 +++++++++++++------------- 10 files changed, 3528 insertions(+), 3646 deletions(-) create mode 100644 src/abstract_tree/insert.rs diff --git a/README.md b/README.md index da330e0..767b858 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,9 @@ Querying a table is similar to SQL. ```dust names = select name from animals -youngins = select species from animals where age <= 10 +youngins = select species from animals { + age <= 10 +} ``` The keywords `table` and `insert` make sure that all of the memory used to hold the rows is allocated at once, so it is good practice to group your rows together instead of using a call for each row. diff --git a/examples/table.ds b/examples/table.ds index 4715bae..10bf4b7 100644 --- a/examples/table.ds +++ b/examples/table.ds @@ -15,12 +15,29 @@ test_select = select from my_table (assert_equal test_select, test_table) test_table = table [ - ["a", 1, true] - ["a", 3, true] + [1, true] + [3, true] ] -test_select_where = select <> from my_table { +test_select_where = select from my_table { text == "a" } (assert_equal test_select_where, test_table) + +test_table = table [ + ["a", 1, true] + ["b", 2, true] + ["a", 3, true] + ["c", 4, true] + ["d", 5, true] + ["e", 6, true] +] + +insert into my_table [ + ["c", 4, true] + ["d", 5, true] + ["e", 6, true] +] + +(assert_equal test_table, my_table) diff --git a/src/abstract_tree/insert.rs b/src/abstract_tree/insert.rs new file mode 100644 index 0000000..c274a6b --- /dev/null +++ b/src/abstract_tree/insert.rs @@ -0,0 +1,40 @@ +use serde::{Deserialize, Serialize}; +use tree_sitter::Node; + +use crate::{AbstractTree, Expression, Identifier, Item, Result, Value, VariableMap}; + +#[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(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)?; + + Ok(Insert { + identifier, + expression, + }) + } + + fn run(&self, source: &str, context: &mut VariableMap) -> 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()?; + + table.reserve(new_rows.len()); + + for row in new_rows { + table.insert(row.into_inner_list()?)?; + } + + context.set_value(table_name, Value::Table(table))?; + + Ok(Value::Empty) + } +} diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index d194891..1b8379e 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -15,6 +15,7 @@ pub mod r#for; pub mod function_call; pub mod identifier; pub mod if_else; +pub mod insert; pub mod item; pub mod logic; pub mod r#match; @@ -29,8 +30,8 @@ pub mod r#while; pub use { assignment::*, expression::*, filter::*, find::*, function_call::*, identifier::*, if_else::*, - item::*, logic::*, math::*, r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*, - statement::*, tool::*, transform::*, value_node::*, + insert::*, item::*, logic::*, math::*, r#async::*, r#for::*, r#match::*, r#while::*, remove::*, + select::*, statement::*, tool::*, transform::*, value_node::*, }; use tree_sitter::Node; diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index e281fad..1500783 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; use crate::{ - AbstractTree, Assignment, Async, Error, Expression, Filter, Find, For, IfElse, Match, Remove, - Result, Select, Transform, Value, VariableMap, While, + AbstractTree, Assignment, Async, Error, Expression, Filter, Find, For, IfElse, Insert, Match, + Remove, Result, Select, Transform, Value, VariableMap, While, }; /// Abstract representation of a statement. @@ -24,6 +24,7 @@ pub enum Statement { Find(Box), Remove(Box), Select(Box