-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvenusMain.cpp
More file actions
118 lines (101 loc) · 2.4 KB
/
venusMain.cpp
File metadata and controls
118 lines (101 loc) · 2.4 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
/*
Jordan Dehmel
jdehmel@outlook.com
github.com/jorbDehmel
2023 - present
MIT licence via mit-license.org held by author
*/
#define TIMER
#include <iostream>
#include <fstream>
#include <chrono>
#include "src/cpu2.hpp"
#include "src/tags.hpp"
using namespace std;
struct options
{
string inFile;
};
options parseArgs(const int argc, const char *argv[])
{
options out;
out.inFile = "";
for (int i = 1; i < argc; i++)
{
string cur(argv[i]);
if (cur[0] != '-')
{
out.inFile = cur;
}
}
if (out.inFile == "")
{
throw runtime_error("No input file provided");
}
return out;
}
int main(const int argc, const char *argv[])
{
options opts;
try
{
opts = parseArgs(argc, argv);
}
catch (runtime_error &e)
{
cout << tags::red_bold
<< "Error parsing arguments.\n"
<< e.what() << '\n'
<< tags::reset;
return 7;
}
string inputFileName = opts.inFile;
// Look for .ter suffix on output
if (inputFileName.size() < 4 || inputFileName.substr(inputFileName.size() - 4) != ".ven")
{
// Ensure it's not a venus-shebang file
if (inputFileName.size() < 6 || inputFileName.substr(inputFileName.size() - 6) != ".vensh")
{
cout << tags::yellow_bold
<< "Warning! The proper file suffix for ternary compiled files is .ven\n"
<< tags::reset;
}
}
ifstream in(inputFileName);
if (!in.is_open())
{
cout << tags::red_bold
<< "Error: Could not open input file.\n"
<< tags::reset;
return 2;
}
// Load source code to string (raw bytes)
string code, line;
while (getline(in, line))
{
// Allows shebangs
if (line[0] != '#')
{
code += line + '\n';
}
}
in.close();
cpu2 c;
c.loadProgram(code);
#ifdef TIMER
auto begin = chrono::high_resolution_clock::now();
while (c.doInstr() != -1)
{
// This comment to make this loop less ugly
}
auto end = chrono::high_resolution_clock::now();
long int ellapsed = chrono::duration_cast<chrono::nanoseconds>(end - begin).count();
cout << "Nanoseconds: " << ellapsed << '\n';
#else
while (c.doInstr() != -1)
{
// This comment to make this loop less ugly
}
#endif
return 0;
}