-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.cpp
83 lines (61 loc) · 1.66 KB
/
interpreter.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
#include "interpreter.hpp"
// system includes
#include <stdexcept>
// module includes
#include "token.hpp"
#include "parse.hpp"
#include "expression.hpp"
#include "environment.hpp"
#include "semantic_error.hpp"
bool Interpreter::parseStream(std::istream & expression) noexcept {
TokenSequenceType tokens = tokenize(expression);
ast = parse(tokens);
return (ast != Expression());
};
Expression Interpreter::evaluate() {
return ast.eval(env);
}
void Interpreter::clear() {
env.reset();
}
void Interpreter::interrupt() {
//MessageQueue<Expression> &interruptMessage = MessageQueue<Expression>::getInstance();
//Expression interruptMsg;
//interruptMessage.wait_and_pop(interruptMsg);
}
void Interpreter::Kernal()
{
MessageQueue<std::string> &inputMessage = MessageQueue<std::string>::getInstance();
MessageQueue<messageOut> &outputMessage = MessageQueue<messageOut>::getInstance();
messageOut outputMsg;
while (1)
{
std::string inputMsg;
inputMessage.wait_and_pop(inputMsg);
std::istringstream expression(inputMsg);
if (inputMsg == "%stop" || inputMsg == "%exit" || inputMsg == "%reset")
{
break;
}
if (!parseStream(expression)) {
outputMsg.isExpression = false;
outputMsg.error = "Error: Invalid Expression. Could not parse.";
outputMessage.push(outputMsg);
}
else
{
try {
outputMsg.expMsg = evaluate();
outputMsg.isExpression = true;
outputMessage.push(outputMsg);
continue;
}
catch (const SemanticError & ex)
{
outputMsg.isExpression = false;
outputMsg.error = ex.what();
outputMessage.push(outputMsg);
}
}
}
}