-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir-ll.h
85 lines (67 loc) · 1.37 KB
/
ir-ll.h
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
#include <cstdint>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <iostream>
#include <list>
class Node {
public:
enum class Type : char {
Constant,
Add,
};
size_t realSize();
const Type type;
Node(const Type type) : type(type) {}
virtual void print(std::ostream& os) const = 0;
friend std::ostream& operator<<(std::ostream& os, const Node& n) {
n.print(os);
return os;
}
virtual ~Node() {}
};
class Constant : public Node {
public:
const int value;
Constant(int value) : Node(Type::Constant), value(value) {}
void print(std::ostream& os) const override {
os << value;
}
};
class Add : public Node {
Node* l_;
Node* r_;
public:
Add(Node* l, Node* r) : Node(Type::Add), l_(l), r_(r) {}
Node* l() const {
return l_;
}
Node* r() const {
return r_;
}
void print(std::ostream& os) const override {
os << *l() << " + " << *r();
}
};
typedef std::deque<Node*> nodeDeque;
typedef std::list<Node*> nodeList;
template<typename container>
class NodeList : public container {
public:
~NodeList() {
for (auto n : *this)
delete n;
}
typename container::iterator at(size_t pos) {
auto it = this->begin();
std::advance(it, pos);
return it;
}
Node* push_back(Node* n) {
container::push_back(n);
return n;
}
};