-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
172 lines (149 loc) · 3.67 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
GOAL:
Create an interpreter which can run "Jo�o," a simple OOP language made to use a directory-oriented object tree
Somewhat dynamically typed but lets not get too angsty about it
*/
#include "Forward.h"
#include "AST.h"
#include "Interpreter.h"
#include "Object.h"
#include "Parser.h"
#include "Args.h"
#include "Terminal.h"
#include <chrono>
#ifdef __linux__
namespace Daemon
{
void initialize(); // Forward-declare for main.cpp specifically
void stop();
}
#endif
template <typename Ty_>
bool has(const std::vector<Ty_>& v, const Ty_& value)
{
for (Ty_ thing : v)
{
if (thing == value)
{
return true;
}
}
return false;
}
int main(int argc, char** argv)
{
std::vector<Args::Flags> flags;
int file_start = -1;
std::string filestr = Args::read_args(flags,argc,argv,file_start);
bool print_main_result = false;
#ifdef _DEBUG
bool print_execution_times = true;
#else
bool print_execution_times = false;
#endif
if(has(flags, Args::Flags::DisableFormatting)) {
Terminal::disableFormatting = true;
}
if ((flags.empty() && filestr.empty()) || has(flags,Args::Flags::Interactive))
{
Args::interactive_mode();
exit(0);
}
if (has(flags, Args::Flags::Version))
{
Args::print_version();
}
if (has(flags, Args::Flags::Help))
{
Args::print_help();
exit(0);
}
if(has(flags,Args::Flags::InitializeDaemon))
{
#ifdef __linux__
#ifdef JOAO_SAFE
//FIXME: Make this case-insensitive
if(!filestr.size() || filestr == "start")
{
Daemon::initialize();
}
else if (filestr == "stop")
{
Daemon::stop();
}
else if (filestr == "restart")
{
Daemon::stop();
Daemon::initialize();
}
else
{
std::cerr << "Unrecognized daemon directive '" << filestr << "'\n";
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
#else
std::cerr << "Joao can only run as a daemon when compiled in Safe Mode.\n";
exit(EXIT_FAILURE);
#endif
#else
std::cerr << "Opening as a daemon is not available on this platform!\n";
exit(EXIT_FAILURE);
#endif
}
if (has(flags, Args::Flags::Main))
{
print_main_result = true;
}
if (has(flags, Args::Flags::Executetime))
{
print_execution_times = true;
}
//Typical execution of a file
std::chrono::steady_clock::time_point t1;
std::ifstream file;
#ifdef _DEBUG
std::cerr << "Opening file " << filestr << "...\n";
#endif
file.open(filestr);
if (!file.good())
{
std::cerr << "Unable to open file " << filestr << "!\n";
exit(1);
}
t1 = std::chrono::steady_clock::now();
Scanner scn;
scn.scan(file);
file.close();
if(print_execution_times)
std::cout << "Scanning took " << std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - t1).count() << " seconds.\n";
t1 = std::chrono::steady_clock::now();
Parser pears(scn);
Program parsed = pears.parse();
if (print_execution_times)
std::cout << "Parsing took " << std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - t1).count() << " seconds.\n";
t1 = std::chrono::steady_clock::now();
Interpreter interpreter(parsed,false);
#ifdef LOUD_AST
parsed.dump();
#endif
//Lets populate the arguments to /main()
std::vector<Value> joao_args;
for(int i = file_start+1; i < argc; ++i)
{
joao_args.push_back(Value(std::string(argv[i])));
}
Value jargs = interpreter.makeBaseTable(joao_args,{},nullptr);
//Execute!
Value v = interpreter.execute(parsed, jargs);
if (print_main_result)
{
std::cout << v.to_json();
}
if (print_execution_times)
std::cout << "Execution took " << std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - t1).count() << " seconds.\n";
#ifdef PRINT_MAIN_RETURN_VAL
std::cout << v.to_string();
#endif
return EXIT_SUCCESS;
}