-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathputast.cc
77 lines (75 loc) · 1.76 KB
/
putast.cc
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
#include "putast.h"
void ast::indent(int indents) {
for (int i = 0; i < indents; ++i) cout << " ";
}
void ast::put(Constant* c, int ind) {
indent(ind);
cout << "Constant '" << c->value << "'" << endl;
}
void ast::put(Operator* o, int ind) {
char op = ' ';
switch (o->operation) {
case Operation::Add:
op = '+';
break;
case Operation::Sub:
op = '-';
break;
case Operation::Mul:
op = '*';
break;
case Operation::Div:
op = '/';
break;
case Operation::Pow:
op = '^';
break;
case Operation::Eq:
op = '=';
break;
}
indent(ind);
cout << "Operator '" << op << "' {" << endl;
indent(ind + 1);
cout << "Left [" << endl;
put(o->left, ind + 2);
indent(ind + 1);
cout << "]," << endl;
indent(ind + 1);
cout << "Right [" << endl;
put(o->right, ind + 2);
indent(ind + 1);
cout << "]" << endl;
indent(ind);
cout << "}" << endl;
}
void ast::put(Block* b, int ind) {
indent(ind);
cout << "Block (" << endl;
for (int i = 0; i < b->list.size(); ++i) {
Expression* e = b->list.at(i);
put(e, ind + 1);
}
indent(ind);
cout << ")" << endl;
}
void ast::put(Expression* e, int ind) {
if (e == nullptr) {
indent(ind);
cout << "(null expression)" << endl;
return;
}
if (e->is<Constant>()) {
put(e->cast<Constant>(), ind);
} else if (e->is<Operator>()) {
put(e->cast<Operator>(), ind);
} else if (e->is<Block>()) {
put(e->cast<Block>(), ind);
}
}
void ast::put(Expression* e) {
put(e, 0);
}
void ast::put(AST* a) {
put(a->base);
}