forked from llvm-class/project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.cpp
161 lines (152 loc) · 4.7 KB
/
ast.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
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
#include "ast.h"
#include "pool.h"
namespace rift {
namespace ast {
std::string const & Str::value() const {
return Pool::getPoolObject(index);
}
std::string const & Var::value() const {
return Pool::getPoolObject(symbol);
}
class Printer: public Visitor {
public:
Printer(std::ostream & s):
s(s) {
}
void visit(ast::Exp * node) override {
s << "???";
}
void visit(ast::Num * node) override {
s << node->value;
}
void visit(ast::Str * node) override {
s << '"' << node->value() << '"';
}
void visit(ast::Var * node) override {
s << node->value();
}
void visit(ast::Seq * node) override {
for (Exp * e : node->body) {
e->accept(this);
s << "\n";
}
}
void visit(ast::Fun * node) override {
s << "function(" ;
for (Var * v : node->args) {
v->accept(this);
s << ", ";
}
s << ") {\n";
node->body->accept(this);
s << "}";
}
void visit(ast::BinExp * node) override {
node->lhs->accept(this);
s << " ";
switch (node->type) {
case ast::BinExp::Type::add: s << "+"; break;
case ast::BinExp::Type::sub: s << "-"; break;
case ast::BinExp::Type::mul: s << "*"; break;
case ast::BinExp::Type::div: s << "/"; break;
case ast::BinExp::Type::eq: s << "=="; break;
case ast::BinExp::Type::neq: s << "!="; break;
case ast::BinExp::Type::lt: s << "<"; break;
case ast::BinExp::Type::gt: s << ">"; break;
case ast::BinExp::Type::dot: s << "%*%"; break;
default: s << "?";
}
s << " ";
node->rhs->accept(this);
}
void visit(ast::Call * node) override {
s << "(";
for (Exp * v : node->args) {
v->accept(this);
s << ", ";
}
s << ")";
}
void visit(ast::UserCall * node) override {
node->name->accept(this);
visit(static_cast<ast::Call*>(node));
}
void visit(ast::SpecialCall * node) override {
visit(static_cast<ast::Call*>(node));
}
void visit(ast::CCall * node) override {
s << "c";
visit(static_cast<ast::SpecialCall*>(node));
}
void visit(ast::EvalCall * node) override {
s << "eval";
visit(static_cast<ast::SpecialCall*>(node));
}
void visit(ast::TypeCall * node) override {
s << "type";
visit(static_cast<ast::SpecialCall*>(node));
}
void visit(ast::LengthCall * node) override {
s << "length";
visit(static_cast<ast::SpecialCall*>(node));
}
void visit(ast::Index * node) override {
node->name->accept(this);
s << "[";
node->index->accept(this);
s << "]";
}
void visit(ast::SimpleAssignment * node) override {
node->name->accept(this);
s << " <- ";
node->rhs->accept(this);
}
void visit(ast::IndexAssignment * node) override {
node->index->accept(this);
s << " <- ";
node->rhs->accept(this);
}
void visit(ast::IfElse * node) override {
s << "if (";
node->guard->accept(this);
s << ") ";
node->ifClause->accept(this);
if (node->elseClause != nullptr) {
s << " else ";
node->elseClause ->accept(this);
}
}
void visit(ast::WhileLoop * node) override {
s << "while (";
node->guard->accept(this);
s << ") ";
node->body->accept(this);
}
private:
std::ostream & s;
};
void Exp::print(std::ostream & s) {
Printer p(s);
this->accept(&p);
}
void Num::accept(Visitor * v) { v->visit(this); }
void Str::accept(Visitor * v) { v->visit(this); }
void Var::accept(Visitor * v) { v->visit(this); }
void Seq::accept(Visitor * v) { v->visit(this); }
void Fun::accept(Visitor * v) { v->visit(this); }
void BinExp::accept(Visitor * v) { v->visit(this); }
void Call::accept(Visitor * v) { v->visit(this); }
void UserCall::accept(Visitor * v) { v->visit(this); }
void SpecialCall::accept(Visitor * v) { v->visit(this); }
void CCall::accept(Visitor * v) { v->visit(this); }
void EvalCall::accept(Visitor * v) { v->visit(this); }
void TypeCall::accept(Visitor * v) { v->visit(this); }
void LengthCall::accept(Visitor * v) { v->visit(this); }
void Index::accept(Visitor * v) { v->visit(this); }
void Assignment::accept(Visitor * v) { v->visit(this); }
void SimpleAssignment::accept(Visitor * v) { v->visit(this); }
void IndexAssignment::accept(Visitor * v) { v->visit(this); }
void IfElse::accept(Visitor * v) { v->visit(this); }
void WhileLoop::accept(Visitor * v) { v->visit(this); }
}
}