forked from olonho/mathvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
92 lines (80 loc) · 2.45 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "include/mathvm.h"
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <iostream>
using namespace mathvm;
using namespace std;
int main(int argc, char** argv) {
string impl = "";
const char* script = NULL;
for (int32_t i = 1; i < argc; i++) {
if (string(argv[i]) == "-j") {
impl = "jit";
} if (string(argv[i]) == "-p") {
impl = "printer";
} else {
script = argv[i];
}
}
Translator* translator = Translator::create(impl);
if (translator == 0) {
cout << "TODO: Implement translator factory in translator.cpp!!!!" << endl;
return 1;
}
const char* expr = "double x; double y;"
"x += 8.0; y = 2.0;"
"print('Hello, x=',x,' y=',y,'\n');";
bool isDefaultExpr = true;
if (script != NULL) {
expr = loadFile(script);
if (expr == 0) {
printf("Cannot read file: %s\n", script);
return 1;
}
isDefaultExpr = false;
}
Code* code = 0;
Status* translateStatus = translator->translate(expr, &code);
if (translateStatus->isError()) {
uint32_t position = translateStatus->getPosition();
uint32_t line = 0, offset = 0;
positionToLineOffset(expr, position, line, offset);
printf("Cannot translate expression: expression at %d,%d; "
"error '%s'\n",
line, offset,
translateStatus->getErrorCstr());
} else {
if (impl != "printer") {
assert(code != 0);
vector<Var*> vars;
if (isDefaultExpr) {
Var* xVar = new Var(VT_DOUBLE, "x");
Var* yVar = new Var(VT_DOUBLE, "y");
vars.push_back(xVar);
vars.push_back(yVar);
xVar->setDoubleValue(42.0);
}
Status* execStatus = code->execute(vars);
if (execStatus->isError()) {
printf("Cannot execute expression: error: %s\n",
execStatus->getErrorCstr());
} else {
if (isDefaultExpr) {
printf("x evaluated to %f\n", vars[0]->getDoubleValue());
for (uint32_t i = 0; i < vars.size(); i++) {
delete vars[i];
}
}
}
delete code;
delete execStatus;
}
}
delete translateStatus;
delete translator;
if (!isDefaultExpr) {
delete [] expr;
}
return 0;
}