-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotscript.cpp
358 lines (255 loc) · 6.84 KB
/
plotscript.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// This is an example of how to to trap Cntrl-C in a cross-platform manner
//by creating a simple REPL event loop and shows how to interrupt it.
#include <csignal>
#include <cstdlib>
#include <atomic>
// This global is needed for communication between the signal handler and the rest of the code. This atomic integer counts the number of times
volatile sig_atomic_t global_status_flag = 0;
extern std::atomic<bool> interrupt;
// *****************************************************************************
// install a signal handler for Cntl-C on Windows
// *****************************************************************************
#if defined(_WIN64) || defined(_WIN32)
#include <windows.h>
// this function is called when a signal is sent to the process
BOOL WINAPI interrupt_handler(DWORD fdwCtrlType) {
switch (fdwCtrlType) {
case CTRL_C_EVENT: // handle Cnrtl-C
// if not reset since last call, exit
if (global_status_flag > 0) {
exit(EXIT_FAILURE);
}
++global_status_flag;
return TRUE;
default:
return FALSE;
}
}
// install the signal handler
inline void install_handler() { SetConsoleCtrlHandler(interrupt_handler, TRUE); }
// *****************************************************************************
// *****************************************************************************
// install a signal handler for Cntl-C on Unix/Posix
// *****************************************************************************
#elif defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix)
#include <unistd.h>
// this function is called when a signal is sent to the process
void interrupt_handler(int signal_num) {
if (signal_num == SIGINT) { // handle Cnrtl-C
// if not reset since last call, exit
if (global_status_flag > 0) {
exit(EXIT_FAILURE);
}
++global_status_flag;
}
}
// install the signal handler
inline void install_handler() {
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = interrupt_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
}
#endif
// *****************************************************************************
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include "interpreter.hpp"
#include "semantic_error.hpp"
#include "startup_config.hpp"
#include "tsQueue.hpp"
#include <thread>
#define START "%start"
#define STOP "%stop"
#define RESET "%reset"
#define EXIT "%exit"
//Message Queue to pass input to interpreter kernel
MessageQueue<MessageType> ichannel;
//Message Queue to pass output expression to TUI
MessageQueue<Expression> ochannel;
//Message Queue to pass excepetions to TUI
MessageQueue<std::string> echannel;
//message queue to pass kernel command from TUI
MessageQueue<MessageType> kernel_cmd_channel;
//message channel for interrupts
MessageQueue<bool> kernel_ichannel;
void prompt() {
std::cout << "\nplotscript> ";
}
std::string readline() {
std::string line;
std::getline(std::cin, line);
return line;
}
void error(const std::string & err_str) {
std::cerr << "Error: " << err_str << std::endl;
}
void info(const std::string & err_str) {
std::cout << "Info: " << err_str << std::endl;
}
int eval_startup(Interpreter & interp)
{
std::ifstream ifs(STARTUP_FILE);
if (!ifs) {
error("Could not open file for reading.");
return EXIT_FAILURE;
}
if (!interp.parseStream(ifs)) {
error("Invalid Program. Could not parse.");
return EXIT_FAILURE;
}
else {
try {
Expression exp = interp.evaluate();
}
catch (const SemanticError & ex) {
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int eval_from_stream(std::istream & stream) {
Interpreter interp;
eval_startup(interp);
if (!interp.parseStream(stream)) {
error("Invalid Program. Could not parse.");
return EXIT_FAILURE;
}
else {
try {
Expression exp = interp.evaluate();
std::cout << exp << std::endl;
}
catch (const SemanticError & ex) {
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int eval_from_file(std::string filename) {
std::ifstream ifs(filename);
if (!ifs) {
error("Could not open file for reading.");
return EXIT_FAILURE;
}
return eval_from_stream(ifs);
}
int eval_from_command(std::string argexp) {
std::istringstream expression(argexp);
return eval_from_stream(expression);
}
bool runKernel()
{
std::string exception;
while (true) {
global_status_flag = 0;
prompt();
std::string line = readline();
if (std::cin.fail() || std::cin.eof()) {
std::cin.clear(); // reset cin state
line.clear(); //clear input string
std::cerr << "Error: interpreter kernel interrupted. \n";
}
else {
if (line == STOP) {
kernel_cmd_channel.push(STOP);
return false;
}
else if (line == RESET)
{
kernel_cmd_channel.push(RESET);
return true;
}
else if (line == EXIT)
{
kernel_cmd_channel.push(EXIT);
kernel_cmd_channel.push(EXIT);
return false;
}
//evaluate plotscript code
else {
ichannel.push(line);
Expression exp;
while (true)
{
if (ochannel.try_pop(exp))
{
std::cout << exp;
break;
}
else if (echannel.try_pop(exception))
{
break;
}
else if (global_status_flag > 0) {
interrupt = true;
}
}
}
}
}
return false;
}
// A REPL is a repeated read-eval-print loop
void repl() {
bool reset = false;
bool start = true;
install_handler();
while (true) {
global_status_flag = 0;
std::string line;
if (!reset && !start)
{
prompt();
line = readline();
if (std::cin.fail() || std::cin.eof()) {
std::cin.clear(); // reset cin state
line.clear(); //clear input string
continue;
}
}
if (line == STOP || line == RESET)
continue;
if (line == START || reset || start)
{
start = false;
reset = false;
Interpreter interp(&ichannel, &ochannel, &echannel, &kernel_cmd_channel, &kernel_ichannel);
eval_startup(interp);
std::thread kernel_thread(interp);
//Call
reset = runKernel();
kernel_thread.join();
if (!kernel_cmd_channel.empty())
break;
}
else
{
error("interpreter kernel not running");
}
}
}
int main(int argc, char *argv[])
{
if (argc == 2) {
return eval_from_file(argv[1]);
}
else if (argc == 3) {
if (std::string(argv[1]) == "-e") {
return eval_from_command(argv[2]);
}
else {
error("Incorrect number of command line arguments.");
}
}
else
{
repl();
}
return EXIT_SUCCESS;
}