-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtable.h
468 lines (408 loc) · 17.4 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* @file symtable.h
* @author Dominik Horky (xhorky32@vutbr.cz)
* @author Roman Janiczek (xjanic25@vutbr.cz)
* @brief Header file for symtable
*/
#ifndef IFJ_PROJECT_SYMTABLE_H
#define IFJ_PROJECT_SYMTABLE_H
// General
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include "returns.h"
/**
* @brief Max size of the stack
*/
#define ST_MAXSTACK CHAR_MAX
/**
* @brief Status codes for symtable
* @enum stCodes
*/
typedef enum stExitCodes {
ST_SUCCESS = 1, /**< Success */
ST_ERROR = 0, /**< Error */
ST_ID_EXISTS = -1 /**< Already exists */
} stC;
/**
* @brief Identifier
*/
typedef char* stID;
/**
* @brief Enum containing node types
* @enum stNodeType
*/
typedef enum stNodeType {
ST_N_UNDEFINED, /**< Not defined (internal) */
ST_N_FUNCTION, /**< Function node */
ST_N_VARIABLE, /**< Variable node */
}stNType;
/**
* @brief Enum containing variable data types
* @enum stVarTypes
*/
typedef enum stVarTypes {
UNKNOWN, /**< [INTERNAL] Internal only */
INT, /**< [BASIC] Integer data type */
STRING, /**< [BASIC] String data type */
FLOAT64, /**< [BASIC] Float data type */
BOOL, /**< [BOOLTHEN] bool data type */
UNDERSCORE /**< [INTERNAL] Underscore (accepts all) */
} stVarType;
/**
* @brief Structure holding data about a function
* @enum stData
*/
typedef struct stFunctionData {
stID identifier; /**< Function ID */
int returnNum; /**< Number of return types/values */
stVarType returnType[CHAR_MAX]; /**< Function return type */
int paramNum; /**< Number of parameters function takes */
stVarType paramTypes[CHAR_MAX]; /**< Parameter types */
bool defined; /**< Is function defined? */
struct stNode** innerSymtable; /**< Inner symtable (is OK?) */
} stFData;
/**
* @brief Sturcutre holding data about a variable
* @enum stData
*/
typedef struct stVariableData {
int scope; /**< Number of scope */
stID identifier; /**< Variable ID */
stVarType type; /**< Variable type (int, string, float64) */
bool defined; /**< Is variable already defined? */
bool fncCall; /**< Not a variable but a function call */
} stVData;
/**
* @brief Structure for the node of the tree
*/
typedef struct stNode {
stID identifier; /**< Identificator of the node */
struct stFunctionData* fData; /**< Pointer to struct holding fnc data */
struct stVariableData* vData; /**< Pointer to struct holding var data */
struct stNode* predecessor; /**< NULL if GST (global symtable)
-> functions symtable, in case of
variables symtable (scopes and subscopes)
this will point to function node that
owns this scope */
struct stNode* LPtr; /**< Pointer to the left subtree */
struct stNode* RPtr; /**< Pointer to the right subtree */
} *stNodePtr;
/**
* @brief Stack structure (support structure for symtable implementation)
*/
typedef struct stStackT {
stNodePtr a[ST_MAXSTACK]; /**< Stack - storing nodes from the tree */
int top; /**< Indicate top of the stack - 0 => empty */
} stStack;
/*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* *
* GENERAL SYMTABLE FUNCTIONS *
* *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***/
/**
* @brief Initialize symtable
* @param symtable Pointer to the symtable
* @post symtable will be initilized to NULL
*/
void stConstruct ( stNodePtr *symtable );
/**
* @brief Destroy symtable
* @note Destroy & free all nodes of the symtable
* @param symtable Pointer to the symtable
*/
void stDestruct ( stNodePtr *symtable );
/**
* @brief Insert new node to the symtable
* @param symtable Pointer to the symtable
* @param identificator New node identificatior
* @param nodeType Type of the node (function or variable)
* @param datatype Datatype saved in the new node (variable type or function return type)
* @param scope Only for variables, set scope lvl
* @return ST_SUCCESS Everything went OK, requested node was deleted
* @return ST_ERROR Something went wrong (allocation problems or other)
* @return ST_ID_EXISTS Node alredy exists in the symtbale
*/
stC stInsert ( stNodePtr *symtable, stID identificator, stNType nodeType, stVarType datatype, int scope );
/**
* @brief Delete node from the symtable
* @param symtable Pointer to the symtable
* @param identificator Identificator of the node to be deleted
* @return ST_SUCCESS Everything went OK, requested node was deleted
* @return ST_ERROR Something went wrong (not found or other problems)
*/
stC stDelete ( stNodePtr *symtable, stID identificator );
/**
* @brief Find node in the symtable
* @param symtable Pointer to the symtable
* @param identificator Identificator of node that we are looking for
* @return stNodePtr* Pointer to the note if found else NULL
*/
stNodePtr stLookUp ( stNodePtr *symtable, stID identificator );
/*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* *
* ADVANCED SYMTABLE FUNCTIONS *
* *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* SETTERS *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***/
/**
* @brief Add return type of the function
* @param stNode Pointer to the selected node of the symtable
* @param datatype Datatype to be set from enum stDataTypes
* @pre stNode is pointer to existing function node of the symtable
* @pre datatype is from the enum stDataTypes
* @post returnType is added to returnType array (if everything is OK)
* @post nothing changed if something is not OK
*/
void stFncSetType ( stNodePtr stNode, stVarType datatype );
/**
* @brief Add or change type of the variable
* @param stNode Pointer to the selected node of the symtable
* @param datatype Datatype to be set from enum stDataTypes
* @pre stNode is pointer to existing variable node of the symtable
* @pre datatype is from the enum stDataTypes
*/
void stVarSetType ( stNodePtr stNode, stVarType datatype );
/**
* @brief Add new parameter to function
* @param stNode Pointer to the selected node of the symtable (has to be function node!)
* @param paramType Parameter type
*/
void stFncSetParam ( stNodePtr stNode, stVarType paramType );
/**
* @brief Set function as defined or undefined inside symtable for functions
* @param stNode Pointer to the selected node of the symtable (has to be function node!)
* @param defined Is defined or not?
*/
void stFncSetDefined ( stNodePtr stNode, bool defined );
/**
* @brief Assign inner symtable to function
* @param stNode Pointer to the selected node of the symtable (has to be function node!)
* @param symtable Pointer to symtable
*/
void stFncSetInnerSt ( stNodePtr stNode, stNodePtr* symtable );
/**
* @brief Set/unset variable as an function call in variables symtable
* @param stNode Pointer to the selected node of the symtable (has to be variable node!)
* @param fncCall Is fncCall?
*/
void stVarSetFncCall ( stNodePtr stNode, bool fncCall );
/**
* @brief Set scope number
* @param stNode Pointer to node
* @param scope Number of the scope
*/
void stVarSetScope ( stNodePtr stNode,int scope );
/*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* GETTERS *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***/
/**
* @brief Get type of the node \n
* Three types exists (UNDEFINED, VARIABLE & FUNCTION)
* @param stNode Pointer to the selected node of the symtable
* @return stNodeType pointer to existing node
*/
stNType stGetNodeType ( stNodePtr stNode );
/**
* @brief Get array of return types for the function
* @param stNode Pointer to the selected node of the symtable
* @return stVarType Array of return types defined for this function (position sensitive)
* @return NULL will be returned if node is not a function node! TAKE NOTE!
* @pre stNode is pointer to existing node of the symtable
*/
stVarType* stFncGetType ( stNodePtr stNode );
/**
* @brief Get type of the selected node
* @param stNode Pointer to the selected node of the symtable
* @return stVarType Type that was saved in the node
* @pre stNode is pointer to existing node of the symtable
*/
stVarType stVarGetType ( stNodePtr stNode );
/**
* @brief Gets information if function was already defined or not from symtable
* @param stNode Pointer to the selected node of the symtable
* @return true Function is defined
* @return false Function is not defined
*/
bool stDefined ( stNodePtr stNode );
/**
* @brief Get number of parameters the function has *
* @param stNode Pointer to the selected node of the symtable (has to be function node!)
* @return int Number of parameters (-1 if error)
*/
int stFncGetNumParams ( stNodePtr stNode );
/**
* @brief Get parameter types array of the function (use @see stFncGetNumParams to get number of params)
* @param stNode Pointer to the selected node of the symtable (has to be function node!)
* @return stVarType* Array of parameter types
*/
stVarType* stFncGetParams ( stNodePtr stNode );
/**
* @brief Get inner symtable of the function
* @param stNode Pointer to the selected node of the symtable (has to be function node!)
* @return stNodePtr* Pointer to inner symtable
*/
stNodePtr* stFncGetInnerSt ( stNodePtr stNode);
/**
* @brief Get name of the variable with scope identifier
* @param stNode Pointer to the node
* @return char* Name of the variable with the scope identifier
*/
char* stVarGetNameS ( stNodePtr stNode );
/*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* *
* SYMBOLTABLE STACK FUNCTIONS *
* *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* WORK WITH STACK AND SYMTABLE *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***/
/**
* @brief Initiate stack of symboltables
* @param stack Pointer to the stack
* @param symtable Pointer to the root symboltable
* @return eRC Return code (RC_OK || RC_WRN_INTERNAL || RC_ERR_INTERNAL)
*/
eRC stackStInit ( stStack *stack, stNodePtr *symtable );
/**
* @brief Destruct stack of symtables
* @param stack Pointer to the stack
* @return eRC Return code (RC_OK || RC_WRN_INTERNAL || RC_ERR_INTERNAL)
*/
eRC stackStDesctruct ( stStack *stack );
/**
* @brief Get pointer to the symtable on the top of the stack
* @note This will not remove the symtable from the stack
* @param stack Pointer to the stack
* @return stNodePtr* Pointer to the symtable on the top of the stack
*/
stNodePtr stackGetTopSt ( stStack *stack );
/**
* @brief Get pointer to the symtable on the bottom of the stack
* @note This will not remove the symtable from the stack
* @param stack Pointer to the stack
* @return stNodePtr* Pointer to the symtable on the bottom of the stack
*/
stNodePtr stackGetBotSt ( stStack *stack );
/**
* @brief Push a new symtable to the top of the stack
* @param stack Pointer to the stack
* @param symtable Pointer to the symtable
* @return eRC Return code (RC_OK || RC_WRN_INTERNAL || RC_ERR_INTERNAL)
*/
eRC stackPushSt ( stStack *stack, stNodePtr *symtable );
/**
* @brief Pop symtable from the stack
* @note This will REMOVE symtable from the top of the stack
* @param stack Pointer to the stack
* @return stNodePtr* Pointer to the symtable poped from the stack
*/
stNodePtr stackPopSt ( stStack *stack );
/*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* WORK SYMTABLE ON THE TOP OF THE STACK *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***/
/**
* @brief Classic symtable insert function (using stack of symtable instead of symtable itself)
* @note This will work with symtable on the top of the stack
* @param stack Pointer to the stack
* @param identifier Identifier of function or variable (char*)
* @param nodeType Type of the node (FUNCTION, VARIABLE or UNDEFINED)
* @param datatype Datatype of the variable (use UNKNOWN in case of working with FUNCTIONS)
* @param scope Only for variables, set scope lvl
* @return eRC Return code (RC_OK || RC_WRN_INTERNAL || RC_ERR_INTERNAL)
*/
eRC stStackInsert ( stStack *stack, stID identifier, stNType nodeType, stVarType datatype, int scope );
/**
* @brief Classic symtable delete function (using stack of symtable instead of symtable itself)
* @note This will work with symtable on the top of the stack
* @param stack Pointer to the stack
* @param identifier Identifier of function or variable (char*)
* @return eRC Return code (RC_OK || RC_WRN_INTERNAL || RC_ERR_INTERNAL)
*/
eRC stStackDelete ( stStack *stack, stID identifier );
/**
* @brief Classic symtable lookup function (using stack of symtable instead of symtable itself)
* @note This will work with symtable on the top of the stack
* @param stack Pointer to the stack
* @param identificator Identifier of function or variable (char*)
* @return stNodePtr Pointer to the node of the symtable (function or variable)
*/
stNodePtr stStackLookUp ( stStack *stack, stID identificator );
/*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* WORK WITH WHOLE STACK *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***/
/**
* @brief Looks throught the stack for variable
* @note Goes through all variable symtables to find variable desired
* @param stack Stack of variables (and global function -> GST -> bottom of the stack)
* @param identificator Name of the variable we want to find
* @return stVarType Type of the variable @see stVarTypes
*/
stVarType stVarTypeLookUp ( stStack *stack, stID identificator );
/*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* *
* STACK HELPER *
* *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***/
/**
* @brief Initialize stack
* @note This function is copied from c402 task of the IAL-DU2
* @param S Pointer to the stack (uninitialized)
*/
void SInitP (stStack *S);
/**
* @brief Push node to the stack
* @note This function is copied from c402 task of the IAL-DU2
* @param S Pointer to the stack (initialized)
* @param ptr Pointer to the node to be put on the stack
*/
void SPushP (stStack *S, stNodePtr ptr);
/**
* @brief Pop node from the top of the stack
* @note This function is copied from c402 task of the IAL-DU2
* @param S Pointer to the stack (initialized)
* @return stNodePtr* Pointer to the node of the symtable that was saved at the top of the stack
*/
stNodePtr STopPopP (stStack *S);
/**
* @brief Check if stack is empty
* @note This function is copied from c402 task of the IAL-DU2
* @param S Pointer to the stack
* @return true Stack is empty
* @return false Stack is not empty
*/
bool SEmptyP (stStack *S);
/*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***
* *
* HELPER & OTHER FUNCTIONS *
* *
*** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ***/
/**
* @brief Check symtable for undefined functions
* @param symtable Pointer to the symtable
* @return eRC @see returnCodes
*/
eRC checkFunctions ( stNodePtr symtable );
/**
* @brief Helper for checkFunctions
* @param symtable Pointer to the symtable
* @return eRC @see returnCodes
*/
eRC checkFunctions2 ( stNodePtr symtable );
/**
* @brief Display BST
* @note Code from IAL C402
* @param symtable Pointer to the symtable
*/
void displayBST( stNodePtr symtable );
/**
* @brief Display BST
* @note Code from IAL C402
* @param symtable Pointer to the symtable
* @param sufix
* @param fromdir
*/
void displayBST2( stNodePtr symtable, char* sufix, char fromdir );
#endif //IFJ_PROJECT_SYMTABLE_H