From b3a616c39aa82fbe51ea2409dc8966e7e4647e81 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Fri, 15 Mar 2019 20:26:25 +0200 Subject: [PATCH] Working on implementing functions --- src/configuration/mod.rs | 35 +++++++++++++++++++++++++++++++++-- src/function.rs | 7 +++++++ src/lib.rs | 13 +++++++------ src/operator/mod.rs | 12 +++++++----- 4 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 src/function.rs diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index 5659161..048c190 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -1,8 +1,11 @@ use crate::{error::Error, value::Value}; use std::collections::HashMap; +use function::Function; pub trait Configuration { fn get_value(&self, identifier: &str) -> Option<&Value>; + + fn get_function(&self, identifier: &str) -> Option<&Function>; } pub struct EmptyConfiguration; @@ -11,12 +14,40 @@ impl Configuration for EmptyConfiguration { fn get_value(&self, identifier: &str) -> Option<&Value> { None } + + fn get_function(&self, identifier: &str) -> Option<&Function> { + None + } } -pub type HashMapConfiguration = HashMap; +pub struct HashMapConfiguration { + variables: HashMap, + functions: HashMap, +} + +impl HashMapConfiguration { + pub fn new() -> Self { + Self { + variables: Default::default(), + functions: Default::default(), + } + } + + pub fn insert_variable(&mut self, identifier: String, value: Value) { + self.variables.insert(identifier, value); + } + + pub fn insert_function(&mut self, identifier: String, function: Function) { + self.functions.insert(identifier, function); + } +} impl Configuration for HashMapConfiguration { fn get_value(&self, identifier: &str) -> Option<&Value> { - self.get(identifier) + self.variables.get(identifier) + } + + fn get_function(&self, identifier: &str) -> Option<&Function> { + self.functions.get(identifier) } } \ No newline at end of file diff --git a/src/function.rs b/src/function.rs new file mode 100644 index 0000000..8af8061 --- /dev/null +++ b/src/function.rs @@ -0,0 +1,7 @@ +use error::Error; +use value::Value; + +pub struct Function { + parameter_amount: usize, + function: fn() -> Result, // TODO continue type +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 9410c6a..523c636 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ mod operator; mod token; mod tree; mod value; +mod function; pub fn eval(string: &str) -> Result { tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(&EmptyConfiguration) @@ -109,12 +110,12 @@ mod test { #[test] fn test_with_configuration() { let mut configuration = HashMapConfiguration::new(); - configuration.insert("tr".to_string(), Value::Boolean(true)); - configuration.insert("fa".to_string(), Value::Boolean(false)); - configuration.insert("five".to_string(), Value::Int(5)); - configuration.insert("six".to_string(), Value::Int(6)); - configuration.insert("half".to_string(), Value::Float(0.5)); - configuration.insert("zero".to_string(), Value::Int(0)); + configuration.insert_variable("tr".to_string(), Value::Boolean(true)); + configuration.insert_variable("fa".to_string(), Value::Boolean(false)); + configuration.insert_variable("five".to_string(), Value::Int(5)); + configuration.insert_variable("six".to_string(), Value::Int(6)); + configuration.insert_variable("half".to_string(), Value::Float(0.5)); + configuration.insert_variable("zero".to_string(), Value::Int(0)); assert_eq!(eval_with_configuration("tr", &configuration), Ok(Value::Boolean(true))); assert_eq!(eval_with_configuration("fa", &configuration), Ok(Value::Boolean(false))); diff --git a/src/operator/mod.rs b/src/operator/mod.rs index e5e774c..bc70c9e 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -477,12 +477,14 @@ impl Operator for Identifier { } fn eval(&self, arguments: &[Value], configuration: &Configuration) -> Result { - expect_argument_amount(arguments.len(), 0)?; - - if let Some(value) = configuration.get_value(&self.identifier).cloned() { - Ok(value) + if arguments.len() == 0 { + if let Some(value) = configuration.get_value(&self.identifier).cloned() { + Ok(value) + } else { + Err(Error::IdentifierNotFound(self.identifier.clone())) + } } else { - Err(Error::IdentifierNotFound(self.identifier.clone())) + unimplemented!() } } }