-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbols.cc
63 lines (52 loc) · 2.31 KB
/
symbols.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
#include "symbols.hh"
#include <iomanip>
Symbol::Symbol ( ) :
id(""),type(MM_VOID_TYPE),symType(LOCAL),isInitialized(false),isConstant(false),child(0) { }
// Empty symbol
Symbol::Symbol (const std::string & _id, const DataType & _type) :
id(_id),type(_type),symType(LOCAL),isInitialized(false),isConstant(false),child(0) { }
// Initialized symbol
Symbol::Symbol (const std::string & _id, const DataType & _type,InitialValue _value) :
id(_id),type(_type),symType(LOCAL),isInitialized(true),isConstant(false),value(_value),child(0) { }
// Dummy symbol
Symbol::Symbol (const std::string & _id, const DataType & _type, const SymbolType & _symType) :
id(_id),type(_type),symType(_symType),isInitialized(false),isConstant(false),child(0) { }
Symbol::~Symbol () { }
std::ostream& operator<<(std::ostream& out, Symbol & symbol) {
out << std::setw(30) << symbol.id
<< std::setw(15) << symbol.type ;
out << std::setw(10);
if( !symbol.isInitialized ) out << "NULL" ;
else if( symbol.type == MM_CHAR_TYPE ) out << (int)symbol.value.charVal ;
else if( symbol.type == MM_INT_TYPE ) out << symbol.value.intVal ;
else if( symbol.type == MM_DOUBLE_TYPE ) out << symbol.value.doubleVal ;
else if( symbol.type == MM_STRING_TYPE ) {
std::string output = "$." + std::to_string(symbol.value.intVal) ;
out << output;
}
else out << "-----" ;
out << std::setw(10);
if( symbol.symType == LOCAL ) out << "local" ;
else if( symbol.symType == TEMP ) out << "temp" ;
else if( symbol.symType == RETVAL ) out << "retval" ;
else if( symbol.symType == PARAM ) out << "param" ;
else if( symbol.symType == CONST ) out << "const" ;
else out << "-----" ;
out << std::setw(10) << symbol.type.getSize()
<< std::setw(10) << symbol.child ;
return out;
}
std::ostream& operator<<(std::ostream & out, SymbolTable & symbolTable) {
out << "Table " << symbolTable.name << "(" << symbolTable.id << ")"
<< " , parent = #" << symbolTable.parent
<< " , paramCount = " << symbolTable.params << std::endl;
for( int idx = 0; idx < symbolTable.table.size() ; idx++ ) {
out << symbolTable.table[idx] << std::endl;
}
return out;
}
SymbolTable::SymbolTable(unsigned int _id,const std::string& _name="") :
id(_id),name(_name),parent(0),params(0),isDefined(false) { }
SymbolTable::~SymbolTable() {
table.clear();
}