forked from vojone/IFJ21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtable.h
205 lines (162 loc) · 5.13 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
/******************************************************************************
* IFJ21
* symtable.h
*
* Authors: Vojtěch Dvořák (xdvora3o), Tomáš Dvořák (xdvora3r)
* Purpose: Declaration of symbol table functions and structures
*
* Based on our solutions of second IAL project
*
* Last change: 7. 12. 2021
*****************************************************************************/
/**
* @file symtable.c
* @brief Declaration of symbol table functions and structures
*
* @authors Vojtěch Dvořák (xdvora3o), Tomáš Dvořák (xdvora3r)
*/
#ifndef SYMTABLE_H
#define SYMTABLE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dstring.h"
#include "dstack.h"
#define BUILTIN_TABLE_SIZE 8 /**< There are 8 predefined buitin functions */
#define UNDEFINED -1
#define UNSET -1
/**
* @brief Specifies type of symbol
*/
typedef enum sym_type {
FUNC, VAR, VAL
} sym_type_t;
/**
* @brief Specifies data type of variable or return type of function
*/
typedef enum sym_dtype {
INT, NUM, STR, NIL, BOOL
} sym_dtype_t;
/**
* @brief Specifies status of symbol (whether it was used, declared, defined)
*/
typedef enum sym_status {
DECLARED, DEFINED
} sym_status_t;
/**
* @brief Structure with all necessary data
*/
typedef struct sym_data {
string_t name;
sym_type_t type;
string_t ret_types;
string_t params;
sym_dtype_t dtype;
sym_status_t status;
bool was_used;
} sym_data_t;
/**
* @brief Element of symbol table (that is, in our case implemented as BST)
*/
typedef struct tree_node {
char * key;
sym_data_t data;
struct tree_node *l_ptr;
struct tree_node *r_ptr;
} tree_node_t;
DSTACK_DECL(tree_node_t*, ts)
/**
* @brief Symbol table data type for basic data storing
*/
typedef struct symtab {
tree_node_t * t;
int parent_ind; /**< Is used for switching contexts int parser */
} symtab_t;
DSTACK_DECL(symtab_t, symtabs) /** Stack of symbol tables */
/**
* @brief Symbol table data type, that can support superimposing of symbols
*/
typedef struct symbol_tables {
symtabs_stack_t symtab_st; /**< Stack for saving symbol tables */
symtab_t global; /**< Global symbol table for functions */
symtab_t symtab; /**< Current symbol table*/
} symbol_tables_t;
/**
* @brief Initializes symbol table
* @param symbol table to be initialized
*/
void init_tab(symtab_t *tab);
/**
* @brief Initializes data structure of symbol
*/
int init_data(sym_data_t *new_data);
/**
* @brief Frees all resources that data holds and set it to the state before initialization
*/
void data_dtor(sym_data_t *new_data);
/**
* @brief Inserts a new element into existing symbol table or updates existing node
* @param tab destination table
* @param key key of new element
*/
void insert_sym(symtab_t *tab, const char *key, sym_data_t new_data);
/**
* @brief Deletes element with specific key and frees all its resources
* @param tab destination table
* @param key key of element to be deleted
*/
void delete_sym(symtab_t *tab, const char *key);
/**
* @brief Replaces deleted element with two children by rightmost element
* @param tab destination table
* @param target replaced element
*/
void replace_rightmost(symtab_t *tab, symtab_t *target);
/**
* @brief Deletetes the entire symbol table and correctly frees its resources
* @param tab symbol table to be deleted
*/
void destroy_tab(symtab_t *tab);
/**
* @brief Searches for symbol table with specific key
* @param tab symbol table in which should be searching executed
* @param key key of element that should be found
* @return Pointer to found symbol or NULL
*/
tree_node_t *search(symtab_t *tab, const char *key);
/**
* @brief Converts character to sym_dtype enum
*/
sym_dtype_t char_to_dtype(char type_c);
/**
* @brief Converts enum type used in symtable to correspoding character symbol
*/
char dtype_to_char(sym_dtype_t type);
/**
* @brief inserts all builtin functions into given symbol table
* @note Inseted functions will have same name as key in symbol table
*/
void load_builtin_f(symtab_t *dst);
/**
* @brief Tries to find function by name in table of builtin functions
* @return Pointer to function data in static table if function is found, other wise NULL
*/
sym_data_t* search_builtin(const char *f_name);
/**
* @brief Checks if key identifies any of builtin functions, if yes puts it into given symtable
* @return True if symbol was found in builtin functions table otherwise false
*/
bool check_builtin(char *key, symtab_t *dst);
/**
* @brief Contains static array with builtin functions and its attributes (parameter, return types)
*/
sym_data_t* builtin_functions(unsigned int index);
/**
* @brief Performs searching in stack of symtabs
* @return If nothing is found returns NULL otherwise returns pointer to first occurence
*/
tree_node_t * deep_search(symtabs_stack_t *sym_stack,
symtab_t *start_symtab,
char *key);
#endif
/*** End of symtable.h ***/