blob: 7ce6d5920e688e2a363fe3df35e1e6a5960ae84a (
plain) (
blame)
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
|
#include "ASTInterpreter.h"
#include "Expression.h"
#include <cassert>
#include <iostream>
ASTInterpreter::ASTInterpreter()
{}
ASTInterpreter::~ASTInterpreter()
{}
ASTInterpreter::ValueStack ASTInterpreter::Run(Expression* program)
{
program->Accept(*this);
ValueStack result;
m_Evaluation.swap(result);
return result;
}
void ASTInterpreter::RunExpression(const ExpressionPtr& e)
{
if (e) {
e->Accept(*this);
}
}
bool ASTInterpreter::EvalToBool(const ExpressionPtr& e)
{
e->Accept(*this);
auto result = m_Evaluation.top();
m_Evaluation.pop();
return result != 0.0;
}
#define NOT_IMPLEMENTED (void)e; assert(0 && "not-implemented")
void ASTInterpreter::Visit(LiteralNull* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(LiteralUndefined* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(LiteralString* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(LiteralNumber* e) {
m_Evaluation.push(e->GetValue());
}
void ASTInterpreter::Visit(LiteralBoolean* e) {
m_Evaluation.push(e->GetValue());
}
void ASTInterpreter::Visit(BinaryExpression* e) {
e->GetLeft()->Accept(*this);
e->GetRight()->Accept(*this);
auto right = m_Evaluation.top();
m_Evaluation.pop();
auto left = m_Evaluation.top();
m_Evaluation.pop();
switch (e->GetOperator()) {
case TokenType::Plus:
std::cout << "PLUS:" << left << ':' << right << std::endl;
m_Evaluation.push(left + right);
break;
case TokenType::Minus:
m_Evaluation.push(left - right);
break;
case TokenType::Star:
m_Evaluation.push(left * right);
break;
case TokenType::Division:
m_Evaluation.push(left / right);
break;
default:
NOT_IMPLEMENTED;
}
}
void ASTInterpreter::Visit(UnaryExpression* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(IdentifierExpression* e) {
auto values = m_Variables.find(e->GetName());
if (values != m_Variables.end()) {
m_Evaluation.push(values->second.top());
} else {
throw "Not-defined";
}
}
void ASTInterpreter::Visit(ListExpression* e) {
for (const auto& stmt: e->GetValues()) {
stmt->Accept(*this);
}
}
void ASTInterpreter::Visit(VariableDefinitionExpression* e) {
e->GetValue()->Accept(*this);
m_Variables[e->GetName()].push(m_Evaluation.top());
m_Evaluation.pop();
}
void ASTInterpreter::Visit(BlockStatement* e) {
for (const auto& stmt: e->GetValues()) {
stmt->Accept(*this);
}
}
void ASTInterpreter::Visit(LabeledStatement* e) {
RunExpression(e->GetStatement());
}
void ASTInterpreter::Visit(IfStatement* e) {
if (EvalToBool(e->GetCondition())) {
RunExpression(e->GetIfStatement());
} else {
RunExpression(e->GetElseStatement());
}
}
void ASTInterpreter::Visit(SwitchStatement* e) { NOT_IMPLEMENTED;}
void ASTInterpreter::Visit(CaseStatement *e) { NOT_IMPLEMENTED;}
void ASTInterpreter::Visit(WhileStatement* e) {
if (e->GetDoWhile()) {
RunExpression(e->GetBody());
}
while (EvalToBool(e->GetCondition())) {
RunExpression(e->GetBody());
}
}
void ASTInterpreter::Visit(ForStatement* e) {
for (RunExpression(e->GetInitialization());
EvalToBool(e->GetCondition());
RunExpression(e->GetIteration())) {
RunExpression(e->GetBody());
}
}
void ASTInterpreter::Visit(FunctionDeclaration* e) { NOT_IMPLEMENTED;}
void ASTInterpreter::Visit(TopStatements* e) {
for (const auto& stmt: e->GetValues()) {
stmt->Accept(*this);
}
}
void ASTInterpreter::Visit(EmptyExpression* e) {
NOT_IMPLEMENTED;
}
|