-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymboltableDef.h
executable file
·138 lines (115 loc) · 4.81 KB
/
symboltableDef.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
/*
* Group no. 50
* Aniruddha Mahajan -------- 2017A7PS0145P
* Ravindra Singh Shekhawat - 2017A7PS0146P
* Shreyas Srikrishna ------- 2017A7PS0162P
* Chetan Sharma ------------ 2017A7PS0182P
*/
#ifndef symboltableDef
#define symboltableDef
#include "lexer.h"
#include "parser.h"
#include "ast.h"
#include "typeExtractorDef.h"
#define SYM_TABLE_SLOTS 20
// Different types of Symbol blocks (which scope/block current table corresponds to)
typedef enum SymbolTableType{
root,
functionBlock,
whileLoopBlock,
forLoopBlock,
switchCaseBlock
}SymbolTableType;
// Different types of symbols (types of hashed entries in the table's slots)
typedef enum SymbolForm{
idEntry,
driverEntry,
functionEntry,
forLoopEntry,
whileLoopEntry,
switchCaseEntry
}SymbolForm;
/* Identifier */
typedef struct IdEntry{
struct ASTNode* node; //pointer to corresponding ast node(IdNode)
int width; // size of the Identifier NUM has different size than RNUM
int offset; // offset calculation field will be assigned in Semantic Analyser Phase
struct Typeof type; //type of the identifier..DONE
bool isInputParam;
bool isTemporary;
struct SymbolTableEntry* next;
bool isOutputParam;
bool isWhileLoopCondition;
int widthOnScreen;
int offsetOnScreen;
}IdEntry;
/* Driver Module*/
typedef struct DriverEntry{
int sequenceNumber;
struct ASTNode* driverNode; //ast node pointer to moduleNode of driver
int activationRecordSize;
int ARSizeWithTemp;
int onScreenARSize;
Block block;
}DriverEntry;
/*Module*/
typedef struct FunctionEntry{
//struct ASTNode* node; //ast node pointer to function's ModuleNode
char functionName[21];
struct ASTNode* inputListHead; //ast node pointer to first input parameter node
struct ASTNode* outputListHead;//ast node pointer to first output parameter node
int sequenceNumber;
bool isDeclared; //true if function prototype exists, false if not
bool isDefined; //true if function definition exists, false if not
struct FunctionType inOutType; //type (input/output) of the function..DONE
int activationRecordSize; //size of activation record (basically sum of all offsets in function)
int ARSizeWithTemp;
int onScreenARSize;
Block block;
}FunctionEntry;
typedef struct ForLoopEntry{
struct ASTNode* node; //ast node pointer to forLoop's forLoopNode
}ForLoopEntry;
typedef struct WhileLoopEntry{
struct ASTNode* node; //ast node pointer to while loop's whileLoopNode
}WhileLoopEntry;
typedef struct SwitchCaseEntry{
struct ASTNode* node; //ast node pointer to switch-case block's ConditionalNode
}SwitchCaseEntry;
//Symbol in the symboltable can be one of the following types of entries :
typedef union Symbol{
struct IdEntry idEntry;
struct DriverEntry driverEntry;
struct FunctionEntry functionEntry;
struct ForLoopEntry forLoopEntry;
struct WhileLoopEntry whileLoopEntry;
struct SwitchCaseEntry switchCaseEntry;
}Symbol;
/*This struct represents a given symbol table entry. (It itself might be a part of a linkedlist of symboltable entries)
*/
typedef struct SymbolTableEntry{
struct SymbolTableEntry* next; //The next symbol table entry in the linkedlist
union Symbol symbol; //The symbol present at this entry
enum SymbolForm tag; //tag for the above union
struct SymbolTable* table; //The symboltable corresponding to current entry(which is a functionEntry/driverEntry/block)
}SymbolTableEntry;
//Union for scope
typedef union Scope{
struct Block block_scope;
char* scope;
}Scope;
/*This struct represents the actual contents of a symbol table
Need to add fields in future for codegen and type extraction if needed*/
typedef struct SymbolTable{
struct SymbolTable* parent; //The parent tree node (the outer scope corresponding to current scope)
struct SymbolTable* next; //The next SymbolTableNode at the current level of tree (basically the rightSibling)
struct SymbolTable* childScope; //basically the child of this table; (inward scoping)
/*When there is collision, the symboltable entries form a chain (linkedlist) at the slot where they collide.
so basically each slot in the table will hold a pointer to the head of the linkedlist of symboltable entries */
struct SymbolTableEntry* listHeads[SYM_TABLE_SLOTS];
union Scope scope;
/*This enumerated variable tells us the type of the block we are currently in
(whether it is a function or conditional block or iterative block)*/
enum SymbolTableType tableType; //This also acts as tag for above union
}SymbolTable;
#endif