Rename VariableMap to Map
This commit is contained in:
parent
3e45c198aa
commit
86499367fc
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let key = self.identifier.inner().clone();
|
||||
let value = self.statement.run(source, context)?;
|
||||
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let statements = &self.statements;
|
||||
|
||||
statements
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
match self {
|
||||
Expression::Value(value_node) => value_node.run(source, context),
|
||||
Expression::Identifier(identifier) => identifier.run(source, context),
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let value = self.expression.run(source, context)?;
|
||||
let list = value.as_list()?;
|
||||
let key = self.identifier.inner();
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let value = self.expression.run(source, context)?;
|
||||
let list = value.as_list()?;
|
||||
let key = self.identifier.inner();
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let value = self.expression.run(source, context)?;
|
||||
let list = value.as_list()?;
|
||||
let key = self.identifier.inner();
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let key = self.name.inner();
|
||||
let definition = if let Some(value) = context.get_value(key)? {
|
||||
value.as_function().cloned()?
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, _source: &str, context: &mut Map) -> Result<Value> {
|
||||
let value = if let Some(value) = context.get_value(&self.0)? {
|
||||
value
|
||||
} else {
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let if_boolean = self.if_expression.run(source, context)?.as_boolean()?;
|
||||
|
||||
if if_boolean {
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
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()?;
|
||||
|
@ -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<Value> {
|
||||
pub fn run_parallel(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let mut prev_result = Ok(Value::Empty);
|
||||
|
||||
for statement in &self.statements {
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let left = self.left.run(source, context)?;
|
||||
let right = self.right.run(source, context)?;
|
||||
let result = match self.operator {
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, _source: &str, _context: &mut Map) -> Result<Value> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let left = self.left.run(source, context)?;
|
||||
let right = self.right.run(source, context)?;
|
||||
let value = match self.operator {
|
||||
|
@ -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<Self>;
|
||||
|
||||
/// 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>;
|
||||
}
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let value = self.expression.run(source, context)?;
|
||||
let mut list = value.into_inner_list()?;
|
||||
let key = self.identifier.inner();
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
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();
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
match self {
|
||||
Statement::Assignment(assignment) => assignment.run(source, context),
|
||||
Statement::Expression(expression) => expression.run(source, context),
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
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))?;
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let value = self.expression.run(source, context)?;
|
||||
let list = value.as_list()?;
|
||||
let key = self.identifier.inner();
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
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)?;
|
||||
|
@ -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<Value> {
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
while self.expression.run(source, context)?.as_boolean()? {
|
||||
for item in &self.items {
|
||||
item.run(source, context)?;
|
||||
|
@ -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<Value> {
|
||||
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<Value> {
|
||||
/// 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();
|
||||
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(
|
||||
|
@ -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;
|
||||
|
@ -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<DustReadline, DefaultHistory> = Editor::new().unwrap();
|
||||
|
||||
rl.set_helper(Some(DustReadline::new()));
|
||||
|
@ -6,28 +6,19 @@ use std::{
|
||||
|
||||
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.
|
||||
///
|
||||
/// 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<String, Value>,
|
||||
}
|
||||
|
||||
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<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
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()
|
||||
);
|
||||
}
|
@ -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<Value>),
|
||||
Map(VariableMap),
|
||||
Map(Map),
|
||||
Table(Table),
|
||||
Function(Function),
|
||||
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`.
|
||||
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<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)?;
|
||||
@ -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)
|
||||
|
@ -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<Value>> 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);
|
||||
@ -294,8 +294,8 @@ impl From<VariableMap> 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);
|
||||
|
Loading…
Reference in New Issue
Block a user