-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtable.h
198 lines (172 loc) · 4.3 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
/**
* @file symtable.c
* @author Tomáš Hobza ([email protected])
* @brief Implementation of the table of symbols functions.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#ifndef SYMTABLE_H
#define SYMTABLE_H
#define SYMTABLE_MAX_ITEMS 1024
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "stack.h"
#include "debug.h"
#include "error.h"
/**
* @brief Enum for the types of the expression.
*/
typedef enum
{
TYPE_EMPTY = -2, // empty expression
TYPE_INVALID = -1, // invalid expression
TYPE_INT = 0, // int
TYPE_DOUBLE = 1, // double
TYPE_STRING = 2, // string
TYPE_BOOL = 3, // bool
TYPE_NIL = 4, // nil
TYPE_INT_NIL = 5, // int?
TYPE_DOUBLE_NIL = 6, // double?
TYPE_STRING_NIL = 7, // string?
TYPE_BOOL_NIL = 8, // bool?
} Expression_type;
typedef struct
{
unsigned long gen_id_idx;
Expression_type type;
bool is_const;
bool is_initialized;
bool is_param;
} VariableData;
typedef struct
{
char *name;
char *id;
Expression_type type;
} ParamData;
typedef struct
{
Expression_type return_type;
bool found_return;
ParamData *params;
int params_count;
int capacity;
} FunctionData;
typedef struct symtable_item
{
char *id;
int scope;
enum
{
VARIABLE = 0,
FUNCTION = 1
} type; // typ symbolu
union
{
VariableData *var_data;
FunctionData *func_data;
} data;
struct symtable_item *next;
} symtable_item;
typedef struct
{
bool all_children_return;
bool found_else;
bool found_return;
symtable_item **symtable;
} symtable_t;
typedef symtable_t *symtable;
typedef struct
{
symtable_item *varItem;
symtable_item *funcItem;
} sym_items;
DECLARE_STACK_FUNCTIONS(symtable);
extern symtable_stack *sym_st;
/**
* @brief Struct to hold the stack of symtables.
*
* @param stack stack of symtables
*/
void symtable_stack_free_all(symtable_stack *stack);
/**
* @brief Calculates the hashed value of a string. Source: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
*
* @param input value to be hashed
* @return int
*/
uint32_t hash(char *input);
/**
* @brief Allocates a new symtable and initializes the items to NULL, then returns the address of the new symtable.
*
* @return symtable* - pointer to the new symtable
*/
symtable symtable_init();
/**
* @brief Adds a new symbol to the hash table of symbols. (Allows duplicates)
*
* @param item item to be added
* @param table table to put the symbol into
* @return symtable_item* - pointer to the new symbol
*/
symtable_item *symtable_add(symtable_item *item, symtable table);
/**
* @brief Finds a symbol in the table and returns it's pointer.
*
* @param name name of the symbol to find
* @param table table to search in
* @param is_func if the symbol is a function
* @return symtable_item* - pointer to the symbol or NULL if not found
*/
symtable_item *symtable_find(char *name, symtable table, bool is_func);
/**
* @brief Finds a symbol in the table and returns it's pointer.
*
* @param name name of the symbol to find
* @param stack stack of symtables to search in
* @param is_func if the symbol is a function
* @return symtable_item* - pointer to the symbol or NULL if not found
*/
symtable_item *symtable_find_in_stack(char *name, symtable_stack *stack, bool is_func);
FunctionData *init_func_data();
VariableData *init_var_data();
ParamData *init_param_data(int count);
/**
* @brief Adds a new parameter to the function.
*
* @param func function data to add the parameter to
*/
void add_param(FunctionData *func);
/**
* @brief Creates a new symbol object.
*
* @param item return a pointer to the new symbol
* @return symtable_item*
*/
symtable_item *init_symtable_item(bool is_func);
/**
* @brief Prints the content of the symtable.
*
* @param table table to print
*/
void symtable_print(symtable table);
/**
* @brief Recursively free all the chained synonyms
*
* @param item synonym to start the freeing with
*/
void free_synonyms(symtable_item *item);
/**
* @brief Free the memory used by the table and all it's items
*
* @param table table to free
*/
void symtable_free(symtable table);
#endif // SYMTABLE_H