-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
137 lines (119 loc) · 3.33 KB
/
main.cpp
File metadata and controls
137 lines (119 loc) · 3.33 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "avm-fmt/Formatter.hpp"
#include "avm-lexer/Lexer.hpp"
#include "avm-lexer/Token.hpp"
#include "avm-lib/InputHandler.hpp"
#include "avm-parser/Instruction.hpp"
#include "avm-parser/Parser.hpp"
#include "avm-virtual-machine/Vm.hpp"
#include "external-libs/include/fmt/format.h"
#include "fmt/format.h"
#include <cstdio>
#include <deque>
#include <fmt/base.h>
#include <gtest/gtest.h>
#include <stdexcept>
#include <string>
void printInstruction(const Instruction &i) {
fmt::println("Instruction: {}, Value: {}", fmt::underlying(i.command),
i.value.value);
}
void fileMode(std::string filename) {
std::string source{InputHandler::readFile(filename)};
Lexer lexer{};
auto tokens{lexer.lex(source)};
Parser parser{tokens};
auto instructions{parser.parse<NonRepl>()};
if (parser.getErrorState()) {
for (auto &error : parser.getErrors()) {
fmt::println("{}", error);
}
std::exit(1);
}
Vm vm{instructions};
vm.interpret();
fmt::print(vm.getOutput());
}
void repl() {
int lineNumber{1};
std::vector<Token> tokens{};
bool breakFromWhile{false};
InputHandler inputHandler{};
while (auto line{inputHandler.readLine()}) {
Lexer lexer{};
auto nTokens{lexer.lex(*line + "\n", lineNumber)};
lineNumber++;
tokens.insert(tokens.end(), nTokens.begin(), nTokens.end());
for (const auto &token : nTokens) {
if (token.type == TokenType::EndOfProgram)
breakFromWhile = true;
}
if (breakFromWhile)
break;
}
Parser parser{tokens};
auto instructions = parser.parse<Repl>();
if (parser.getErrorState()) {
for (const auto &error : parser.getErrors()) {
fmt::println(stderr, "{}", error);
}
std::exit(1);
}
Vm vm{instructions};
vm.interpret();
fmt::print(vm.getOutput());
}
void formatFile(std::string filename, std::deque<std::string> args) {
(void)args;
Formatter formatter{};
std::string source{InputHandler::readFile(filename)};
if (auto formattedString{formatter.formatAvm(source)}) {
fmt::println("{}", *formattedString);
} else {
fmt::println(stderr, "Error, couldn't format file: Invalid avm program.");
}
}
void handleOptions(std::deque<std::string> args) {
args.pop_front(); // Remove program name
try {
std::string option = args.at(0);
std::string value = args.at(1);
if (option == "--format") {
args.pop_front();
formatFile(value, args);
} else {
fmt::println(stderr, "Usage: ./avm --format <.amv-file>");
}
} catch (std::out_of_range &e) {
fmt::println(stderr, "Usage: ./avm --format <.amv-file>");
}
}
std::deque<std::string> getArgs(int argc, char *argv[]) {
std::deque<std::string> args;
for (int i = 0; i < argc; i++) {
args.push_back(argv[i]);
}
return args;
}
constexpr bool test_mode{false};
int main(int argc, char *argv[]) {
if constexpr (test_mode) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
} else {
std::deque<std::string> args = getArgs(argc, argv);
try {
if (args.size() == 1) {
repl();
} else if (args.size() == 2) {
fileMode(args[1]);
} else if (args.size() > 2) {
handleOptions(args);
} else {
fmt::println(stderr, "Usage: ./avm [.avm file]");
}
} catch (std::exception &e) {
fmt::println(stderr, "{}", e.what());
}
return 0;
}
}