-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtable.h
132 lines (115 loc) · 3.66 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
/**
* @file symtable.h
* @author Jan Brudný ([email protected])
* @author Jakub Vlk ([email protected])
* @author Antonín Jarolím ([email protected])
* @brief Tabulka symbolů
* Implementation IFJ22 compiler
*
* The implementation used the hash table solution from the 2nd homework of IAL 2021/Z
*/
#ifndef IFJ22_SYMTABLE_H
#define IFJ22_SYMTABLE_H
#include "common.h"
#include "lex.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// musí být 2^n
#define MAX_HTSIZE 128
// definuje maximální zanoření v programu (kolik může být zanořených rámců)
#define MAX_SYMTABLES 128
// konstanty pro FNV-1a
#define FNV_OFFSET 14695981039346656037UL
#define FNV_PRIME 1099511628211UL
typedef enum symbolType {
function,
variable,
literal,
undefinedType
} symbolType_t;
typedef enum symbolDataType {
string,
stringNullable,
floating,
floatingNullable,
integer,
integerNullable,
nil,
undefinedDataType, // void ekvivalent
booltype,
symbolDataTypeLength
} symbolDataType_t;
// Todo: we have a great problem here:
// we need to know that symbol is variable, but also the type of the variable,
// so we need something like data type and symbol type???
typedef struct symbol {
const char *identifier;
symbolType_t type;
symbolDataType_t dataType;
int symtablePos;
struct DTList *firstParam;
symbolDataType_t returnType;
token_t token;
} symbol_t;
typedef struct htItem {
const char *key;
symbol_t value;
struct htItem *next;
} htItem_t;
typedef htItem_t *htTable_t[MAX_HTSIZE];
typedef struct symtable {
htTable_t functions;
htTable_t *current;
htTable_t main[MAX_SYMTABLES];
htTable_t infunc[MAX_SYMTABLES];
bool isInFunction;
int last;
int lastMain;
} symtable_t;
typedef struct DTListMem DTListMem_T;
typedef struct DTListMem {
DTListMem_T *next;
symbolDataType_t type;
char *identifier;
} DTListMem_T;
typedef struct DTList {
int len;
DTListMem_T *first;
} DTList_T;
void printSymtable(symtable_t *symtable);
void printHashtable(htTable_t *table, const char *index);
void printSymbol(symbol_t *symbol);
size_t ht_get_hash(const char *key);
void htInit(htTable_t *table);
htItem_t *htSearch(htTable_t *table, const char *key);
void htInsertItem(htTable_t *table, const char *key, symbol_t data);
symbol_t *htGetItem(htTable_t *table, const char *key);
void htDeleteItem(htTable_t *table, const char *key);
void htDestroy(htTable_t *table);
void symInit(symtable_t *symtable);
void symDestroy(symtable_t *symtable);
void symNewLocal(symtable_t *symtable);
bool symDelLocal(symtable_t *symtable);
void symInsert(symtable_t *symtable, symbol_t symbol);
void symInsertFunction(symtable_t *symtable, symbol_t symbol);
symbol_t *symSearchVar(symtable_t *symtable, const char *identifier);
symbol_t *symSearchFunc(symtable_t *symtable, const char *identifier);
void symSwitch(symtable_t *symtable);
void symSwitchBack(symtable_t *symtable);
DTList_T *createDTL(int count, ...);
symbol_t createSymbolVarLit(const char *name,
symbolType_t type,
symbolDataType_t dataType,
token_t token);
symbol_t *createSymbolFunction(const char *name,
symbolType_t type,
DTList_T *paramList,
symbolDataType_t returnType);
void saveBuildInFunctions(symtable_t *symtable);
void initDTList(DTList_T *list);
void insDTList(DTList_T *list, symbolDataType_t typ, char *identifier);
size_t countDtList(DTList_T *list);
#endif//IFJ22_SYMTABLE_H