-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
44 lines (34 loc) · 1.04 KB
/
Copy pathmain.cpp
File metadata and controls
44 lines (34 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "parser.h"
#include "lexer.h"
#include "differentiator.h"
#include <iostream>
#include <string>
#include <vector>
int main() {
std::cout << "Enter an expression: ";
std::string line;
if (!std::getline(std::cin, line)) {
return 1;
}
std::cout << "Differentiate with respect to: ";
std::string var;
if(!std::getline(std::cin, var)){
return 1;
}
try {
Lexer lexer(line);
std::vector<Token> tokens;
for(Token t = lexer.nextToken(); t.type != TokenType::END; t = lexer.nextToken()){
tokens.push_back(t);
}
Parser parser(tokens);
std::unique_ptr<Expr> ast = parser.parseExpression();
std::cout << "Parsed expression AST: " << printExpr(ast.get()) << "\n";
auto derivative = differentiate(ast.get(), var);
std::cout << "Derivative: " << simplifyAndPrint(derivative.get()) << "\n";
} catch (const std::runtime_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}