Rename VariableMap to Map

This commit is contained in:
Jeff 2023-10-25 16:44:50 -04:00
parent 3e45c198aa
commit 86499367fc
28 changed files with 98 additions and 102 deletions

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Result, Value, VariableMap}; use crate::{AbstractTree, Error, Map, Result, Value};
use super::{identifier::Identifier, statement::Statement}; use super::{identifier::Identifier, statement::Statement};
@ -49,7 +49,7 @@ impl AbstractTree for Assignment {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let key = self.identifier.inner().clone(); let key = self.identifier.inner().clone();
let value = self.statement.run(source, context)?; let value = self.statement.run(source, context)?;

View File

@ -2,7 +2,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Async { pub struct Async {
@ -37,7 +37,7 @@ impl AbstractTree for Async {
Ok(Async { statements }) Ok(Async { statements })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let statements = &self.statements; let statements = &self.statements;
statements statements

View File

@ -1,9 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ use crate::{value_node::ValueNode, AbstractTree, Error, Identifier, Map, Result, Tool, Value};
value_node::ValueNode, AbstractTree, Error, Identifier, Result, Tool, Value, VariableMap,
};
use super::{function_call::FunctionCall, logic::Logic, math::Math}; 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<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
match self { match self {
Expression::Value(value_node) => value_node.run(source, context), Expression::Value(value_node) => value_node.run(source, context),
Expression::Identifier(identifier) => identifier.run(source, context), Expression::Identifier(identifier) => identifier.run(source, context),

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Filter { pub struct Filter {
@ -28,7 +28,7 @@ impl AbstractTree for Filter {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let value = self.expression.run(source, context)?; let value = self.expression.run(source, context)?;
let list = value.as_list()?; let list = value.as_list()?;
let key = self.identifier.inner(); let key = self.identifier.inner();

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Find { pub struct Find {
@ -28,7 +28,7 @@ impl AbstractTree for Find {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let value = self.expression.run(source, context)?; let value = self.expression.run(source, context)?;
let list = value.as_list()?; let list = value.as_list()?;
let key = self.identifier.inner(); let key = self.identifier.inner();

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct For { pub struct For {
@ -28,7 +28,7 @@ impl AbstractTree for For {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let value = self.expression.run(source, context)?; let value = self.expression.run(source, context)?;
let list = value.as_list()?; let list = value.as_list()?;
let key = self.identifier.inner(); let key = self.identifier.inner();

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Result, Value, VariableMap}; use crate::{AbstractTree, Error, Map, Result, Value};
use super::{expression::Expression, identifier::Identifier}; use super::{expression::Expression, identifier::Identifier};
@ -33,7 +33,7 @@ impl AbstractTree for FunctionCall {
Ok(FunctionCall { name, arguments }) Ok(FunctionCall { name, arguments })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let key = self.name.inner(); let key = self.name.inner();
let definition = if let Some(value) = context.get_value(key)? { let definition = if let Some(value) = context.get_value(key)? {
value.as_function().cloned()? value.as_function().cloned()?

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Identifier(String); pub struct Identifier(String);
@ -29,7 +29,7 @@ impl AbstractTree for Identifier {
Ok(Identifier(identifier.to_string())) Ok(Identifier(identifier.to_string()))
} }
fn run(&self, _source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, _source: &str, context: &mut Map) -> Result<Value> {
let value = if let Some(value) = context.get_value(&self.0)? { let value = if let Some(value) = context.get_value(&self.0)? {
value value
} else { } else {

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct IfElse { pub struct IfElse {
@ -57,7 +57,7 @@ impl AbstractTree for IfElse {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let if_boolean = self.if_expression.run(source, context)?.as_boolean()?; let if_boolean = self.if_expression.run(source, context)?.as_boolean()?;
if if_boolean { if if_boolean {

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Insert { pub struct Insert {
@ -22,7 +22,7 @@ impl AbstractTree for Insert {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let table_name = self.identifier.inner().clone(); let table_name = self.identifier.inner().clone();
let mut table = self.identifier.run(source, context)?.as_table()?.clone(); let mut table = self.identifier.run(source, context)?.as_table()?.clone();
let new_rows = self.expression.run(source, context)?.into_inner_list()?; let new_rows = self.expression.run(source, context)?.into_inner_list()?;

View File

@ -4,7 +4,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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. /// An abstractiton of an independent unit of source code, or a comment.
/// ///
@ -21,7 +21,7 @@ impl Item {
Self { statements } Self { statements }
} }
pub fn run_parallel(&self, source: &str, context: &mut VariableMap) -> Result<Value> { pub fn run_parallel(&self, source: &str, context: &mut Map) -> Result<Value> {
let statements = &self.statements; let statements = &self.statements;
let run_result = statements.into_par_iter().try_for_each(|statement| { let run_result = statements.into_par_iter().try_for_each(|statement| {
let mut context = context.clone(); let mut context = context.clone();
@ -63,7 +63,7 @@ impl AbstractTree for Item {
Ok(Item { statements }) Ok(Item { statements })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let mut prev_result = Ok(Value::Empty); let mut prev_result = Ok(Value::Empty);
for statement in &self.statements { for statement in &self.statements {

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Logic { pub struct Logic {
@ -44,7 +44,7 @@ impl AbstractTree for Logic {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let left = self.left.run(source, context)?; let left = self.left.run(source, context)?;
let right = self.right.run(source, context)?; let right = self.right.run(source, context)?;
let result = match self.operator { let result = match self.operator {

View File

@ -6,7 +6,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Match {} pub struct Match {}
@ -16,7 +16,7 @@ impl AbstractTree for Match {
todo!() todo!()
} }
fn run(&self, _source: &str, _context: &mut VariableMap) -> Result<Value> { fn run(&self, _source: &str, _context: &mut Map) -> Result<Value> {
todo!() todo!()
} }
} }

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Math { pub struct Math {
@ -42,7 +42,7 @@ impl AbstractTree for Math {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let left = self.left.run(source, context)?; let left = self.left.run(source, context)?;
let right = self.right.run(source, context)?; let right = self.right.run(source, context)?;
let value = match self.operator { let value = match self.operator {

View File

@ -36,7 +36,7 @@ pub use {
use tree_sitter::Node; 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 /// This trait is implemented by the Evaluator's internal types to form an
/// executable tree that resolves to a single value. /// 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<Self>; fn from_syntax_node(source: &str, node: Node) -> Result<Self>;
/// Execute dust code by traversing the tree /// Execute dust code by traversing the tree
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value>; fn run(&self, source: &str, context: &mut Map) -> Result<Value>;
} }

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Remove { pub struct Remove {
@ -28,7 +28,7 @@ impl AbstractTree for Remove {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let value = self.expression.run(source, context)?; let value = self.expression.run(source, context)?;
let mut list = value.into_inner_list()?; let mut list = value.into_inner_list()?;
let key = self.identifier.inner(); let key = self.identifier.inner();

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Select { pub struct Select {
@ -53,7 +53,7 @@ impl AbstractTree for Select {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let value = self.expression.run(source, context)?; let value = self.expression.run(source, context)?;
let old_table = value.as_table()?; let old_table = value.as_table()?;
let column_names = if self.identifiers.len() > 0 { let column_names = if self.identifiers.len() > 0 {
@ -70,7 +70,7 @@ impl AbstractTree for Select {
for row in old_table.rows() { for row in old_table.rows() {
let mut new_row = Vec::new(); 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() { for (i, value) in row.iter().enumerate() {
let column_name = old_table.headers().get(i).unwrap(); let column_name = old_table.headers().get(i).unwrap();

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, Filter, Find, For, IfElse, Insert, Match, AbstractTree, Assignment, Async, Error, Expression, Filter, Find, For, IfElse, Insert, Map,
Remove, Result, Select, Transform, Value, VariableMap, While, Match, Remove, Result, Select, Transform, Value, While,
}; };
/// Abstract representation of a statement. /// Abstract representation of a statement.
@ -82,7 +82,7 @@ impl AbstractTree for Statement {
} }
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
match self { match self {
Statement::Assignment(assignment) => assignment.run(source, context), Statement::Assignment(assignment) => assignment.run(source, context),
Statement::Expression(expression) => expression.run(source, context), Statement::Expression(expression) => expression.run(source, context),

View File

@ -11,7 +11,7 @@ use reqwest::blocking::get;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum Tool { pub enum Tool {
@ -243,7 +243,7 @@ impl AbstractTree for Tool {
Ok(tool) Ok(tool)
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
match self { match self {
Tool::Assert(expressions) => { Tool::Assert(expressions) => {
for expression in expressions { for expression in expressions {
@ -364,7 +364,7 @@ impl AbstractTree for Tool {
let created = metadata.created()?.elapsed()?.as_secs() as i64; let created = metadata.created()?.elapsed()?.as_secs() as i64;
let modified = metadata.modified()?.elapsed()?.as_secs() as i64; let modified = metadata.modified()?.elapsed()?.as_secs() as i64;
let accessed = metadata.accessed()?.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("type".to_string(), Value::String(file_type))?;
metadata_output.set_value("size".to_string(), Value::Integer(size))?; metadata_output.set_value("size".to_string(), Value::Integer(size))?;

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Transform { pub struct Transform {
@ -28,7 +28,7 @@ impl AbstractTree for Transform {
}) })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let value = self.expression.run(source, context)?; let value = self.expression.run(source, context)?;
let list = value.as_list()?; let list = value.as_list()?;
let key = self.identifier.inner(); let key = self.identifier.inner();

View File

@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Error, Expression, Function, Identifier, Item, Result, Table, Value, ValueType, AbstractTree, Error, Expression, Function, Identifier, Item, Map, Result, Table, Value,
VariableMap, ValueType,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[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<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let value_source = &source[self.byte_range()]; let value_source = &source[self.byte_range()];
let value = match &self.value_type { let value = match &self.value_type {
ValueType::Any => todo!(), ValueType::Any => todo!(),
@ -161,7 +161,7 @@ impl AbstractTree for ValueNode {
} }
ValueType::Empty => Value::Empty, ValueType::Empty => Value::Empty,
ValueType::Map(nodes) => { ValueType::Map(nodes) => {
let mut values = VariableMap::new(); let mut values = Map::new();
for (key, node) in nodes { for (key, node) in nodes {
let value = node.run(source, context)?; let value = node.run(source, context)?;

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct While { pub struct While {
@ -29,7 +29,7 @@ impl AbstractTree for While {
Ok(While { expression, items }) Ok(While { expression, items })
} }
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> { fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
while self.expression.run(source, context)?.as_boolean()? { while self.expression.run(source, context)?.as_boolean()? {
for item in &self.items { for item in &self.items {
item.run(source, context)?; item.run(source, context)?;

View File

@ -6,7 +6,7 @@ use std::fmt::{self, Debug, Formatter};
use tree_sitter::{Parser, Tree as TSTree}; 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. /// 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))); /// assert_eq!(evaluate("1 + 2 + 3"), Ok(Value::Integer(6)));
/// ``` /// ```
pub fn evaluate(source: &str) -> Result<Value> { pub fn evaluate(source: &str) -> Result<Value> {
let mut context = VariableMap::new(); let mut context = Map::new();
evaluate_with_context(source, &mut context) evaluate_with_context(source, &mut context)
} }
@ -44,7 +44,7 @@ pub fn evaluate(source: &str) -> Result<Value> {
/// Ok(Value::Integer(10)) /// Ok(Value::Integer(10))
/// ); /// );
/// ``` /// ```
pub fn evaluate_with_context(source: &str, context: &mut VariableMap) -> Result<Value> { pub fn evaluate_with_context(source: &str, context: &mut Map) -> Result<Value> {
let mut parser = Parser::new(); let mut parser = Parser::new();
parser.set_language(language()).unwrap(); 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. /// abstract trees called [Item][]s that can be run to execute the source code.
pub struct Evaluator<'context, 'code> { pub struct Evaluator<'context, 'code> {
_parser: Parser, _parser: Parser,
context: &'context mut VariableMap, context: &'context mut Map,
source: &'code str, source: &'code str,
syntax_tree: TSTree, syntax_tree: TSTree,
} }
@ -69,7 +69,7 @@ impl Debug for Evaluator<'_, '_> {
} }
impl<'context, 'code> Evaluator<'context, 'code> { 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(); let syntax_tree = parser.parse(source, None).unwrap();
Evaluator { Evaluator {
@ -147,7 +147,7 @@ mod tests {
#[test] #[test]
fn evaluate_map() { 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("x".to_string(), Value::Integer(1)).unwrap();
map.set_value("foo".to_string(), Value::String("bar".to_string())) map.set_value("foo".to_string(), Value::String("bar".to_string()))
@ -243,7 +243,7 @@ mod tests {
#[test] #[test]
fn evaluate_function_call() { fn evaluate_function_call() {
let mut context = VariableMap::new(); let mut context = Map::new();
assert_eq!( assert_eq!(
evaluate_with_context( evaluate_with_context(

View File

@ -8,9 +8,7 @@ pub use crate::{
abstract_tree::*, abstract_tree::*,
error::*, error::*,
evaluator::*, evaluator::*,
value::{ value::{function::Function, map::Map, table::Table, value_type::ValueType, Value},
function::Function, table::Table, value_type::ValueType, variable_map::VariableMap, Value,
},
}; };
mod abstract_tree; mod abstract_tree;

View File

@ -11,7 +11,7 @@ use rustyline::{
use std::{borrow::Cow, fs::read_to_string}; 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. /// Command-line arguments to be parsed.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -128,7 +128,7 @@ impl Highlighter for DustReadline {
} }
fn run_cli_shell() { fn run_cli_shell() {
let mut context = VariableMap::new(); let mut context = Map::new();
let mut rl: Editor<DustReadline, DefaultHistory> = Editor::new().unwrap(); let mut rl: Editor<DustReadline, DefaultHistory> = Editor::new().unwrap();
rl.set_helper(Some(DustReadline::new())); rl.set_helper(Some(DustReadline::new()));

View File

@ -6,28 +6,19 @@ use std::{
use crate::{value::Value, Error, Result, Table}; use crate::{value::Value, Error, Result, Table};
impl Serialize for VariableMap {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.variables.serialize(serializer)
}
}
/// A collection dust variables comprised of key-value pairs. /// 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 /// The inner value is a BTreeMap in order to allow VariableMap instances to be sorted and compared
/// to one another. /// to one another.
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Deserialize)] #[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Deserialize)]
pub struct VariableMap { pub struct Map {
variables: BTreeMap<String, Value>, variables: BTreeMap<String, Value>,
} }
impl VariableMap { impl Map {
/// Creates a new instace. /// Creates a new instace.
pub fn new() -> Self { pub fn new() -> Self {
VariableMap { Map {
variables: BTreeMap::new(), variables: BTreeMap::new(),
} }
} }
@ -101,7 +92,7 @@ impl VariableMap {
}) })
} }
} else { } else {
let mut new_map = VariableMap::new(); let mut new_map = Map::new();
new_map.set_value(next_identifier.to_string(), value)?; 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 { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl Display for VariableMap { impl Display for Map {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{{\n")?; write!(f, "{{\n")?;
for (key, value) in &self.variables { 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 { fn from(value: &Table) -> Self {
let mut map = VariableMap::new(); let mut map = Map::new();
for (row_index, row) in value.rows().iter().enumerate() { for (row_index, row) in value.rows().iter().enumerate() {
map.set_value(row_index.to_string(), Value::List(row.clone())) 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<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.variables.serialize(serializer)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn get_and_set_simple_value() { 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(); map.set_value("x".to_string(), Value::Integer(1)).unwrap();
@ -184,19 +184,19 @@ mod tests {
#[test] #[test]
fn get_and_set_nested_maps() { 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(); .unwrap();
map.set_value("x.x".to_string(), Value::Map(VariableMap::new())) map.set_value("x.x".to_string(), Value::Map(Map::new()))
.unwrap(); .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(); .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(); .unwrap();
assert_eq!( assert_eq!(
Value::Map(VariableMap::new()), Value::Map(Map::new()),
map.get_value("x.x.x.x").unwrap().unwrap() map.get_value("x.x.x.x").unwrap().unwrap()
); );
} }

View File

@ -1,7 +1,7 @@
//! Types that represent runtime values. //! Types that represent runtime values.
use crate::{ use crate::{
error::{Error, Result}, error::{Error, Result},
Function, Table, ValueType, VariableMap, Function, Map, Table, ValueType,
}; };
use json::JsonValue; use json::JsonValue;
@ -20,9 +20,9 @@ use std::{
}; };
pub mod function; pub mod function;
pub mod map;
pub mod table; pub mod table;
pub mod value_type; pub mod value_type;
pub mod variable_map;
/// Whale value representation. /// Whale value representation.
/// ///
@ -32,7 +32,7 @@ pub mod variable_map;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub enum Value { pub enum Value {
List(Vec<Value>), List(Vec<Value>),
Map(VariableMap), Map(Map),
Table(Table), Table(Table),
Function(Function), Function(Function),
String(String), String(String),
@ -161,7 +161,7 @@ impl Value {
} }
/// Borrows the value stored in `self` as `Vec<Value>`, or returns `Err` if `self` is not a `Value::Map`. /// Borrows the value stored in `self` as `Vec<Value>`, 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 { match self {
Value::Map(map) => Ok(map), Value::Map(map) => Ok(map),
value => Err(Error::ExpectedMap { value => Err(Error::ExpectedMap {
@ -498,7 +498,7 @@ impl TryFrom<JsonValue> for Value {
Number(number) => Ok(Value::Float(f64::from(number))), Number(number) => Ok(Value::Float(f64::from(number))),
Boolean(boolean) => Ok(Value::Boolean(boolean)), Boolean(boolean) => Ok(Value::Boolean(boolean)),
Object(object) => { Object(object) => {
let mut map = VariableMap::new(); let mut map = Map::new();
for (key, node_value) in object.iter() { for (key, node_value) in object.iter() {
let value = Value::try_from(node_value)?; let value = Value::try_from(node_value)?;
@ -536,7 +536,7 @@ impl TryFrom<&JsonValue> for Value {
Number(number) => Ok(Value::Float(f64::from(*number))), Number(number) => Ok(Value::Float(f64::from(*number))),
Boolean(boolean) => Ok(Value::Boolean(*boolean)), Boolean(boolean) => Ok(Value::Boolean(*boolean)),
Object(object) => { Object(object) => {
let mut map = VariableMap::new(); let mut map = Map::new();
for (key, node_value) in object.iter() { for (key, node_value) in object.iter() {
let value = Value::try_from(node_value)?; let value = Value::try_from(node_value)?;
@ -829,7 +829,7 @@ impl<'de> Visitor<'de> for ValueVisitor {
where where
M: MapAccess<'de>, M: MapAccess<'de>,
{ {
let mut map = VariableMap::new(); let mut map = Map::new();
while let Some((key, value)) = access.next_entry()? { while let Some((key, value)) = access.next_entry()? {
map.set_value(key, value) map.set_value(key, value)

View File

@ -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 comfy_table::{Cell, Color, ContentArrangement, Table as ComfyTable};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
@ -280,8 +280,8 @@ impl From<&mut Vec<Value>> for Table {
} }
} }
impl From<VariableMap> for Table { impl From<Map> for Table {
fn from(map: VariableMap) -> Self { fn from(map: Map) -> Self {
let keys = map.inner().keys().cloned().collect(); let keys = map.inner().keys().cloned().collect();
let values = map.inner().values().cloned().collect(); let values = map.inner().values().cloned().collect();
let mut table = Table::new(keys); let mut table = Table::new(keys);
@ -294,8 +294,8 @@ impl From<VariableMap> for Table {
} }
} }
impl From<&VariableMap> for Table { impl From<&Map> for Table {
fn from(map: &VariableMap) -> Self { fn from(map: &Map) -> Self {
let keys = map.inner().keys().cloned().collect(); let keys = map.inner().keys().cloned().collect();
let values = map.inner().values().cloned().collect(); let values = map.inner().values().cloned().collect();
let mut table = Table::new(keys); let mut table = Table::new(keys);
@ -308,8 +308,8 @@ impl From<&VariableMap> for Table {
} }
} }
impl From<&mut VariableMap> for Table { impl From<&mut Map> for Table {
fn from(map: &mut VariableMap) -> Self { fn from(map: &mut Map) -> Self {
let keys = map.inner().keys().cloned().collect(); let keys = map.inner().keys().cloned().collect();
let values = map.inner().values().cloned().collect(); let values = map.inner().values().cloned().collect();
let mut table = Table::new(keys); let mut table = Table::new(keys);