-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotscript.cpp
More file actions
205 lines (183 loc) · 4.15 KB
/
plotscript.cpp
File metadata and controls
205 lines (183 loc) · 4.15 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
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <thread>
#include "startup_config.hpp"
#include "interpreter.hpp"
#include "semantic_error.hpp"
#include "cntlc_tracer.hpp"
typedef message_queue<std::string> MessageQueueStr;
void repl(Interpreter interp);
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, std::string filename) {
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;
}
}
if (filename == STARTUP_FILE)
repl(interp);
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, filename);
}
int eval_from_command(std::string argexp) {
std::istringstream expression(argexp);
return eval_from_stream(expression, "no_file");
}
void ProcessData(MessageQueueStr * msgIn, MessageQueueStr * msgOut, Interpreter * interp)
{
while (1)
{
std::string popMessage;
msgIn->wait_and_pop(popMessage);
if (popMessage == "%stop") break;
std::istringstream expMsg(popMessage);
std::ostringstream textOutput;
if (!interp->parseStream(expMsg))
{
msgOut->push("Invalid Expression. Could not parse.");
}
else
{
try
{
Expression exp = interp->evaluate();
textOutput << exp;
msgOut->push(textOutput.str());
}
catch (const SemanticError & ex)
{
textOutput << ex.what();
msgOut->push(textOutput.str());
}
}
}
}
// A REPL is a repeated read-eval-print loop
void repl(Interpreter interp) {
bool reset = false;
install_handler();
while (1)
{
global_status_flag = 0;
Interpreter InscopeInterp(interp);
MessageQueueStr * msgIn = new MessageQueueStr();
MessageQueueStr * msgOut = new MessageQueueStr();
bool KernalRunning = true;
std::thread * worker;
worker = new std::thread(ProcessData, msgIn, msgOut, &InscopeInterp);
std::string textOutput;
while (!std::cin.eof()) {
if (reset)
{
reset = false;
break;
}
prompt();
std::string line = readline();
if (line.empty()) continue;
if (line == "%exit")
{
msgIn->push("%stop");
worker->join();
return;
}
if (line == "%stop" && KernalRunning == true)
{
msgIn->push(line);
worker->join();
KernalRunning = false;
}
else if (line == "%start" && KernalRunning == false)
{
worker = new std::thread(ProcessData, msgIn, msgOut, &InscopeInterp);
KernalRunning = true;
}
else if (line == "%reset")
{
if (KernalRunning == true)
{
msgIn->push("%stop");
worker->join();
}
reset = true;
}
else if (KernalRunning == false)
{
std::cout << "Error: interpreter kernel not running";
continue;
}
else
{
msgIn->push(line);
while (!msgOut->try_pop(textOutput))
{
if (global_status_flag > 0) {
global_status_flag = 0;
std::cout << "Error: interpreter kernel interrupted";
msgIn->push("%stop");
worker->detach();
worker->~thread();
msgIn = new MessageQueueStr();
msgOut = new MessageQueueStr();
InscopeInterp = interp;
worker = new std::thread(ProcessData, msgIn, msgOut, &InscopeInterp);
break;
}
if (msgOut->try_pop(textOutput))
break;
}
std::cout << textOutput;
}
}
}
}
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 {
eval_from_file(STARTUP_FILE);
}
return EXIT_SUCCESS;
}