forked from rift-lecture/rift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
102 lines (89 loc) · 2.7 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
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <chrono>
#include "llvm.h"
#include "ast.h"
#include "parser.h"
#include "runtime.h"
#include "compiler.h"
#include "tests.h"
#include "rift.h"
#include "gc.h"
using namespace std;
using namespace llvm;
using namespace rift;
extern double eval_time;
void interactive() {
cout << "rift console - type exit to quit" << endl;
Environment * env = new Environment(nullptr);
while (not cin.eof()) {
try {
cout << "> ";
std::string in;
while (true) {
std::string i;
getline(cin, i);
if (i.empty()) break;
if (i.at(i.length() - 1) == '\\') {
in.append(i.substr(0, i.length() - 1));
in.append("\n");
} else {
in.append(i);
break;
}
}
if (in == "exit")
break;
if (in.empty())
continue;
in = in + "\n";
auto start = chrono::high_resolution_clock::now();
RVal * x = eval(env, in.c_str());
auto t = chrono::high_resolution_clock::now() - start;
cout << *x << endl;
double total_t = static_cast<double>(t.count()) / chrono::high_resolution_clock::period::den;
cout << "Evaluation: " << eval_time << "[s]" << endl;
cout << "Evaluation & compilation: " << total_t << endl;
cout << "Compilation: " << (1 - eval_time/total_t)*100 << "[%]" << endl;
} catch (char const * error) {
std::cerr << error << std::endl;
std::cout << std::endl;
}
}
}
void runScript(char const * filename) {
std::ifstream s(filename);
if (not s.is_open()) {
std::cerr << "Unable to open file " << filename << endl;
} else {
Parser p;
ast::Fun * x = new ast::Fun(p.parse(s));
Environment * env = new Environment(nullptr);
compile(x)(env)->print(cout);
}
}
bool DEBUG = false;
int main(int argc, char * argv[]) {
// initialize the JIT
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMInitializeNativeAsmParser();
// force GC to be initialized:
new Environment(nullptr);
int argPos = 1;
if (argc > argPos) {
if (0 == strncmp("-d", argv[argPos], 2)) {
DEBUG = true;
argPos++;
}
}
if (argc == argPos) {
tests();
interactive();
} else {
if (argc > argPos+1)
cerr << "Only one script can be loaded at a time" << endl;
else
runScript(argv[argPos]);
}
}