-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHackVirtualMachine.cpp
67 lines (55 loc) · 2.01 KB
/
HackVirtualMachine.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
#include <iostream>
#include "../HackVirtualMachine/VM/Utility/Utility.h"
#include "../HackVirtualMachine/VM/Parser.h"
#include "../HackVirtualMachine/VM/VirtualMachine.h"
int main()
{
try {
while (true) {
Ui::uiLogo();
string directoryPath = File::askDirectoryPath();
// check for valid directory and clear console.
bool isValidDir = File::isValidDirectory(directoryPath);
Ui::clear();
// if not valid directory force user to re-enter.
while (!isValidDir) {
Ui::uiLogo();
cout << "Invalid directory!" << endl;
directoryPath = File::askDirectoryPath();
isValidDir = File::isValidDirectory(directoryPath);
Ui::clear();
}
vector<string> vmFiles = File::getVMFiles(directoryPath);
if (vmFiles.empty()) {
cerr << "No .vm files found in the directory!" << endl;
continue;
}
// Prioritize Sys.vm if it exists
vector<string> orderedFiles;
for (const auto& file : vmFiles) {
if (fs::path(file).filename() == "Sys.vm") {
orderedFiles.insert(orderedFiles.begin(), file);
}
else {
orderedFiles.push_back(file);
}
}
RAM ram;
VirtualMachine virtualMachine("Prog.asm", ram);
try {
cout << "Files successfully opened" << endl;
// converting files to new file named Prog.asm.
virtualMachine.convertFiles(orderedFiles);
cout << "Finished converting!\n" << endl;
}
catch (const runtime_error& e) {
cerr << "Error during translating: " << e.what() << endl;
}
}
}
catch (const exception& e) {
cerr << "Unhandled exception: " << e.what() << '\n';
return 1;
}
return 0;
}