-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtable.h
190 lines (163 loc) · 4.04 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
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file symtable.h
* @author Josef Kuchař ([email protected])
* @author Matej Sirovatka ([email protected])
* @author Tomáš Běhal ([email protected])
* @author Šimon Benčík ([email protected])
* @brief Header file for symbol table
*/
#ifndef __SYMTABLE_H__
#define __SYMTABLE_H__
#include <stdbool.h>
#include <string.h>
#include "str.h"
#include "token.h"
typedef struct htab htab_t;
typedef enum { HTAB_VARIABLE, HTAB_FUNCTION } htab_value_type_t;
typedef struct {
token_type_t type;
token_attribute_t value;
} htab_var_t;
typedef struct {
str_t name;
token_type_t type;
bool required;
} htab_param_t;
typedef struct {
token_type_t type;
bool required;
} htab_return_t;
typedef struct {
int param_count;
int param_count_guess;
bool defined;
htab_param_t* params;
htab_return_t returns;
} htab_fun_t;
typedef const char* htab_key_t; // Key type
typedef struct {
htab_value_type_t type;
union {
htab_fun_t function;
htab_var_t variable;
};
} htab_value_t; // Value type
// Key value pair
typedef struct htab_pair {
htab_key_t key; // Key
htab_value_t value; // Value
} htab_pair_t;
// One item in hash table
struct htab_item {
struct htab_pair pair;
struct htab_item* next;
};
// Hash table representation
struct htab {
size_t size;
size_t arr_size;
struct htab_item** arr_ptr;
};
/**
* @brief Create new hash table
*
* @return htab_t*
*/
htab_t* htab_new();
/**
* @brief Get number of items in hash table
*
* @param t Hash table
* @return Number of items
*/
size_t htab_size(const htab_t* t);
/**
* @brief Find item in hash table
*
* @param t Hash table
* @param key Key of searched item
* @return Pointer to item or NULL if not found
*/
htab_pair_t* htab_find(htab_t* t, htab_key_t key);
/**
* @brief Add new item to hash table
*
* @param t Hash table
* @param key Key (e.g. variable name)
* @param value Value (e.g. variable type)
* @return htab_pair_t*
*/
htab_pair_t* htab_add(htab_t* t, htab_key_t key, htab_value_t value);
/**
* @brief Add new function item to hash table
*
* @param t Hash table
* @param token Current token (function name)
* @return Pointer to new function inside hash table
*/
htab_pair_t* htab_add_function(htab_t* t, token_t* token, bool definition);
/**
* @brief Add parameter to existing function
*
* @param fun Pointer to function inside hash table
* @param token Current token (type)
*/
void htab_function_add_param(htab_pair_t* fun, token_t* token);
/**
* @brief Add parameter name to existing function parameter
*
* @param fun Pointer to function inside hash table
* @param token Current token (variable name)
*/
void htab_function_add_param_name(htab_pair_t* fun, token_t* token);
/**
* @brief Add return type to existing function
*
* @param fun Pointer to function inside hash table
* @param token Current token (return type)
*/
void htab_function_add_return_type(htab_pair_t* fun, token_t* token);
/**
* @brief Add variable to hash table
*
* @param t Hash table
* @param token Current token (variable name)
*/
bool htab_add_variable(htab_t* t, token_t* token);
/**
* @brief Check if all functions are properly defined
* Throws error if not
*
* @param t Hash table
*/
void htab_function_check_all_defined(htab_t* t);
/**
* @brief Check if function is call compatible with definition
* Throws error if not
*
* @param fun Pointer to function inside hash table
* @param param_count Expected number of parameters
* @param definition Wheter function is definition or call
*/
void htab_function_check_params(htab_pair_t* fun, int param_count, bool definition);
/**
* @brief Remove all items from hash table
*
* @param t Hash table
*/
void htab_clear(htab_t* t);
/**
* @brief Free hash table
*
* @param t Hash table
*/
void htab_free(htab_t* t);
/**
* @brief Define all buildin functions
*
* @param t Hash table
*/
void htab_define_buildin(htab_t* t);
#endif // __SYMTABLE_H__