-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotscript.cpp
More file actions
289 lines (236 loc) · 7.17 KB
/
plotscript.cpp
File metadata and controls
289 lines (236 loc) · 7.17 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
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
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <thread>
#include "interpreter.hpp"
#include "semantic_error.hpp"
#include "startup_config.hpp"
#include "message_queue.hpp"
#include "message.hpp"
//typedef std::string InputMessage;
//typedef Expression OutputMessage;
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 startup(Interpreter & interp){
std::ifstream ifs(STARTUP_FILE);
if(!ifs){
error("Could not open file for reading.");
return EXIT_FAILURE;
}
if(!interp.parseStream(ifs)){
ifs.close();
error("Invalid Program. Could not parse.");
return EXIT_FAILURE;
}
else{
try{
Expression exp = interp.evaluate();
}
catch(const SemanticError & ex){
ifs.close();
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
}
ifs.close();
return EXIT_SUCCESS;
}
int eval_from_stream(std::istream & stream){
Interpreter interp;
/*** Evaluate startup.pls ***/
if(startup(interp) != 0){
error("Invalid Startup Program.");
return EXIT_FAILURE;
}
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 isRunning(std::thread * kernel) noexcept{
//return kernel.joinable();
if(kernel != nullptr){
return kernel->joinable();
}
return false;
}
// Start an interpreter kernel in a separate thread.
// It should have no effect if a thread is already running.
// The number of threads used should be two after this command.
void start(std::thread * kernel, Interpreter * interp){
if(!isRunning(kernel)){
kernel = new std::thread(&Interpreter::threadEvalLoop, std::ref(*interp));
}
}
// Stop a running interpreter kernel. It should have no effect if a
// thread is already stopped.
// The number of threads used should be one after this command.
void stop(std::thread * kernel, MessageQueue<Message> * inQ){
if(isRunning(kernel)){
inQ->push(Message(Message::Type::StringType, "%stop"));
kernel->join(); // Wait for thread execution to catch up
//delete kernel; // De-allocate thread object heap memory
kernel = nullptr; // Remove old memory address stored
}
}
// Stop and reset a running interpreter kernel to the default state,
// clearing environment. It should then start a new running kernel.
// The number of threads used should be two after this command.
void reset(std::thread * kernel, Interpreter * interp, MessageQueue<Message> * inQ){
if(isRunning(kernel)){
inQ->push(Message(Message::Type::StringType, "%reset"));
kernel->join(); // Wait for thread execution to catch up
//kernel->~thread(); // De-allocate thread object heap memory
kernel = nullptr; // Remove old memory address stored
}
if(!isRunning(kernel)){
kernel = new std::thread(&Interpreter::threadEvalLoop, std::ref(*interp));
}
}
// If a user enters a plotscript expression when the interpreter is not
// running, display the error message: "Error: interpreter kernel not running".
// A REPL is a repeated read-eval-print loop
void repl(){
MessageQueue<Message> inputQueue;
MessageQueue<Message> outputQueue;
// Initialize Interpreter object and check results of startup
Interpreter interp(&inputQueue, &outputQueue);
//if(!outputQueue.empty()){
// Message result;
// if(outputQueue.try_pop(result)){
// try{
// Expression exp = result.getExp();
// std::cout << exp << std::endl;
// }
// catch (const SemanticError & ex){
// std::cerr << ex.what() << std::endl;
// }
// }
//}
// Start the new thread
std::thread * kernelThread = nullptr;
kernelThread = new std::thread(&Interpreter::threadEvalLoop, std::ref(interp));
//std::thread interpKernel(interp);
while(!std::cin.eof()){
prompt();
std::string line = readline();
if(line.empty()) continue;
// Check for special kernel control commands?
if(line == "%exit"){
if(isRunning(kernelThread)){
inputQueue.push(Message(Message::Type::StringType, "%stop"));
kernelThread->join(); // Wait for thread execution to catch up
//delete kernel; // De-allocate thread object heap memory
kernelThread = nullptr; // Remove old memory address stored
}
break;
}
else if(line == "%start"){
if(!isRunning(kernelThread)){
kernelThread = new std::thread(&Interpreter::threadEvalLoop, std::ref(interp));
}
continue;
}
else if(line == "%stop"){
if(isRunning(kernelThread)){
inputQueue.push(Message(Message::Type::StringType, "%stop"));
kernelThread->join(); // Wait for thread execution to catch up
//delete kernel; // De-allocate thread object heap memory
kernelThread = nullptr; // Remove old memory address stored
}
continue;
}
else if(line == "%reset"){
if(isRunning(kernelThread)){
inputQueue.push(Message(Message::Type::StringType, "%reset"));
kernelThread->join(); // Wait for thread execution to catch up
//kernel->~thread(); // De-allocate thread object heap memory
kernelThread = nullptr; // Remove old memory address stored
}
if(!isRunning(kernelThread)){
kernelThread = new std::thread(&Interpreter::threadEvalLoop, std::ref(interp));
continue;
}
else std::abort();
}
else if(!isRunning(kernelThread)){
error("interpreter kernel not running");
continue;
}
// Push message to input queue
inputQueue.push(Message(Message::Type::StringType, line));
// Asynchronously receive output results to display
Message result;
outputQueue.wait_and_pop(result);
try{
Expression exp = result.getExp();
std::cout << exp << std::endl;
}
catch(const SemanticError & ex){
std::cerr << ex.what() << std::endl;
}
}
// Double-check the Interpreter kernel was told to stop
if(isRunning(kernelThread)){
inputQueue.push(Message(Message::Type::StringType, "%stop"));
kernelThread->join(); // Wait for thread execution to catch up
//delete kernel; // De-allocate thread object heap memory
kernelThread = nullptr; // Remove old memory address stored
}
// Double-check current # of threads is 1 (how can I do this?)
// End of program
}
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;
}