-
Notifications
You must be signed in to change notification settings - Fork 0
/
identifiermanager.cpp
334 lines (267 loc) · 7.47 KB
/
identifiermanager.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "identifiermanager.h"
#include <QtCore/QList>
static const int VariableAddressWidth = 4;
class IdentifierManager::IdentifierBase
{
public:
enum Type
{
Const,
Variable,
Procedure
};
Type type() const
{
return mType;
}
inline void setName( const QString &name )
{
mName = name;
}
inline QString name() const
{
return mName;
}
inline ProcedureIdentifier* parentProcedure() const
{
return mParentProcedure;
}
protected:
IdentifierBase( ProcedureIdentifier *parent, Type type )
: mParentProcedure( parent ), mType( type )
{
}
private:
ProcedureIdentifier *mParentProcedure;
Type mType;
QString mName;
};
class IdentifierManager::ConstIdentifier : public IdentifierManager::IdentifierBase
{
public:
ConstIdentifier( ProcedureIdentifier *parent )
: IdentifierBase( parent, Const )
{
}
inline void setIndex( int index )
{
mIndex = index;
}
inline int index() const
{
return mIndex;
}
private:
int mValue;
int mIndex;
};
class IdentifierManager::VariableIdentifier : public IdentifierManager::IdentifierBase
{
public:
VariableIdentifier( ProcedureIdentifier *parent )
: IdentifierBase( parent, Variable )
{
}
inline void setIndex( int index )
{
mIndex = index;
}
inline int index() const
{
return mIndex;
}
private:
int mIndex;
};
class IdentifierManager::ProcedureIdentifier : public IdentifierManager::IdentifierBase
{
public:
ProcedureIdentifier( int index, ProcedureIdentifier *parent )
: IdentifierBase( parent, Procedure ),
mIndex( index ),
mVariableAddressCounter( 0 )
{
}
void addIdentifier( IdentifierBase *identifier )
{
if ( identifier->type() == IdentifierBase::Variable ) {
VariableIdentifier *varIdentifier = static_cast<VariableIdentifier*>( identifier );
varIdentifier->setIndex( mVariableAddressCounter );
mVariableAddressCounter += VariableAddressWidth;
}
mChildIdentifiers.append( identifier );
}
inline QList<IdentifierBase*> childIdentifiers() const
{
return mChildIdentifiers;
}
void cleanup()
{
qDeleteAll( mChildIdentifiers );
mChildIdentifiers.clear();
}
inline int index() const
{
return mIndex;
}
inline int variableSize() const
{
return mVariableAddressCounter;
}
private:
int mIndex;
int mVariableAddressCounter;
QList<IdentifierBase*> mChildIdentifiers;
};
IdentifierManager::IdentifierManager()
: mMainProcedure( new ProcedureIdentifier( 0, 0 ) ),
mCurrentProcedure( mMainProcedure ),
mProcedureCounter( 1 )
{
}
IdentifierManager::~IdentifierManager()
{
delete mMainProcedure;
}
void IdentifierManager::setName( const QString &name )
{
mCurrentName = name;
}
void IdentifierManager::setValue( int value )
{
mCurrentValue = value;
}
bool IdentifierManager::pushConstIdentifier()
{
int index = 0;
if ( mConstIdentifierValues.contains( mCurrentValue ) ) {
index = mConstIdentifierValues.indexOf( mCurrentValue );
} else {
mConstIdentifierValues.append( mCurrentValue );
index = mConstIdentifierValues.count() - 1;
}
ConstIdentifier *identifier = new ConstIdentifier( mCurrentProcedure );
identifier->setName( mCurrentName );
identifier->setIndex( index );
mCurrentProcedure->addIdentifier( identifier );
return true;
}
bool IdentifierManager::pushVariableIdentifier()
{
VariableIdentifier *identifier = new VariableIdentifier( mCurrentProcedure );
identifier->setName( mCurrentName );
mCurrentProcedure->addIdentifier( identifier );
return true;
}
bool IdentifierManager::pushProcedure()
{
ProcedureIdentifier *identifier = new ProcedureIdentifier( mProcedureCounter, mCurrentProcedure );
identifier->setName( mCurrentName );
mCurrentProcedure->addIdentifier( identifier );
mProcedureCounter++;
mCurrentProcedure = identifier;
return true;
}
bool IdentifierManager::endProcedure()
{
ProcedureIdentifier *identifier = mCurrentProcedure;
if ( !identifier->parentProcedure() ) {
qDebug("IdentifierManager::endProcedure(): Try to leave main method");
return false;
}
mCurrentProcedure = identifier->parentProcedure();
identifier->cleanup();
return true;
}
bool IdentifierManager::hasVariableIdentifier( const QString &name ) const
{
return (find( IdentifierBase::Variable, name ) != 0);
}
bool IdentifierManager::hasConstIdentifier( const QString &name ) const
{
return (find( IdentifierBase::Const, name ) != 0);
}
bool IdentifierManager::hasProcedureIdentifier( const QString &name ) const
{
return (find( IdentifierBase::Procedure, name ) != 0);
}
bool IdentifierManager::hasLocalVariableIdentifier( const QString &name ) const
{
return (findLocal( IdentifierBase::Variable, name ) != 0);
}
bool IdentifierManager::hasLocalConstIdentifier( const QString &name ) const
{
return (findLocal( IdentifierBase::Const, name ) != 0);
}
bool IdentifierManager::hasLocalProcedureIdentifier( const QString &name ) const
{
return (findLocal( IdentifierBase::Procedure, name ) != 0);
}
int IdentifierManager::procedureIndex( const QString &name ) const
{
ProcedureIdentifier *identifier = static_cast<ProcedureIdentifier*>( find( IdentifierBase::Procedure, name ) );
return identifier->index();
}
int IdentifierManager::constIndex( const QString &name ) const
{
ConstIdentifier *identifier = static_cast<ConstIdentifier*>( find( IdentifierBase::Const, name ) );
return identifier->index();
}
int IdentifierManager::constIndex( int value ) const
{
return mConstIdentifierValues.indexOf( value );
}
int IdentifierManager::currentProcedureIndex() const
{
return mCurrentProcedure->index();
}
int IdentifierManager::currentProcedureVariableSize() const
{
return mCurrentProcedure->variableSize();
}
int IdentifierManager::procedureCount() const
{
return mProcedureCounter;
}
QList<int> IdentifierManager::constIdentifierValues() const
{
return mConstIdentifierValues;
}
void IdentifierManager::getVariableAddress( const QString &name, VariableType &type, int &variableAddress, int &procedureIndex ) const
{
const VariableIdentifier *identifier = static_cast<VariableIdentifier*>( find( IdentifierBase::Variable, name ) );
variableAddress = identifier->index();
const ProcedureIdentifier *parentProcedure = identifier->parentProcedure();
if ( parentProcedure == mMainProcedure ) {
type = Main;
} else if ( parentProcedure == mCurrentProcedure ) {
type = Local;
} else {
type = Global;
procedureIndex = parentProcedure->index();
}
}
IdentifierManager::IdentifierBase* IdentifierManager::find( int type, const QString &name ) const
{
ProcedureIdentifier *procedure = mCurrentProcedure;
do {
if ( type == IdentifierBase::Procedure ) {
if ( procedure->name() == name )
return procedure;
}
foreach ( IdentifierBase *identifier, procedure->childIdentifiers() ) {
if ( (identifier->type() == type) && (identifier->name() == name) )
return identifier;
}
procedure = procedure->parentProcedure();
} while ( procedure );
return 0;
}
IdentifierManager::IdentifierBase* IdentifierManager::findLocal( int type, const QString &name ) const
{
foreach ( IdentifierBase *identifier, mCurrentProcedure->childIdentifiers() ) {
if ( (identifier->type() == type) && (identifier->name() == name) )
return identifier;
}
return 0;
}