-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (58 loc) · 1.83 KB
/
main.cpp
File metadata and controls
65 lines (58 loc) · 1.83 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
#include <fstream>
#include <chrono>
#include "Compiler.h"
using namespace Noble::Compiler;
/**
* @param fileName The name of the file whose contents we want to read
* @param fileBuffer Reference to a string where the file contents are stored
* @return The contents of the file, unformatted, as a std::string
*/
bool ReadFile(const std::string& fileName, std::string& fileBuffer)
{
std::ifstream file(fileName);
if (!file.is_open())
{
std::cout << "Failed to open file '" << fileName << "'\n";
return false;
}
file.seekg(0, std::ios::end);
const long size = file.tellg();
std::string buffer(size, ' ');
file.seekg(0);
file.read(&buffer[0], size);
fileBuffer = buffer;
return true;
}
int main(const int argc, char** argv)
{
//If argc == 1, no arguments were provided
if (argc == 1)
{
std::cout << "Usage: 'noblec filename.extension'\n";
return 0;
}
//Start compile timer
typedef std::chrono::high_resolution_clock clock;
typedef std::chrono::duration<float> duration;
const clock::time_point start = clock::now();
//Assume each arg is the name of a file to be compiled
for (int i = 1; i < argc; ++i)
{
std::string fileContents;
if (!ReadFile(argv[i], fileContents))
{
std::cerr << "Unable to read file!\n";
return -1;
}
const std::string fileTitle = argv[i];
const std::string fileName = fileTitle.substr(0, fileTitle.find_first_of('.'));
if (Compiler compiler; !compiler.Compile(fileContents, fileName))
{
std::cout << "Error compiling '" << argv[i] << "'\n";
return -1;
}
}
const duration elapsed = clock::now() - start;
std::cout << "Compilation succeeded in " << elapsed.count() << "s\n";
return 0;
}