-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.cpp
66 lines (58 loc) · 1.96 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
#include <iostream>
#include <memory>
#include <unistd.h>
#include "./src/core.h"
#include "./src/fs.h"
#include "./src/parser2.h"
#include "./src/checker/vm2.h"
#include "./src/checker/module2.h"
#include "./src/checker/debug.h"
#include "./src/checker/compiler.h"
using namespace tr;
void run(const string &bytecode, const string &code, const string &fileName) {
ZoneScoped;
auto module = std::make_shared<vm2::Module>(bytecode, fileName, code);
bench(1, [&]{
vm2::run(module);
module->printErrors();
});
}
void compileAndRun(const string &code, const string &file, const string &fileName) {
ZoneScoped;
auto bytecodePath = file + ".tsb";
auto buffer = fileRead(file);
checker::Compiler compiler;
Parser parser;
auto result = parser.parseSourceFile(file, buffer, types::ScriptTarget::Latest, false, ScriptKind::TS, {});
auto program = compiler.compileSourceFile(result);
auto bin = program.build();
fileWrite(bytecodePath, bin);
std::filesystem::last_write_time(bytecodePath, std::filesystem::last_write_time(file));
checker::printBin(bin);
auto module = make_shared<vm2::Module>(bin, fileName, code);
vm2::run(module);
module->printErrors();
}
int main(int argc, char *argv[]) {
ZoneScoped;
std::string file;
auto cwd = std::filesystem::current_path();
if (argc > 1) {
file = cwd.string() + "/" + argv[1];
} else {
file = cwd.string() + "/../tests/basic1.ts";
}
if (!fileExists(file)) {
std::cout << "File not found " << file << "\n";
return 4;
}
auto code = fileRead(file);
auto bytecode = file + ".tsb";
auto relative = std::filesystem::relative(file, cwd);
if (fileExists(bytecode) && std::filesystem::last_write_time(bytecode) == std::filesystem::last_write_time(file)) {
run(fileRead(bytecode), code, relative.string());
} else {
compileAndRun(code, file, relative.string());
}
return 0;
}