-
Notifications
You must be signed in to change notification settings - Fork 0
/
If.hpp
79 lines (73 loc) · 1.69 KB
/
If.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
#ifndef IF_HPP
#define IF_HPP
class IfCond : public Tree {
public:
IfCond(Expression *c)
: Tree(c, nullptr) {}
virtual void emit() override {
output << indent << "if (";
left->emit();
output << ')';
right->emit();
}
};
class IfElseCond : public Tree {
public:
IfElseCond(Expression *c)
: Tree(c, nullptr) {}
virtual void emit() override {
output << indent << "else if (";
left->emit();
output << ')';
right->emit();
}
};
class IfElse : public Tree {
public:
IfElse(Tree *i)
: Tree(i, nullptr) {}
virtual void emit() override {
output << " {\n";
++indent;
left->emit();
--indent;
output << indent << "}\n";
if (right) {
right->emit();
}
}
};
class Else : public Tree {
public:
Else(Tree *e)
: Tree(e, nullptr) {}
virtual void emit() override {
output << indent << "else {\n";
++indent;
left->emit();
--indent;
output << indent << "}\n";
}
};
class If : public Tree {
public:
If(Expression *c, Tree *i, Tree *e = nullptr, const std::vector<std::pair<Expression*,Tree*>>& ei = {})
: Tree(nullptr, nullptr) {
left = new IfCond(c);
Tree *ptr = left->link(new IfElse(i));
for (const auto& elseif : ei) {
ptr = ptr->link(new IfElseCond(elseif.first));
ptr = ptr->link(new IfElse(elseif.second));
}
if (e) {
ptr->link(new Else(e));
}
}
virtual void emit() override {
left->emit();
if (right) {
right->emit();
}
}
};
#endif // IF_HPP