diff --git a/src/abstract_tree/assignment.rs b/src/abstract_tree/assignment.rs index 1b3844c..72a72fa 100644 --- a/src/abstract_tree/assignment.rs +++ b/src/abstract_tree/assignment.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Result, Value, VariableMap}; +use crate::{AbstractTree, Error, Map, Result, Value}; use super::{identifier::Identifier, statement::Statement}; @@ -49,7 +49,7 @@ impl AbstractTree for Assignment { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let key = self.identifier.inner().clone(); let value = self.statement.run(source, context)?; diff --git a/src/abstract_tree/async.rs b/src/abstract_tree/async.rs index 29583cc..1cd9d34 100644 --- a/src/abstract_tree/async.rs +++ b/src/abstract_tree/async.rs @@ -2,7 +2,7 @@ use rayon::prelude::*; use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Result, Statement, Value, VariableMap}; +use crate::{AbstractTree, Error, Map, Result, Statement, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Async { @@ -37,7 +37,7 @@ impl AbstractTree for Async { Ok(Async { statements }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let statements = &self.statements; statements diff --git a/src/abstract_tree/expression.rs b/src/abstract_tree/expression.rs index 7f5cbe7..1407869 100644 --- a/src/abstract_tree/expression.rs +++ b/src/abstract_tree/expression.rs @@ -1,9 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{ - value_node::ValueNode, AbstractTree, Error, Identifier, Result, Tool, Value, VariableMap, -}; +use crate::{value_node::ValueNode, AbstractTree, Error, Identifier, Map, Result, Tool, Value}; use super::{function_call::FunctionCall, logic::Logic, math::Math}; @@ -48,7 +46,7 @@ impl AbstractTree for Expression { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { match self { Expression::Value(value_node) => value_node.run(source, context), Expression::Identifier(identifier) => identifier.run(source, context), diff --git a/src/abstract_tree/filter.rs b/src/abstract_tree/filter.rs index 44081c6..4fa41d5 100644 --- a/src/abstract_tree/filter.rs +++ b/src/abstract_tree/filter.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Identifier, Item, Result, Value, VariableMap}; +use crate::{AbstractTree, Expression, Identifier, Item, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Filter { @@ -28,7 +28,7 @@ impl AbstractTree for Filter { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let value = self.expression.run(source, context)?; let list = value.as_list()?; let key = self.identifier.inner(); diff --git a/src/abstract_tree/find.rs b/src/abstract_tree/find.rs index 65690a6..a5d30c3 100644 --- a/src/abstract_tree/find.rs +++ b/src/abstract_tree/find.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Identifier, Item, Result, Value, VariableMap}; +use crate::{AbstractTree, Expression, Identifier, Item, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Find { @@ -28,7 +28,7 @@ impl AbstractTree for Find { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let value = self.expression.run(source, context)?; let list = value.as_list()?; let key = self.identifier.inner(); diff --git a/src/abstract_tree/for.rs b/src/abstract_tree/for.rs index cefd260..2321740 100644 --- a/src/abstract_tree/for.rs +++ b/src/abstract_tree/for.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Identifier, Item, Result, Value, VariableMap}; +use crate::{AbstractTree, Expression, Identifier, Item, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct For { @@ -28,7 +28,7 @@ impl AbstractTree for For { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let value = self.expression.run(source, context)?; let list = value.as_list()?; let key = self.identifier.inner(); diff --git a/src/abstract_tree/function_call.rs b/src/abstract_tree/function_call.rs index fdd7374..d0e4134 100644 --- a/src/abstract_tree/function_call.rs +++ b/src/abstract_tree/function_call.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Result, Value, VariableMap}; +use crate::{AbstractTree, Error, Map, Result, Value}; use super::{expression::Expression, identifier::Identifier}; @@ -33,7 +33,7 @@ impl AbstractTree for FunctionCall { Ok(FunctionCall { name, arguments }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let key = self.name.inner(); let definition = if let Some(value) = context.get_value(key)? { value.as_function().cloned()? diff --git a/src/abstract_tree/identifier.rs b/src/abstract_tree/identifier.rs index 88b8bea..106baf6 100644 --- a/src/abstract_tree/identifier.rs +++ b/src/abstract_tree/identifier.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Result, Value, VariableMap}; +use crate::{AbstractTree, Error, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Identifier(String); @@ -29,7 +29,7 @@ impl AbstractTree for Identifier { Ok(Identifier(identifier.to_string())) } - fn run(&self, _source: &str, context: &mut VariableMap) -> Result { + fn run(&self, _source: &str, context: &mut Map) -> Result { let value = if let Some(value) = context.get_value(&self.0)? { value } else { diff --git a/src/abstract_tree/if_else.rs b/src/abstract_tree/if_else.rs index c6fc256..7ca34e3 100644 --- a/src/abstract_tree/if_else.rs +++ b/src/abstract_tree/if_else.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Result, Statement, Value, VariableMap}; +use crate::{AbstractTree, Expression, Map, Result, Statement, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct IfElse { @@ -57,7 +57,7 @@ impl AbstractTree for IfElse { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let if_boolean = self.if_expression.run(source, context)?.as_boolean()?; if if_boolean { diff --git a/src/abstract_tree/insert.rs b/src/abstract_tree/insert.rs index 40e390f..a4affbe 100644 --- a/src/abstract_tree/insert.rs +++ b/src/abstract_tree/insert.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Identifier, Result, Value, VariableMap}; +use crate::{AbstractTree, Expression, Identifier, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Insert { @@ -22,7 +22,7 @@ impl AbstractTree for Insert { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + 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()?; diff --git a/src/abstract_tree/item.rs b/src/abstract_tree/item.rs index b311e8e..49ae564 100644 --- a/src/abstract_tree/item.rs +++ b/src/abstract_tree/item.rs @@ -4,7 +4,7 @@ use rayon::prelude::*; use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Result, Statement, Value, VariableMap}; +use crate::{AbstractTree, Error, Map, Result, Statement, Value}; /// An abstractiton of an independent unit of source code, or a comment. /// @@ -21,7 +21,7 @@ impl Item { Self { statements } } - pub fn run_parallel(&self, source: &str, context: &mut VariableMap) -> Result { + pub fn run_parallel(&self, source: &str, context: &mut Map) -> Result { let statements = &self.statements; let run_result = statements.into_par_iter().try_for_each(|statement| { let mut context = context.clone(); @@ -63,7 +63,7 @@ impl AbstractTree for Item { Ok(Item { statements }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let mut prev_result = Ok(Value::Empty); for statement in &self.statements { diff --git a/src/abstract_tree/logic.rs b/src/abstract_tree/logic.rs index adedad4..5cc033e 100644 --- a/src/abstract_tree/logic.rs +++ b/src/abstract_tree/logic.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Expression, Result, Value, VariableMap}; +use crate::{AbstractTree, Error, Expression, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Logic { @@ -44,7 +44,7 @@ impl AbstractTree for Logic { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let left = self.left.run(source, context)?; let right = self.right.run(source, context)?; let result = match self.operator { diff --git a/src/abstract_tree/match.rs b/src/abstract_tree/match.rs index 8e35e25..5c95eef 100644 --- a/src/abstract_tree/match.rs +++ b/src/abstract_tree/match.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Result, Value, VariableMap}; +use crate::{AbstractTree, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Match {} @@ -16,7 +16,7 @@ impl AbstractTree for Match { todo!() } - fn run(&self, _source: &str, _context: &mut VariableMap) -> Result { + fn run(&self, _source: &str, _context: &mut Map) -> Result { todo!() } } diff --git a/src/abstract_tree/math.rs b/src/abstract_tree/math.rs index 905a3cc..cbb93b6 100644 --- a/src/abstract_tree/math.rs +++ b/src/abstract_tree/math.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Expression, Result, Value, VariableMap}; +use crate::{AbstractTree, Error, Expression, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Math { @@ -42,7 +42,7 @@ impl AbstractTree for Math { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let left = self.left.run(source, context)?; let right = self.right.run(source, context)?; let value = match self.operator { diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index 1b8379e..407da7c 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -36,7 +36,7 @@ pub use { use tree_sitter::Node; -use crate::{Result, Value, VariableMap}; +use crate::{Map, Result, Value}; /// This trait is implemented by the Evaluator's internal types to form an /// executable tree that resolves to a single value. @@ -53,5 +53,5 @@ pub trait AbstractTree: Sized { fn from_syntax_node(source: &str, node: Node) -> Result; /// Execute dust code by traversing the tree - fn run(&self, source: &str, context: &mut VariableMap) -> Result; + fn run(&self, source: &str, context: &mut Map) -> Result; } diff --git a/src/abstract_tree/remove.rs b/src/abstract_tree/remove.rs index a923223..0e92175 100644 --- a/src/abstract_tree/remove.rs +++ b/src/abstract_tree/remove.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Identifier, Item, Result, Value, VariableMap}; +use crate::{AbstractTree, Expression, Identifier, Item, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Remove { @@ -28,7 +28,7 @@ impl AbstractTree for Remove { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let value = self.expression.run(source, context)?; let mut list = value.into_inner_list()?; let key = self.identifier.inner(); diff --git a/src/abstract_tree/select.rs b/src/abstract_tree/select.rs index 51b1af9..20e214a 100644 --- a/src/abstract_tree/select.rs +++ b/src/abstract_tree/select.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Identifier, Item, Result, Table, Value, VariableMap}; +use crate::{AbstractTree, Expression, Identifier, Item, Map, Result, Table, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Select { @@ -53,7 +53,7 @@ impl AbstractTree for Select { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + 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.len() > 0 { @@ -70,7 +70,7 @@ impl AbstractTree for Select { for row in old_table.rows() { let mut new_row = Vec::new(); - let mut row_context = VariableMap::new(); + let mut row_context = Map::new(); for (i, value) in row.iter().enumerate() { let column_name = old_table.headers().get(i).unwrap(); diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index 1500783..80e620b 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, Insert, Match, - Remove, Result, Select, Transform, Value, VariableMap, While, + AbstractTree, Assignment, Async, Error, Expression, Filter, Find, For, IfElse, Insert, Map, + Match, Remove, Result, Select, Transform, Value, While, }; /// Abstract representation of a statement. @@ -82,7 +82,7 @@ impl AbstractTree for Statement { } } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { match self { Statement::Assignment(assignment) => assignment.run(source, context), Statement::Expression(expression) => expression.run(source, context), diff --git a/src/abstract_tree/tool.rs b/src/abstract_tree/tool.rs index 4119b9e..091e2f3 100644 --- a/src/abstract_tree/tool.rs +++ b/src/abstract_tree/tool.rs @@ -11,7 +11,7 @@ use reqwest::blocking::get; use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Expression, Result, Table, Value, ValueType, VariableMap}; +use crate::{AbstractTree, Error, Expression, Map, Result, Table, Value, ValueType}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub enum Tool { @@ -243,7 +243,7 @@ impl AbstractTree for Tool { Ok(tool) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { match self { Tool::Assert(expressions) => { for expression in expressions { @@ -364,7 +364,7 @@ impl AbstractTree for Tool { let created = metadata.created()?.elapsed()?.as_secs() as i64; let modified = metadata.modified()?.elapsed()?.as_secs() as i64; let accessed = metadata.accessed()?.elapsed()?.as_secs() as i64; - let mut metadata_output = VariableMap::new(); + let mut metadata_output = Map::new(); metadata_output.set_value("type".to_string(), Value::String(file_type))?; metadata_output.set_value("size".to_string(), Value::Integer(size))?; diff --git a/src/abstract_tree/transform.rs b/src/abstract_tree/transform.rs index 908dbe7..94db846 100644 --- a/src/abstract_tree/transform.rs +++ b/src/abstract_tree/transform.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Identifier, Item, Result, Value, VariableMap}; +use crate::{AbstractTree, Expression, Identifier, Item, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Transform { @@ -28,7 +28,7 @@ impl AbstractTree for Transform { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let value = self.expression.run(source, context)?; let list = value.as_list()?; let key = self.identifier.inner(); diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index d28457b..f92d691 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; use crate::{ - AbstractTree, Error, Expression, Function, Identifier, Item, Result, Table, Value, ValueType, - VariableMap, + AbstractTree, Error, Expression, Function, Identifier, Item, Map, Result, Table, Value, + ValueType, }; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -136,7 +136,7 @@ impl AbstractTree for ValueNode { }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { let value_source = &source[self.byte_range()]; let value = match &self.value_type { ValueType::Any => todo!(), @@ -161,7 +161,7 @@ impl AbstractTree for ValueNode { } ValueType::Empty => Value::Empty, ValueType::Map(nodes) => { - let mut values = VariableMap::new(); + let mut values = Map::new(); for (key, node) in nodes { let value = node.run(source, context)?; diff --git a/src/abstract_tree/while.rs b/src/abstract_tree/while.rs index c6c139f..4bb1ac7 100644 --- a/src/abstract_tree/while.rs +++ b/src/abstract_tree/while.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, Item, Result, Value, VariableMap}; +use crate::{AbstractTree, Expression, Item, Map, Result, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct While { @@ -29,7 +29,7 @@ impl AbstractTree for While { Ok(While { expression, items }) } - fn run(&self, source: &str, context: &mut VariableMap) -> Result { + fn run(&self, source: &str, context: &mut Map) -> Result { while self.expression.run(source, context)?.as_boolean()? { for item in &self.items { item.run(source, context)?; diff --git a/src/evaluator.rs b/src/evaluator.rs index 3bf868b..b709249 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -6,7 +6,7 @@ use std::fmt::{self, Debug, Formatter}; use tree_sitter::{Parser, Tree as TSTree}; -use crate::{abstract_tree::item::Item, language, AbstractTree, Result, Value, VariableMap}; +use crate::{abstract_tree::item::Item, language, AbstractTree, Map, Result, Value}; /// Evaluate the given source code. /// @@ -20,7 +20,7 @@ use crate::{abstract_tree::item::Item, language, AbstractTree, Result, Value, Va /// assert_eq!(evaluate("1 + 2 + 3"), Ok(Value::Integer(6))); /// ``` pub fn evaluate(source: &str) -> Result { - let mut context = VariableMap::new(); + let mut context = Map::new(); evaluate_with_context(source, &mut context) } @@ -44,7 +44,7 @@ pub fn evaluate(source: &str) -> Result { /// Ok(Value::Integer(10)) /// ); /// ``` -pub fn evaluate_with_context(source: &str, context: &mut VariableMap) -> Result { +pub fn evaluate_with_context(source: &str, context: &mut Map) -> Result { let mut parser = Parser::new(); parser.set_language(language()).unwrap(); @@ -57,7 +57,7 @@ pub fn evaluate_with_context(source: &str, context: &mut VariableMap) -> Result< /// abstract trees called [Item][]s that can be run to execute the source code. pub struct Evaluator<'context, 'code> { _parser: Parser, - context: &'context mut VariableMap, + context: &'context mut Map, source: &'code str, syntax_tree: TSTree, } @@ -69,7 +69,7 @@ impl Debug for Evaluator<'_, '_> { } impl<'context, 'code> Evaluator<'context, 'code> { - fn new(mut parser: Parser, context: &'context mut VariableMap, source: &'code str) -> Self { + fn new(mut parser: Parser, context: &'context mut Map, source: &'code str) -> Self { let syntax_tree = parser.parse(source, None).unwrap(); Evaluator { @@ -147,7 +147,7 @@ mod tests { #[test] fn evaluate_map() { - let mut map = VariableMap::new(); + let mut map = Map::new(); map.set_value("x".to_string(), Value::Integer(1)).unwrap(); map.set_value("foo".to_string(), Value::String("bar".to_string())) @@ -243,7 +243,7 @@ mod tests { #[test] fn evaluate_function_call() { - let mut context = VariableMap::new(); + let mut context = Map::new(); assert_eq!( evaluate_with_context( diff --git a/src/lib.rs b/src/lib.rs index 6cbb42d..4df445e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,7 @@ pub use crate::{ abstract_tree::*, error::*, evaluator::*, - value::{ - function::Function, table::Table, value_type::ValueType, variable_map::VariableMap, Value, - }, + value::{function::Function, map::Map, table::Table, value_type::ValueType, Value}, }; mod abstract_tree; diff --git a/src/main.rs b/src/main.rs index 77b5f9e..46d5adc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use rustyline::{ use std::{borrow::Cow, fs::read_to_string}; -use dust_lang::{evaluate, evaluate_with_context, Value, VariableMap}; +use dust_lang::{evaluate, evaluate_with_context, Map, Value}; /// Command-line arguments to be parsed. #[derive(Parser, Debug)] @@ -128,7 +128,7 @@ impl Highlighter for DustReadline { } fn run_cli_shell() { - let mut context = VariableMap::new(); + let mut context = Map::new(); let mut rl: Editor = Editor::new().unwrap(); rl.set_helper(Some(DustReadline::new())); diff --git a/src/value/variable_map.rs b/src/value/map.rs similarity index 88% rename from src/value/variable_map.rs rename to src/value/map.rs index d89e0d5..c64b148 100644 --- a/src/value/variable_map.rs +++ b/src/value/map.rs @@ -6,28 +6,19 @@ use std::{ use crate::{value::Value, Error, Result, Table}; -impl Serialize for VariableMap { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: serde::Serializer, - { - self.variables.serialize(serializer) - } -} - /// A collection dust variables comprised of key-value pairs. /// /// The inner value is a BTreeMap in order to allow VariableMap instances to be sorted and compared /// to one another. #[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Deserialize)] -pub struct VariableMap { +pub struct Map { variables: BTreeMap, } -impl VariableMap { +impl Map { /// Creates a new instace. pub fn new() -> Self { - VariableMap { + Map { variables: BTreeMap::new(), } } @@ -101,7 +92,7 @@ impl VariableMap { }) } } else { - let mut new_map = VariableMap::new(); + let mut new_map = Map::new(); new_map.set_value(next_identifier.to_string(), value)?; @@ -140,13 +131,13 @@ impl VariableMap { } } -impl Default for VariableMap { +impl Default for Map { fn default() -> Self { Self::new() } } -impl Display for VariableMap { +impl Display for Map { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "{{\n")?; for (key, value) in &self.variables { @@ -156,9 +147,9 @@ impl Display for VariableMap { } } -impl From<&Table> for VariableMap { +impl From<&Table> for Map { fn from(value: &Table) -> Self { - let mut map = VariableMap::new(); + let mut map = Map::new(); for (row_index, row) in value.rows().iter().enumerate() { map.set_value(row_index.to_string(), Value::List(row.clone())) @@ -169,13 +160,22 @@ impl From<&Table> for VariableMap { } } +impl Serialize for Map { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + self.variables.serialize(serializer) + } +} + #[cfg(test)] mod tests { use super::*; #[test] fn get_and_set_simple_value() { - let mut map = VariableMap::new(); + let mut map = Map::new(); map.set_value("x".to_string(), Value::Integer(1)).unwrap(); @@ -184,19 +184,19 @@ mod tests { #[test] fn get_and_set_nested_maps() { - let mut map = VariableMap::new(); + let mut map = Map::new(); - map.set_value("x".to_string(), Value::Map(VariableMap::new())) + map.set_value("x".to_string(), Value::Map(Map::new())) .unwrap(); - map.set_value("x.x".to_string(), Value::Map(VariableMap::new())) + map.set_value("x.x".to_string(), Value::Map(Map::new())) .unwrap(); - map.set_value("x.x.x".to_string(), Value::Map(VariableMap::new())) + map.set_value("x.x.x".to_string(), Value::Map(Map::new())) .unwrap(); - map.set_value("x.x.x.x".to_string(), Value::Map(VariableMap::new())) + map.set_value("x.x.x.x".to_string(), Value::Map(Map::new())) .unwrap(); assert_eq!( - Value::Map(VariableMap::new()), + Value::Map(Map::new()), map.get_value("x.x.x.x").unwrap().unwrap() ); } diff --git a/src/value/mod.rs b/src/value/mod.rs index 586bd42..fe867c1 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -1,7 +1,7 @@ //! Types that represent runtime values. use crate::{ error::{Error, Result}, - Function, Table, ValueType, VariableMap, + Function, Map, Table, ValueType, }; use json::JsonValue; @@ -20,9 +20,9 @@ use std::{ }; pub mod function; +pub mod map; pub mod table; pub mod value_type; -pub mod variable_map; /// Whale value representation. /// @@ -32,7 +32,7 @@ pub mod variable_map; #[derive(Debug, Clone, Default)] pub enum Value { List(Vec), - Map(VariableMap), + Map(Map), Table(Table), Function(Function), String(String), @@ -161,7 +161,7 @@ impl Value { } /// Borrows the value stored in `self` as `Vec`, or returns `Err` if `self` is not a `Value::Map`. - pub fn as_map(&self) -> Result<&VariableMap> { + pub fn as_map(&self) -> Result<&Map> { match self { Value::Map(map) => Ok(map), value => Err(Error::ExpectedMap { @@ -498,7 +498,7 @@ impl TryFrom for Value { Number(number) => Ok(Value::Float(f64::from(number))), Boolean(boolean) => Ok(Value::Boolean(boolean)), Object(object) => { - let mut map = VariableMap::new(); + let mut map = Map::new(); for (key, node_value) in object.iter() { let value = Value::try_from(node_value)?; @@ -536,7 +536,7 @@ impl TryFrom<&JsonValue> for Value { Number(number) => Ok(Value::Float(f64::from(*number))), Boolean(boolean) => Ok(Value::Boolean(*boolean)), Object(object) => { - let mut map = VariableMap::new(); + let mut map = Map::new(); for (key, node_value) in object.iter() { let value = Value::try_from(node_value)?; @@ -829,7 +829,7 @@ impl<'de> Visitor<'de> for ValueVisitor { where M: MapAccess<'de>, { - let mut map = VariableMap::new(); + let mut map = Map::new(); while let Some((key, value)) = access.next_entry()? { map.set_value(key, value) diff --git a/src/value/table.rs b/src/value/table.rs index 727f14d..26f70af 100644 --- a/src/value/table.rs +++ b/src/value/table.rs @@ -1,4 +1,4 @@ -use crate::{Error, Result, Value, VariableMap}; +use crate::{Error, Map, Result, Value}; use comfy_table::{Cell, Color, ContentArrangement, Table as ComfyTable}; use serde::{Deserialize, Serialize}; use std::{ @@ -280,8 +280,8 @@ impl From<&mut Vec> for Table { } } -impl From for Table { - fn from(map: VariableMap) -> Self { +impl From for Table { + fn from(map: Map) -> Self { let keys = map.inner().keys().cloned().collect(); let values = map.inner().values().cloned().collect(); let mut table = Table::new(keys); @@ -294,8 +294,8 @@ impl From for Table { } } -impl From<&VariableMap> for Table { - fn from(map: &VariableMap) -> Self { +impl From<&Map> for Table { + fn from(map: &Map) -> Self { let keys = map.inner().keys().cloned().collect(); let values = map.inner().values().cloned().collect(); let mut table = Table::new(keys); @@ -308,8 +308,8 @@ impl From<&VariableMap> for Table { } } -impl From<&mut VariableMap> for Table { - fn from(map: &mut VariableMap) -> Self { +impl From<&mut Map> for Table { + fn from(map: &mut Map) -> Self { let keys = map.inner().keys().cloned().collect(); let values = map.inner().values().cloned().collect(); let mut table = Table::new(keys);