-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.hpp
151 lines (129 loc) · 2.97 KB
/
interpreter.hpp
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
#include <string>
#include <cctype>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <map>
namespace lsbasi {
class Interpreter: public VisitorInterpreter {
AST *ast;
std::map<std::string, float> symbols;
float dispatcher(AST *node){
return node->handler(this);
}
float visit(Assign * node){
std::string var = (static_cast<Id *>(node->id))->value;
float value = dispatcher(node->expr);
symbols[var] = value;
return value;
}
float visit(Id * node){
std::stringstream error;
std::string var = node->value;
if (symbols.count(var))
return symbols[var];
else{
error << "Undefined variable => " << var;
throw std::runtime_error(error.str());
}
}
float visit(Num *node){
return node->value;
}
float visit(BinOp *node){
float ret = 0;
switch(node->token->type){
case Token::_PLUS:
ret = dispatcher(node->left) + dispatcher(node->right);
break;
case Token::_MINUS:
ret = dispatcher(node->left) - dispatcher(node->right);
break;
case Token::_MUL:
ret = dispatcher(node->left) * dispatcher(node->right);
break;
case Token::_INT_DIV:
ret = int(dispatcher(node->left) / dispatcher(node->right));
break;
case Token::_REAL_DIV:
ret = dispatcher(node->left) / dispatcher(node->right);
break;
}
return ret;
}
float visit(UnaryOp *node){
float ret = 0;
switch(node->token->type){
case Token::_PLUS:
ret = (+1) * dispatcher(node->fact);
break;
case Token::_MINUS:
ret = (-1) * dispatcher(node->fact);
break;
}
return ret;
}
float visit(Empty *node){
return 0;
}
float visit(StatementList *node){
float ret = 0;
std::vector<AST*>::iterator begin = node->statements.begin();
std::vector<AST*>::iterator end = node->statements.end();
for (std::vector<AST*>::iterator it = begin; it != end; ++it)
ret = dispatcher(*it);
return ret;
}
float visit(Program *node){
float ret = 0;
//dispatcher(node->id);
dispatcher(node->bloque);
return ret;
}
float visit(Block *node){
float ret = 0;
for (VarDecl * var : node->vars){
dispatcher(var);
}
dispatcher(node->compound_statement);
return ret;
}
float visit(DecList *node){
float ret = 0;
return ret;
}
float visit(Type *node){
float ret = 0;
return ret;
}
float visit(VarsDecl *node){
float ret = 0;
return ret;
}
float visit(VarDecl *node){
return 0.0;
}
float visit(ProcedureDecl *ast){
return 0.0;
}
float visit(FormalPrametersList *ast){
return 0.0;
}
public:
std::string environment(){
std::stringstream ss;
std::string sep = "";
ss << "{";
for (std::map<std::string, float>::iterator it=symbols.begin(); it!=symbols.end(); ++it){
ss << sep << it->first << " => " << it->second;
sep = ",";
}
ss << "}";
return ss.str();
}
Interpreter(AST * ast): ast(ast){}
float interpret (){
return dispatcher(ast);
}
};
};