/
rpnlist.h
155 lines (137 loc) · 3.26 KB
/
rpnlist.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
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
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: rpnlist.h - reverse polish notation classes header file
// Copyright (C) 2013 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2013-03-26 initial version (parts removed from translator.h)
#ifndef RPNLIST_H
#define RPNLIST_H
#include <list>
#include <memory>
#include <vector>
class Table;
class Token;
using TokenPtr = std::shared_ptr<Token>;
class RpnItem;
using RpnItemPtr = std::shared_ptr<RpnItem>;
using RpnItemVector = std::vector<RpnItemPtr>;
// class for holding an item in the RPN output list
class RpnItem
{
public:
RpnItem(TokenPtr token) : m_token{token} {}
RpnItem(TokenPtr token, RpnItemVector attached) : m_token{token},
m_attached{attached} {}
// access functions
TokenPtr &token(void)
{
return m_token;
}
int attachedCount(void) const
{
return m_attached.size();
}
const RpnItemVector attached() const
{
return m_attached;
}
bool operator==(const RpnItem &other) const;
bool operator!=(const RpnItem &other) const
{
return !(*this == other);
}
private:
TokenPtr m_token; // pointer to token
RpnItemVector m_attached; // array of attached item pointers
};
using RpnItemList = std::list<RpnItemPtr>;
// class for holding a list of RPN items
class RpnList
{
public:
RpnList(void) {}
RpnList(RpnList &&other) :
m_list{other.m_list},
m_codeSize{other.m_codeSize}
{
other.m_list.clear();
other.m_codeSize = 0;
}
RpnList &operator=(RpnList &&other)
{
std::swap(m_list, other.m_list);
std::swap(m_codeSize, other.m_codeSize);
return *this;
}
bool operator==(const RpnList &other) const;
bool operator!=(const RpnList &other) const
{
return !(*this == other);
}
RpnItemList::const_iterator begin() const
{
return m_list.begin();
}
RpnItemList::const_iterator end() const
{
return m_list.end();
}
int count() const
{
return m_list.size();
}
int empty() const
{
return m_list.empty();
}
RpnItemPtr back() const
{
return m_list.back();
}
TokenPtr lastToken(void) const
{
return m_list.back()->token();
}
void clear()
{
m_list.clear();
}
void append(TokenPtr token, RpnItemVector attached = RpnItemVector{})
{
m_list.emplace_back(RpnItemPtr{new RpnItem{token, attached}});
}
RpnItemList::iterator insert(RpnItemList::iterator iterator, TokenPtr token)
{
return m_list.emplace(iterator, RpnItemPtr{new RpnItem{token}});
}
RpnItemList::iterator appendIterator(TokenPtr token)
{
return insert(m_list.end(), token);
}
int codeSize(void) const
{
return m_codeSize;
}
bool setCodeSize(Table &table, TokenPtr &token);
private:
RpnItemList m_list; // list of rpn items
int m_codeSize; // size of code required for list
};
#endif // RPNLIST_H