-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
80 lines (65 loc) · 1.64 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Batch.h"
#include "Ops.h"
#include "Debug.h"
#include "Memory.h"
#include "VM.h"
enum ExitCodes {
OK = 0,
TOO_MANY_ARGS = -1,
COMPILE_ERROR = -2,
INTERPRET_ERROR = -3
};
static void repl(VM& vm) {
std::string line;
for (;;) {
std::cout << "> ";
std::getline(std::cin, line);
std::cout << "\n";
vm.interpret(line);
}
}
static std::string fileToString(const std::string& path) {
std::ifstream file(path);
std::stringstream stream;
stream << file.rdbuf();
return stream.str();
}
int main(int argc, const char** argv) {
VM vm;
/*size_t testSize = 73473457;
std::cout << Memory::compressOpCodesToValue<size_t>(Memory::splitValueIntoOpCodes(testSize)) << "\n";
std::cout << "\n" << testSize << "\n";*/
/*
Batch batch;
//Add constant '1.2'
uint16_t index = batch.addConstant(1.2);
batch.addOp(Op::Code::Constant, 11);
batch.addOps(Memory::toOpCodes(index), 11);
//Add constant '345'
size_t index2 = batch.addConstant(345);
batch.addOp(Op::Code::Constant32, 12);
batch.addOps(Memory::toOpCodes(index2), 12);
//Negate stack top
batch.addOp(Op::Code::Negate, 12);
//Add
batch.addOp(Op::Code::Add, 12);
batch.addOp(Op::Code::Return, 12);
Debug::disassembleBatch(batch);
vm.interpret(batch);
*/
if (argc == 1) {
repl(vm);
}
else if (argc == 2) {
vm.interpret(fileToString(argv[1]));
}
else {
std::cout << "Usage: Titan [path]\n";
return TOO_MANY_ARGS;
}
return 0;
}