From ff60640ff8d7f631e0e4e359befacbdce970bbb9 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 1 Oct 2023 05:03:59 -0400 Subject: [PATCH] Clean up --- src/interface.rs | 162 ++++++++++++++++++++++-------------------- src/value/function.rs | 6 +- src/value/mod.rs | 20 ++---- 3 files changed, 95 insertions(+), 93 deletions(-) diff --git a/src/interface.rs b/src/interface.rs index 8f1485a..3722feb 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -202,74 +202,13 @@ impl EvaluatorTree for Statement { #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub enum Expression { - Identifier(String), + Identifier(Identifier), Value(Value), ControlFlow(Box), Assignment(Box), Math(Box), } -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct Math { - left: Expression, - operator: MathOperator, - right: Expression, -} - -impl EvaluatorTree for Math { - fn new(node: Node, source: &str) -> Result { - let left_node = node.child(0).unwrap(); - let operator_node = node.child(1).unwrap().child(0).unwrap(); - let right_node = node.child(2).unwrap(); - println!("{left_node:#?}"); - println!("{operator_node:#?}"); - println!("{right_node:#?}"); - let operator = match operator_node.kind() { - "+" => MathOperator::Add, - "-" => MathOperator::Subtract, - "*" => MathOperator::Multiply, - "/" => MathOperator::Divide, - "%" => MathOperator::Modulo, - _ => { - return Err(Error::UnexpectedSyntax { - expected: "+, -, *, / or %", - actual: operator_node.kind(), - location: operator_node.start_position(), - }) - } - }; - - Ok(Math { - left: Expression::new(left_node, source)?, - operator, - right: Expression::new(right_node, source)?, - }) - } - - fn run(&self, context: &mut VariableMap) -> Result { - let left_value = self.left.run(context)?.as_number()?; - let right_value = self.right.run(context)?.as_number()?; - let outcome = match self.operator { - MathOperator::Add => left_value + right_value, - MathOperator::Subtract => left_value - right_value, - MathOperator::Multiply => left_value * right_value, - MathOperator::Divide => left_value / right_value, - MathOperator::Modulo => left_value % right_value, - }; - - Ok(Value::Float(outcome)) - } -} - -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub enum MathOperator { - Add, - Subtract, - Multiply, - Divide, - Modulo, -} - impl EvaluatorTree for Expression { fn new(node: Node, source: &str) -> Result { let node = if node.kind() == "expression" { @@ -279,12 +218,7 @@ impl EvaluatorTree for Expression { }; let expression = match node.kind() { - "identifier" => { - let byte_range = node.byte_range(); - let identifier = &source[byte_range]; - - Self::Identifier(identifier.to_string()) - } + "identifier" => Self::Identifier(Identifier::new(node, source)?), "value" => Expression::Value(Value::new(node, source)?), "control_flow" => Expression::ControlFlow(Box::new(ControlFlow::new(node, source)?)), "assignment" => Expression::Assignment(Box::new(Assignment::new(node, source)?)), @@ -303,15 +237,7 @@ impl EvaluatorTree for Expression { fn run(&self, context: &mut VariableMap) -> Result { match self { - Expression::Identifier(identifier) => { - let value = context.get_value(&identifier)?; - - if let Some(value) = value { - Ok(value) - } else { - Ok(Value::Empty) - } - } + Expression::Identifier(identifier) => identifier.run(context), Expression::Value(value) => Ok(value.clone()), Expression::ControlFlow(control_flow) => control_flow.run(context), Expression::Assignment(_) => todo!(), @@ -320,6 +246,30 @@ impl EvaluatorTree for Expression { } } +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] +pub struct Identifier(String); + +impl Identifier { + pub fn take_inner(self) -> String { + self.0 + } +} + +impl EvaluatorTree for Identifier { + fn new(node: Node, source: &str) -> Result { + let byte_range = node.byte_range(); + let identifier = &source[byte_range]; + + Ok(Identifier(identifier.to_string())) + } + + fn run(&self, context: &mut VariableMap) -> Result { + let value = context.get_value(&self.0)?.unwrap_or_default(); + + Ok(value) + } +} + #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct ControlFlow { if_expression: Expression, @@ -382,6 +332,64 @@ impl Assignment { } } +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] +pub struct Math { + left: Expression, + operator: MathOperator, + right: Expression, +} + +impl EvaluatorTree for Math { + fn new(node: Node, source: &str) -> Result { + let left_node = node.child(0).unwrap(); + let operator_node = node.child(1).unwrap().child(0).unwrap(); + let right_node = node.child(2).unwrap(); + let operator = match operator_node.kind() { + "+" => MathOperator::Add, + "-" => MathOperator::Subtract, + "*" => MathOperator::Multiply, + "/" => MathOperator::Divide, + "%" => MathOperator::Modulo, + _ => { + return Err(Error::UnexpectedSyntax { + expected: "+, -, *, / or %", + actual: operator_node.kind(), + location: operator_node.start_position(), + }) + } + }; + + Ok(Math { + left: Expression::new(left_node, source)?, + operator, + right: Expression::new(right_node, source)?, + }) + } + + fn run(&self, context: &mut VariableMap) -> Result { + let left_value = self.left.run(context)?.as_number()?; + let right_value = self.right.run(context)?.as_number()?; + let outcome = match self.operator { + MathOperator::Add => left_value + right_value, + MathOperator::Subtract => left_value - right_value, + MathOperator::Multiply => left_value * right_value, + MathOperator::Divide => left_value / right_value, + MathOperator::Modulo => left_value % right_value, + }; + + Ok(Value::Float(outcome)) + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] +pub enum MathOperator { + Add, + Subtract, + Multiply, + Divide, + Modulo, +} + #[cfg(test)] mod tests { use crate::Table; diff --git a/src/value/function.rs b/src/value/function.rs index 9183c9d..bb9bb99 100644 --- a/src/value/function.rs +++ b/src/value/function.rs @@ -2,16 +2,16 @@ use std::fmt::{self, Display, Formatter}; use serde::{Deserialize, Serialize}; -use crate::{Result, Statement, Value, VariableMap}; +use crate::{Identifier, Result, Statement, Value, VariableMap}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Function { - identifiers: Vec, + identifiers: Vec, statements: Vec, } impl Function { - pub fn new(identifiers: Vec, statements: Vec) -> Self { + pub fn new(identifiers: Vec, statements: Vec) -> Self { Function { identifiers, statements, diff --git a/src/value/mod.rs b/src/value/mod.rs index ae9c6c1..1f19337 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}, - EvaluatorTree, Expression, Function, Statement, Table, Time, ValueType, VariableMap, + EvaluatorTree, Function, Identifier, Statement, Table, Time, ValueType, VariableMap, }; use json::JsonValue; @@ -108,11 +108,9 @@ impl Value { let child = node.child(index).unwrap(); if child.kind() == "identifier" { - let child_expression = Expression::new(child, source)?; + let identifier = Identifier::new(child, source)?; - if let Expression::Identifier(column_name) = child_expression { - column_names.push(column_name) - } + column_names.push(identifier.take_inner()) } if child.kind() == "list" { @@ -142,11 +140,9 @@ impl Value { let child = node.child(index).unwrap(); if child.kind() == "identifier" { - let child_identifier = Expression::new(child, source)?; + let identifier = Identifier::new(child, source)?; - if let Expression::Identifier(identifier) = child_identifier { - key = identifier - } + key = identifier.take_inner() } if child.kind() == "value" { @@ -167,11 +163,9 @@ impl Value { let child = node.child(index).unwrap(); if child.kind() == "identifier" { - let child_identifier = Expression::new(child, source)?; + let identifier = Identifier::new(child, source)?; - if let Expression::Identifier(identifier) = child_identifier { - identifiers.push(identifier) - } + identifiers.push(identifier) } if child.kind() == "statement" {