-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.cpp
More file actions
142 lines (117 loc) · 3.17 KB
/
interpreter.cpp
File metadata and controls
142 lines (117 loc) · 3.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
#include "interpreter.hpp"
// system includes
#include <stdexcept>
#include <iostream>
// module includes
#include "token.hpp"
#include "parse.hpp"
#include "expression.hpp"
#include "environment.hpp"
#include "semantic_error.hpp"
#include "startup_config.hpp"
Interpreter::Interpreter()
{
inputQ = nullptr;
outputQ = nullptr;
}
Interpreter::Interpreter(MessageQueue<Message> * inQ, MessageQueue<Message> * outQ)
{
inputQ = inQ;
outputQ = outQ;
startup();
}
void Interpreter::startup()
{
std::ifstream ifs(STARTUP_FILE);
std::string strResult;
Message result;
if( (!ifs) && (outputQ != nullptr) ){
strResult = "Error: Could not open startup file for reading.";
//outputQ->push(Message(Message::Type::ErrorType, strResult));
}
// Only send message for evaluation errors
result = evalStream(ifs);
if( result.isError() && (outputQ != nullptr) ){
//outputQ->push(result);
}
ifs.close();
}
//void Interpreter::operator()() const
//{
// while(true){
//
// // Take a unit of work from the input queue
// Message line;
// inputQ->wait_and_pop(line);
//
// // Check for special kernel control commands
// if(line.getString() == "%stop") break;
// if(line.getString() == "%exit") break;
// if(line.getString() == "%reset"){
// // Clear and reset the Environment
// env.reset();
// startup();
// break;
// }
//
// // Process input and add result to output MessageQueue
// std::istringstream inStream(line.getString());
// Message result = evalStream(inStream);/*** Not thread-safe ***/
// outputQ->push(result);
// }
//}
void Interpreter::threadEvalLoop()
{
while(true){
// Take a unit of work from the input queue
Message line;
inputQ->wait_and_pop(line);
// Check for special kernel control commands
if(line.getString() == "%stop") break;
if(line.getString() == "%exit") break;
if(line.getString() == "%reset"){
// Clear and reset the Environment
env.reset();
startup();
break;
}
// Process input and add result to output MessageQueue
std::istringstream inStream(line.getString());
//Message result = evalStream(inStream);/*** Not thread-safe ***/
outputQ->push(Message(evalStream(inStream)));
}
// End of Program
}
Message Interpreter::evalStream(std::istream & stream){
std::ostringstream outStream;
std::string errResult;
Expression expResult;
Message result;
if(!parseStream(stream)){
errResult = "Error: Invalid Expression. Could not parse.";
result = Message(Message::Type::ErrorType, errResult);
}
else{
try{
expResult = evaluate();
result = Message(Message::Type::ExpressionType, expResult);
}
catch(const SemanticError & ex){
outStream << ex.what();
errResult = outStream.str();
result = Message(Message::Type::ErrorType, errResult);
}
}
return result;
}
/***********************************************************************
Backwards-Compatibility Base Code Methods (Don't Touch)
**********************************************************************/
bool Interpreter::parseStream(std::istream & expression) noexcept{
TokenSequenceType tokens = tokenize(expression);
ast = parse(tokens);
return (ast != Expression());
};
Expression Interpreter::evaluate(){
return ast.eval(env);
}