aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-01-17 23:28:48 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2024-01-17 23:28:48 +0100
commit9cc61497ed8a2336f33407d3262181e4ac3b46cb (patch)
treeaac75b57b0fffea64abcd23cbac4d27c875fee48 /src/main.rs
parent77cf9aa7535a1d9481f0bd3caeea26e2b85c5019 (diff)
add commonsense and expression_functions
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs67
1 files changed, 9 insertions, 58 deletions
diff --git a/src/main.rs b/src/main.rs
index d1e914d..466ad09 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,8 @@ pub mod expression;
pub mod function;
pub mod operation;
pub mod string;
+pub mod commonsense;
+pub mod expression_function;
use std::collections::HashMap;
@@ -12,72 +14,21 @@ use context::Context;
use expression::Expression;
use function::Function;
use operation::Operation;
-
-fn add(a: Complex, b: Complex) -> Complex {
- a + b
-}
-
-fn sub(a: Complex, b: Complex) -> Complex {
- a - b
-}
-
-fn mul(a: Complex, b: Complex) -> Complex {
- a * b
-}
-
-fn div(a: Complex, b: Complex) -> Complex {
- a / b
-}
-
-fn pow(a: Complex, b: Complex) -> Complex {
- a.pow(b)
-}
-
-fn sqrt(a: Complex) -> Complex {
- a.sqrt()
-}
-
-fn sin(a: Complex) -> Complex {
- a.sin()
-}
-
-fn cos(a: Complex) -> Complex {
- a.cos()
-}
-
-fn tan(a: Complex) -> Complex {
- a.tan()
-}
+use expression_function::ExpressionFunction;
fn main() {
- let expr = "cos(3)";
+ let func = ExpressionFunction::from_string("f(x) = x^2".to_string());
+
+ let expr = "f(3)";
let ctx: Context = Context::new()
- .with_operations(operations![{
- '+' => &add,
- '-' => &sub,
- '*' => &mul,
- '/' => &div,
- '^' => &pow
- }])
- .with_functions(functions!({"sqrt" => &sqrt, "sin" => &sin, "cos" => &cos, "tan" => &tan}))
- .with_variables(variables!({"x" => Complex::new(5.0, 0.0)}));
+ .with_operations(commonsense_operations!{})
+ .with_functions(commonsense_functions!{func.name() => func})
+ .with_variables(commonsense_variables!{});
let value = Expression::from_string(expr);
match value.evaluate(&ctx) {
Ok(res) => println!("{} = {}", expr, res),
Err(err) => println!("Error: {}", err),
}
-
- let value = Expression::from_string("sin(3)");
- match value.evaluate(&ctx) {
- Ok(res) => println!("{} = {}", expr, res),
- Err(err) => println!("Error: {}", err),
- }
-
- let value = Expression::from_string("tan(3)");
- match value.evaluate(&ctx) {
- Ok(res) => println!("{} = {}", expr, res),
- Err(err) => println!("Error: {}", err),
- }
}