-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArgs.cpp
256 lines (243 loc) · 6.96 KB
/
Args.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "Args.h"
#include "Scanner.h"
#include "Parser.h"
#include "Interpreter.h"
#include "FailureOr.h"
#include "Terminal.h"
#include "Object.h"
#include "Error.h"
#include <sstream>
#include <exception>
#ifdef __GNUG__
#include <strings.h>
#define _strcmpi strcasecmp
#endif
std::string Args::read_args(std::vector<Flags>& v, int argc, char** argv, int& file_start)
{
for (int i = 1; i < argc; ++i)
{
if (_strcmpi(argv[i], "-i") == 0)
{
v.push_back(Flags::Interactive);
continue;
}
if (_strcmpi(argv[i], "-v") == 0)
{
v.push_back(Flags::Version);
continue;
}
if (_strcmpi(argv[i], "-h") == 0)
{
v.push_back(Flags::Help);
continue;
}
if (_strcmpi(argv[i], "-m") == 0)
{
v.push_back(Flags::Main);
continue;
}
if (_strcmpi(argv[i], "-e") == 0)
{
v.push_back(Flags::Executetime);
continue;
}
if (_strcmpi(argv[i], "-d") == 0)
{
v.push_back(Flags::InitializeDaemon);
continue;
}
if (_strcmpi(argv[i], "-f") == 0)
{
v.push_back(Flags::DisableFormatting);
continue;
}
//If this isn't any of these argless flags then I guess it's the file
file_start = i;
return std::string(argv[i]);
}
return std::string();
}
void Args::print_version()
{
std::cout << "Joao v" << VERSION_STRING <<"\tCopyright (C) 2021-2023 Altoids1 ([email protected])\n";
}
void Args::print_help()
{
std::cout << "Acceptable arguments: [option] [script [args]]\n";
std::cout << "Available options are:\n";
std::cout << " -v\tshow version information\n";
std::cout << " -h\tshow help dialog\n";
std::cout << " -i\tenter interactive mode\n";
std::cout << " -m\tprints the output of /main() into stdout after program completion, in JSON format.\n";
std::cout << " -e\tprints the execution time. On by default when compiled with _DEBUG.\n";
#if defined(__linux__) && defined(JOAO_SAFE)
std::cout << " -d\tInitializes Joao as a daemon, instead of executing a script.\n";
#endif
std::cout << " -f\tDisables colour (and other formatting) when emitting errors or messages.\n";
}
//Helper function for interactive_mode, since it has a fancy output :^)
static void print_version_fancy() {
/*
The original ASCII art, provided by our friends at figlet:
_
| | ___ __ _ ___
_ | |/ _ \ / _` |/ _ \
| |_| | (_) | (_| | (_) |
\___/ \___/ \__,_|\___/
*/
/*
Adding the pizzazz:
======_
======| | ___ __ _ ___
=======| |/ _ \ / _' |/ _ \
====| |_| | (_) | (_| | (_) |
===== \___/ \___/ \__,_|\___/
*/
const std::string asciiArt = R"(
======_ | Reference: https://altoids1.github.io/Joao/index.html
======| | ___ __ _ ___ |
=======| |/ _ \ / _' |/ _ \ |
====| |_| | (_) | (_| | (_) | | Version 1.2.3
===== \___/ \___/ \__,_|\___/ |
)";
// NOTE: Printing this with colour codes *could* have been a heap of spaghetti,
// but I'm going to do it the *right* way and just have this for-loop. You're welcome.
size_t charsSinceNewline = 0; // This should be a for loop initializer but, we still support C++17 (for some reason)
for(char c : asciiArt) {
Terminal::SetBold(std::cout,true);
if(c == '\n') {
charsSinceNewline = 0;
} else {
++charsSinceNewline;
}
switch(c) {
case '=':
Terminal::SetBold(std::cout,false);
Terminal::SetColor(std::cout,
Terminal::Color::Red
);
std::cout << c;
Terminal::ClearFormatting(std::cout);
continue;
case '1':
std::cout << VERSION_MAJOR;
break;
case '2':
std::cout << VERSION_MINOR;
break;
case '3':
std::cout << VERSION_PATCH;
break;
default:
std::cout << c;
}
}
}
static Program interactive_default_program() {
std::stringstream dummy_code;
dummy_code << "/main(){return 0;}/quit(){ throw /error/New(0,\"REPL exited via quit() call.\");}";
Scanner scn;
scn.scan(dummy_code);
if(scn.is_malformed)
throw std::runtime_error("interactive's default program failed to scan! This is a bug :(");
Parser prs(scn);
Program ret = prs.parse();
if(ret.is_malformed)
throw std::runtime_error("interactive's default program failed to scan! This is a bug :(");
return ret;
}
static std::optional<Value> try_run_expression(Interpreter& interp, Program& prog, std::string&& expr_str) {
std::stringstream expr_ss(expr_str);
Scanner scn(true);
scn.scan(expr_ss);
if(scn.is_malformed)
return {};
Parser prs(scn);
ASTNode* ptr;
try {
ptr = prs.parse_repl_expression(prog);
} catch (error::parser& err) {
return {};
}
if(ptr == nullptr)
return {};
Value ret = interp.evaluate_expression(ptr);
delete ptr; // FIXME: be RAII about this, come on.
if(interp.error) {
//In this case, we have to display this error value manually.
if(interp.error.t_vType == Value::vType::Object) {
Object* obj = interp.error.t_value.as_object_ptr;
if(Value* whatPtr = obj->has_property(interp,"what"); whatPtr != nullptr) {
Terminal::SetColor(std::cout, Terminal::Color::Red);
std::cout << whatPtr->to_string() << '\n';
Terminal::ClearFormatting(std::cout);
return {};
}
}
Terminal::SetColor(std::cout, Terminal::Color::Red);
std::cout << interp.error.to_string();
Terminal::ClearFormatting(std::cout);
return {};
}
return ret;
}
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
//In Emscripten's current setup, innocently doing std::getline() with std::cin actually causes us
//to hammer the external JS function that is feeding us stuff from stdin,
//as well as the fake /dev/tty.
//
//This means that we'll have to get kinda funky with it if we want to get user input w/o the front-end being a laggy, spinny, crashy mess.
//
//Make sure to use the -sASYNCIFY arg to em++ to make this work.
//meson.build *SHOULD* have it as a default linker argument. Try not to override it or the whole binary may just, not work at runtime.
static void detail_get_line(std::string& ret)
{
using std::cin;
char tryget;
while(true) {
tryget = cin.get();
if(std::char_traits<char>::not_eof(tryget))
break;
emscripten_sleep(100); // To avoid hammering it for characters.
}
ret.push_back(tryget);
while(true) {
tryget = cin.get();
if(tryget == std::char_traits<char>::eof() || !cin.good())
return;
if(tryget == '\n' || tryget == '\r')
return;
ret.push_back(tryget);
}
return;
}
#endif
void Args::interactive_mode()
{
print_version_fancy();
Program prog = interactive_default_program();
Interpreter interp(prog,true);
interp.push_stack("#repl");
while (true)
{
Terminal::SetColor(std::cout,Terminal::Color::Red);
std::cout << "> ";
Terminal::SetColor(std::cout,Terminal::Color::RESET);
std::string input;
#ifdef __EMSCRIPTEN__ // Explanation at the top of detail_get_line()'s def/impl.
detail_get_line(input);
#else
std::getline(std::cin, input);
#endif
if(input.empty())
continue;
if(input == "quit()") // FIXME: support quit() more generically
return;
if(input.back() != ';')
input.push_back(';');
auto result = try_run_expression(interp, prog, std::move(input));
if(result)
std::cout << result.value().to_string() << std::endl;
}
}