From c980e490145c53c35f05927afa4bb79f614026e0 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 20 Mar 2019 16:57:21 +0200 Subject: [PATCH] Add shortcut functions to evaluate `Node` into value types + Add functions `Node::eval_[type]` and `Node::eval_[type]_with_configuration` to evaluate a node directly into a value type Partially implements #15 --- src/tree/mod.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 0a9baf9..5c73845 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -1,6 +1,9 @@ use crate::{configuration::Configuration, error::Error, operator::*, value::Value}; use token::Token; +use value::TupleType; use EmptyConfiguration; +use FloatType; +use IntType; mod display; @@ -57,6 +60,111 @@ impl Node { self.eval_with_configuration(&EmptyConfiguration) } + /// Evaluates the operator tree rooted at this node into a string with an the given configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_string_with_configuration( + &self, + configuration: &Configuration, + ) -> Result { + match self.eval_with_configuration(configuration) { + Ok(Value::String(string)) => Ok(string), + Ok(value) => Err(Error::expected_string(value)), + Err(error) => Err(error), + } + } + + /// Evaluates the operator tree rooted at this node into a float with an the given configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_float_with_configuration( + &self, + configuration: &Configuration, + ) -> Result { + match self.eval_with_configuration(configuration) { + Ok(Value::Float(float)) => Ok(float), + Ok(value) => Err(Error::expected_float(value)), + Err(error) => Err(error), + } + } + + /// Evaluates the operator tree rooted at this node into an integer with an the given configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_int_with_configuration( + &self, + configuration: &Configuration, + ) -> Result { + match self.eval_with_configuration(configuration) { + Ok(Value::Int(int)) => Ok(int), + Ok(value) => Err(Error::expected_int(value)), + Err(error) => Err(error), + } + } + + /// Evaluates the operator tree rooted at this node into a boolean with an the given configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_boolean_with_configuration( + &self, + configuration: &Configuration, + ) -> Result { + match self.eval_with_configuration(configuration) { + Ok(Value::Boolean(boolean)) => Ok(boolean), + Ok(value) => Err(Error::expected_boolean(value)), + Err(error) => Err(error), + } + } + + /// Evaluates the operator tree rooted at this node into a tuple with an the given configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_tuple_with_configuration( + &self, + configuration: &Configuration, + ) -> Result { + match self.eval_with_configuration(configuration) { + Ok(Value::Tuple(tuple)) => Ok(tuple), + Ok(value) => Err(Error::expected_tuple(value)), + Err(error) => Err(error), + } + } + + /// Evaluates the operator tree rooted at this node into a string with an empty configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_string(&self) -> Result { + self.eval_string_with_configuration(&EmptyConfiguration) + } + + /// Evaluates the operator tree rooted at this node into a float with an empty configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_float(&self) -> Result { + self.eval_float_with_configuration(&EmptyConfiguration) + } + + /// Evaluates the operator tree rooted at this node into an integer with an empty configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_int(&self) -> Result { + self.eval_int_with_configuration(&EmptyConfiguration) + } + + /// Evaluates the operator tree rooted at this node into a boolean with an empty configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_boolean(&self) -> Result { + self.eval_boolean_with_configuration(&EmptyConfiguration) + } + + /// Evaluates the operator tree rooted at this node into a tuple with an empty configuration. + /// + /// Fails, if one of the operators in the expression tree fails. + pub fn eval_tuple(&self) -> Result { + self.eval_tuple_with_configuration(&EmptyConfiguration) + } + fn children(&self) -> &[Node] { &self.children }