-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymbolTable.h
162 lines (129 loc) · 4.5 KB
/
symbolTable.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
156
157
158
159
160
161
162
#ifndef _SYMBOLTable
#define _SYMBOLTable
#include "lexerDef.h"
#include "ast.h"
#define LOCAL_BIN_COUNT 13
#define MODULE_BIN_COUNT 13
#define SCOPE_BIN_COUNT 13
#define IO_BIN_COUNT 1
#define DYNAMIC_BIN_COUNT 1
enum _stType {
DRIVER_ST , MODULE_ST , FOR_ST, WHILE_ST , SWITCH_ST
} ;
struct _baseST ;
struct _moduleST ;
enum _variableType
{
VAR_INPUT ,
VAR_PLACEHOLDER,
VAR_OUTPUT ,
VAR_LOCAL ,
VAR_LOOP ,
VAR_MODULE
} ;
struct _varST {
char * lexeme ; // variable name
tokenID datatype ; // TK_INTEGER, TK_REAL, TK_BOOLEAN, TK_ARRAY
arrayTypeInfo *arrayIndices ; // if TK_ARRAY
int size ;
int offset ;
int tinker ;
void *scope ;
enum _variableType varType ;
} ;
struct _moduleST
{
char * lexeme ;
enum _stType tableType ;
void * parent ;
struct _varSTentry *localVars[LOCAL_BIN_COUNT] ;
struct _varSTentry *dynamicVars[DYNAMIC_BIN_COUNT] ;
struct _varSTentry *inputVars[IO_BIN_COUNT] ;
struct _varSTentry *outputVars[IO_BIN_COUNT] ;
struct _moduleSTentry *scopeVars[SCOPE_BIN_COUNT] ;
int filledMod ;
int inputSize ;
int outputSize ;
int currOffset ;
int scopeSize ;
} ;
struct _moduleSTentry {
struct _moduleST * thisModuleST ;
struct _moduleSTentry * next ;
} ;
struct _varSTentry {
struct _varST * thisVarST ;
struct _varSTentry * next ;
} ;
struct _baseST {
struct _varSTentry * vars[LOCAL_BIN_COUNT] ;
struct _moduleSTentry * modules[MODULE_BIN_COUNT] ;
struct _moduleST * driverST ;
int semanticError ;
} ;
struct _guardTinkerNode
{
int tinker ;
struct _guardTinkerNode *next ;
} ;
typedef enum _insertVarType
{
INSERT_INPUT, INSERT_OUTPUT, INSERT_LOCAL, INSERT_DYNAMIC
} insertVarType ;
typedef enum _searchVarType
{
SEARCH_INPUT, SEARCH_OUTPUT, SEARCH_LOCAL
} searchVarType ;
typedef enum _stType stType ;
typedef enum _variableType variableType ;
typedef struct _baseST baseST ;
typedef struct _moduleST moduleST ;
typedef struct _varST varST ;
typedef struct _varSTentry varSTentry ;
typedef struct _moduleSTentry moduleSTentry ;
typedef struct _guardTinkerNode guardTinkerNode ;
//general functions
int hashFunction ( char* lexeme , int size ) ;
char * generateString () ;
// function for base symbol table
baseST * createBaseST () ;
moduleST * createModuleST (void *parent, char * lexeme, int currOffset) ;
moduleST * createDriverST () ;
moduleST * createScopeST (moduleST * parent , stType scopeType ) ;
varST * createVarST (char *lexeme, void *scope, variableType varType, tokenID datatype) ;
// Insertion
void insertModuleSTInbaseST (moduleST * thisModule) ;
void insertVarSTInbaseST (varST * thisVarST ) ;
void insertScopeST ( moduleST* parent , moduleST * thisScopeST ) ;
void insertVar (moduleST *thisModule, varST *thisVarST, insertVarType flag) ;
//search
varST * searchVarInbaseST (char * lexeme) ;
moduleST * searchModuleInbaseST (char * lexeme) ;
varST* searchVarModuleList (moduleST* thisModule, char* lexeme, searchVarType flag) ;
varST* searchVarModule (moduleST * thisModule , char * lexeme) ;
varST * searchVar (moduleST *thisModule , char *lexeme) ;
// Printing
void printBaseST () ;
void printModuleST (moduleST * thisModuleST, int printParam) ;
// Tinker --> Checked for module outputs, loop variable, placeholders only
void printOutputsNotTinkered (moduleST *baseModule) ;
int checkAllOutputsTinkered (moduleST *baseModule) ;
void idListTinker (moduleST* baseModule, astNode *idListHead) ;
void tinkerVar (moduleST *baseModule, varST *var, astNode *varASTNode) ;
int hasTinkerListChanged (guardTinkerNode *tinkerHeadBefore, guardTinkerNode *tinkerHeadAfter) ;
guardTinkerNode* getGuardTinkerList (moduleST *baseModule, astNode *exprNode) ;
void getExprVars (moduleST *baseModule, guardTinkerNode *tinkerHead, astNode *exprNode) ;
void addTinkerList (guardTinkerNode *tinkerHead, int tinker) ;
// ST population
baseST * fillSymbolTable (astNode * thisASTNode, int depthSTPrint) ;
void fillModuleST (moduleST* baseModule , astNode* moduleASTNode, int depthSTPrint) ;
// Function call checker
int isValidCall (moduleST* thisModule , astNode * funcIDNode , int haveReturns) ;
varST * checkIP (moduleST * thisModule ,moduleST * targetModule , astNode * inputNode) ;
varST * checkOP (moduleST * thisModule ,moduleST * targetModule , astNode * inputNode) ;
// Helper
char* varTypeToString (variableType varType) ;
int getSize(varST * thisVar) ;
char *getParentModuleName (moduleST *scope) ;
char *typeIDToString (tokenID id) ;
#endif