-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtable.h
103 lines (79 loc) · 3.08 KB
/
symtable.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
#ifndef __SYMTABLE_H__
#define __SYMTABLE_H__
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
using namespace std;
// A symbol table for a single scope, i.e. block.
// It might reference its surrounding and inner scopes
// (the parent and children symbol tables).
class SymbolTable {
// The unique number of this block
int number;
// Pointer to the parent symbol table
// (might be NULL for the global table)
SymbolTable* parent;
// The mapping of identifiers to their types
map<string, string> mapping;
map<string, size_t> filenr;
map<string, size_t> linenr;
map<string, size_t> index;
// All symbol tables beneath this one (the sub-scopes)
// are keps in this map.
// Function blocks are stored under their name which
// makes it possible to list the function definition
// along with its scope. Normal blocks simply use
// their block number as key.
map<string,SymbolTable*> subscopes;
public:
// Creates and returns a new symbol table.
//
// Use "new SymbolTable(NULL)" to create the global table
SymbolTable(SymbolTable* parent);
// Creates a new empty table beneath the current table and returns it.
SymbolTable* enterBlock();
SymbolTable* leaveBlock();
// Adds the function name as symbol to the current table
// and creates a new empty table beneath the current one.
//
// Example: To enter the function "void add(int a, int b)",
// use "currentSymbolTable->enterFunction("add", "void(int,int)");
SymbolTable* enterFunction(string name,
string signature);
// Add a symbol with the provided name and type to the current table.
//
// Example: To add the variable declaration "int i = 23;"
// use "currentSymbolTable->addSymbol("i", "int");
void addSymbol(string name, string type);
void addLine(string name, size_t filenr, size_t linenr, size_t index);
// Dumps the content of the symbol table and all its inner scopes
// depth denotes the level of indention.
//
// Example: "global_symtable->dump(symfile, 0)"
void dump(FILE* symfile, int depth);
string map_function_types(string typeList);
void print_globals(FILE* symfile);
// Look up name in this and all surrounding blocks and return its type.
//
// Returns the empty string "" if variable was not found
string lookup(string name);
int lookupBlock(string name);
// Looks through the symbol table chain to find the function which
// surrounds the scope and returns its signature
// or "" if there is no surrounding function.
//
// Use parentFunction(NULL) to get the parentFunction of the current block.
string parentFunction(SymbolTable* innerScope);
// Running id number for symbol tables
static int N;
// Parses a function signature and returns all types as vector.
// The first element of the vector is always the return type.
//
// Example: "SymbolTable::parseSignature("void(int,int)")
// returns ["void", "int", "int"]
static vector<string> parseSignature(string signature);
};
extern SymbolTable *current_table;
extern SymbolTable *global_table;
#endif