@@ -16,7 +16,8 @@ https://github.com/rudlorenz/mathParser
1616```
1717
1818To use Expressions in your project add src folder with ` add_subdirectory() ` command to your ` CmakeLists.txt ` file and
19- include ` Expressions.h ` and ` Parser.h ` in source files.
19+ include ` Expression.h ` and ` Parser.h ` in source files.
20+ Cmake will automatically fetch gtest and fmt library.
2021
2122Alternatively you can use supplied ` main.cpp ` .
2223
@@ -26,51 +27,76 @@ Parsing expression from string:
2627
2728``` c++
2829#include < Parser.h>
30+ #include < fmt/core.h>
2931
30- auto result = parser::parse(" x + y" );
31- if (result != nullptr ) {
32- std::cout << "result : " << result->to_string();
32+ std::optional<mexpr::Expression> result = parser::parse(" x + y" );
33+ if (!result.has_value()) {
34+ fmt::print (stderr, "Something wrong!");
35+ return 0;
3336}
37+
38+ fmt::print (stdout, "input : {}\n", result->to_string());
39+ fmt::print(stdout, "as expressions : {}\n", result->to_expr_string());
3440```
3541
3642Parse expression from string and find derivative:
3743
3844```c++
3945#include <Parser.h>
4046
41- auto result = parser::parse(" x * x + 2*x + 10" );
42- if (result != nullptr )
43- {
44- auto derivative = result->diff("x");
45- std::cout << "expression : " << result->to_string() << "\n"
46- << "derivative : " << derivative->to_string();
47+ std::optional<mexpr::Expression> result = parser::parse("x * x + 2*x + 10");
48+ if (!result.has_value()) {
49+ fmt::print(stderr, "Something wrong!");
50+ return 0;
4751}
52+
53+ fmt::print(stdout, "input : {}\n", input);
54+ fmt::print(stdout, "as expressions : {}\n", result->to_expr_string());
55+ fmt::print(stdout, "d/dx : {}\n", result->diff("x").to_string());
4856```
4957
5058Calls are chainable:
5159
5260``` c++
5361auto result = parser::parse(" x*x*y + x*y*y" );
54- auto as_string = result != nullptr
55- ? result->diff (x)->diff(y)->to_string();
62+ auto as_string = result
63+ ? result->diff (x).diff(y).to_string();
64+ : "empty";
5665```
5766
5867You can create expressions directly, but it's highly discouraged:
5968
6069```c++
6170#include "Expressions.h"
62- // x * x + y
63- auto var_x = std::make_shared<Variable>("x");
64- auto var_y = std::make_shared<Variable>("y");
65- auto result = std::make_shared<Sum>(
66- std::make_shared<Mul>(var_x, var_x),
67- var_y
68- );
71+ using namespace mexpr;
72+
73+ // x * x + y
74+ auto var_x = Expression{"x"};
75+ auto var_y = Expression{"y"};
76+ auto two_expr = Expression{2};
77+ auto var_x_squared = Expression{
78+ binary_expr_type::mul,
79+ var_x.clone(),
80+ var_x.clone(),
81+ }
82+ auto result = Expression{
83+ binary_expr_type::sub,
84+ Expression{
85+ binary_expr_type::sum,
86+ std::move(var_x_squared),
87+ std::move(var_y)
88+ },
89+ std::move(two_expr)
90+ }
6991```
7092
7193## TODO
7294
73951 . Unit testing.
74- 2 . Proper lexical and synax analysis i.e. input expression validation. Right now it works "garbage in --> garbage out".
96+ 2 . ~~ Proper lexical and syntax analysis i.e. input expression validation. Right now it works "garbage in --> garbage out".~~
75973 . Expression simplification.
76- 4 . Proper parser errors.
98+ 4 . ~~ Proper parser errors.~~
99+ 5 . More unit tests.
100+ 6 . POW operation.
101+ 7 . Check for proper "unary" function parsing.
102+ 8 . Expression evaluation.
0 commit comments