-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotscript.cpp
252 lines (204 loc) · 6.03 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
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <thread>
#include<chrono>
#include "interpreter.hpp"
#include "semantic_error.hpp"
#include "startup_config.hpp"
// This is an example of how to to trap Cntrl-C in a cross-platform manner
// it creates a simple REPL event loop and shows how to interrupt it.
#include <csignal>
#include <cstdlib>
// This global is needed for communication between the signal handler
// and the rest of the code. This atomic integer counts the number of times
// Cntl-C has been pressed by not reset by the REPL code.
volatile sig_atomic_t global_status_flag = 0;
// *****************************************************************************
// 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
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_from_stream(std::istream & stream, Interpreter *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, Interpreter *interp){
std::ifstream ifs(filename);
if(!ifs){
error("Could not open file for reading.");
return EXIT_FAILURE;
}
return eval_from_stream(ifs,interp);
}
int eval_from_command(std::string argexp, Interpreter *interp){
std::istringstream expression(argexp);
return eval_from_stream(expression,interp);
}
// A REPL is a repeated read-eval-print loop
void repl(Interpreter *interp, bool &threadReset){
install_handler();
std::thread *thr1;
if (threadReset == true) {
eval_from_file(STARTUP_FILE, interp);
thr1 = new std::thread(&Interpreter::Kernal, interp);
}
Interpreter * newInterp = interp;
//newInterp = interp; // replacing with new interpreter for resetting
while (!std::cin.eof()) {
global_status_flag = 0;
MessageQueue<std::string> &inputMessage = MessageQueue<std::string>::getInstance();
MessageQueue<messageOut> &outputMessage = MessageQueue<messageOut>::getInstance();
messageOut output;
prompt();
std::string line = readline();
if (line.empty()) continue;
if (threadReset == false) {
if (line == "%start") {
thr1 = new std::thread(&Interpreter::Kernal, interp);
threadReset = true;
continue;
}
else {
error("interpreter kernel not running");
continue;
}
}
//if(line != "%start")
inputMessage.push(line);
if (line == "%stop") {
while (!thr1->joinable()) {
}
thr1->join();
threadReset = false;
continue;
}
else if (line == "%exit") {
thr1->join();
exit(EXIT_SUCCESS);
}
else if (line == "%reset") {
if (threadReset == true) {
while (!thr1->joinable()) {
}
thr1->join();
}
interp->clear();
interp = newInterp;
thr1 = new std::thread(&Interpreter::Kernal, interp);
continue;
}
while (!outputMessage.try_pop(output)) {
if (global_status_flag > 0) {
thr1->detach();
thr1->~thread();
interp->clear();
interp = new Interpreter;
interp = newInterp;
thr1 = new std::thread(&Interpreter::Kernal, interp);
error("interpreter kernel interrupted");
break;
}
}
//outputMessage.wait_and_pop(output);
if (line != "%start") {
if (output.isExpression == true) {
std::cout << output.expMsg << std::endl;
}
else {
std::cerr << output.error << std::endl;
}
}
}
thr1->join();
}
int main(int argc, char *argv[])
{
Interpreter *interp = new Interpreter;
bool threadReset = true;
if(argc == 2){
return eval_from_file(argv[1],interp);
}
else if(argc == 3){
if(std::string(argv[1]) == "-e"){
return eval_from_command(argv[2],interp);
}
else{
error("Incorrect number of command line arguments.");
}
}
else{
repl(interp,threadReset);
}
return EXIT_SUCCESS;
}