-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
56 lines (48 loc) · 1.59 KB
/
main.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <cmath>
#include "AbstractSyntaxTree.h"
#include "Lexer.h"
#include "Parser.h"
using namespace std;
int main() {
try {
// built-ins
functions["max"] = [](const std::vector<double> &args) {
if (args.size() != 2) {
throw std::runtime_error("max() function expects 2 arguments");
}
return std::max(args[0], args[1]);
};
functions["min"] = [](const std::vector<double> &args) {
if (args.size() != 2) {
throw std::runtime_error("min() function expects 2 arguments");
}
return std::min(args[0], args[1]);
};
functions["pow"] = [](const std::vector<double> &args) {
if (args.size() != 2) {
throw std::runtime_error("pow() function expects 2 arguments");
}
return std::pow(args[0], args[1]);
};
functions["abs"] = [](const std::vector<double> &args) {
if (args.size() != 1) {
throw std::runtime_error("abs() function expects 1 argument");
}
return std::abs(args[0]);
};
std::string input;
while (std::getline(std::cin, input)) {
Lexer lexer(input);
Parser parser(lexer);
ExpressionPtr tree = parser.parse();
double result = tree->evaluate();
std::cout << result << std::endl;
}
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
};