-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
111 lines (84 loc) · 2.32 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
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <regex>
#include <filesystem>
#include <vector>
#include "include/parser.h"
#include "include/code.h"
using namespace std;
namespace fs = std::filesystem;
string constructTranslatedFilePath(string path)
{
size_t position = path.rfind(".vm");
return path.replace(position, 3, ".asm");
}
string constructTranslatedDirectoryPath(string path)
{
string newPath = path;
size_t position = path.rfind("/");
if (position != string::npos && position == path.size() - 1)
{
newPath = path.replace(position, 1, "");
}
size_t slashIndex = path.find_last_of('/');
newPath += "/" + path.substr(slashIndex + 1);
return newPath + ".asm";
}
void processFile(string sourcePath)
{
Parser parser(sourcePath);
vector<vector<string>> commands = parser.getCommands();
string translatedPath = constructTranslatedFilePath(sourcePath);
Code code(translatedPath, commands, false);
code.translate();
}
void processDirectory(string sourcePath, vector<string> files)
{
string translatedPath = constructTranslatedDirectoryPath(sourcePath);
bool isNew = true;
for (const auto &file : files)
{
Parser parser(file);
vector<vector<string>> commands = parser.getCommands();
Code code(translatedPath, commands, isNew);
code.translate();
isNew = false;
}
}
int main(int argc, char *argv[])
{
if (!argv[1])
{
cout << "You must specify a vm file path!" << endl;
return 1;
}
string sourcePath = argv[1];
if (!regex_match(sourcePath, regex("^.+\\.vm")))
{
if (!fs::is_directory(sourcePath))
{
cerr << "Error: " << sourcePath << " is not a directory.\n";
return 1;
}
vector<string> vmFiles;
for (const auto &entry : fs::directory_iterator(sourcePath))
{
if (entry.is_regular_file() && entry.path().extension() == ".vm")
{
vmFiles.push_back(entry.path().string());
}
}
if (vmFiles.empty())
{
cout << "Directory does not contain vm files!" << sourcePath << "\n";
}
else
{
processDirectory(sourcePath, vmFiles);
}
}
else
{
processFile(sourcePath);
}
return 0;
}