-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantic.h
222 lines (197 loc) · 7.18 KB
/
semantic.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
/**
* @project Compiler IFJ22
* @file semantic.h
* @authors Matej Vadovič <xvadov01>
*/
#pragma once
#include "symtable.h"
#include "syntax_tree.h"
enum error_state {
OK = 0,
REDECLARATION_UNDECLARED = 3,
WRONG_ARGUMENT_RETURN_TYPE_COUNT = 4,
UNDEFINED_VAR = 5,
WRONG_RETURN_EXPR_COUNT = 6,
TYPE_COMPATIBILITY_ERROR = 7,
OTHER_SEM_ERROR = 8,
INTERNAL_ERROR = 99
};
/**
* @brief Structure to hold information about builtin function
*
*/
typedef struct builtin_fnc_def {
char *name;
char *params_array[5];
char *return_type;
} builtin_fnc_def_t;
/**
* @brief The main function of semantic analysis
*
* @param node main program BODY syntax tree node
* @return OK if sucess, else error enum
*/
enum error_state semantic_analysis(struct tree_node *node);
/* ------------------------------------------------------------- */
/* Auxiliary Functions */
/* ------------------------------------------------------------- */
/**
* @brief Creates and fills VARIABLES node in AST
*
* @param symtable symbol table stack where function variables are stored
* @param node parent syntax tree node
* @return OK if success, else error enum
*/
enum error_state add_variables_node(symtable_t *symtable, struct tree_node *node);
/**
* @brief value_t destructor
*
* @param toBeDeleted
*/
void value_t_free(void *toBeDeleted);
/**
* @brief Create builtin function entries in a symbol table
*
* @param fnc_symtable symbol table stack where function entries will be added
* @return OK if success, else error enum
*/
enum error_state add_builtin_functions(symtable_t *fnc_symtable);
/**
* @brief Adds parameter entry into a value_t structure
*
* @param content value_t structure where type will be stored
* @param type variable type
* @return OK if success, else error enum
*/
enum error_state add_parameter(value_t *content, char *type);
/**
* @brief Scan program and creates function declaration entries in symbol table
*
* @param fnc_symtable symbol table stack where function entries will be
* @param node main program BODY syntax tree node
* @return OK if success, else error enum
*/
enum error_state declarations_traversal(symtable_t *fnc_symtable, struct tree_node *node);
/**
* @brief Prints symbol table entries
*
* @param symtable symbol table stack, where top hashtable will be printed
*/
void print_table(symtable_t *symtable);
/**
* @brief Adds parameters as variables into a function frame
*
* @param symtable symbol table stack
* @param parameters_node PARAMETERS syntax tree node
* @return OK if success, else error enum
*/
enum error_state add_params_into_frame(symtable_t *symtable, struct tree_node *parameters_node);
/**
* @brief Translates type of enum tree_node_type into string
*
* @param type syntax tree node representing data type
* @return string of coresponding data type
*/
char *translate_type(enum tree_node_type type);
/**
* @brief Function arguments check
*
* @param symtable symbol table stack with a top frame where variables will be searched
* @param node VARIABLES of a function call syntax tree node
* @param fnc_rec hashtable record of the called function
* @return OK if success, else error enum
*/
enum error_state check_arguments(symtable_t *symtable, struct tree_node *node, htab_pair_t *fnc_rec);
/* ------------------------------------------------------------- */
/* Traversals functions */
/* ------------------------------------------------------------- */
/**
* @brief Traverse and process BODY node
*
* @param fnc_symtable symbol table stack with function records
* @param symtable symbol table stack with local hashtables(including main program)
* @param body_node BODY syntax tree node
* @param fnc_name function name string
* @return OK if success, else error enum
*/
enum error_state traverse_body(symtable_t *fnc_symtable, symtable_t *symtable, struct tree_node *body_node, char *fnc_name);
/**
* @brief Traverse and process FNC_DEC node
*
* @param fnc_symtable symbol table stack with function records
* @param symtable symbol table stack with local hashtables(including main program)
* @param node FNC_DEC syntax tree node
* @return OK if success, else error enum
*/
enum error_state traverse_fnc_dec(symtable_t *fnc_symtable, symtable_t *symtable, struct tree_node *node);
/**
* @brief Traverse and process ASSIGN node
*
* @param fnc_symtable symbol table stack with function records
* @param symtable symbol table stack with local hashtables(including main program)
* @param node ASSIGN syntax tree node
* @return OK if success, else error enum
*/
enum error_state traverse_assign(symtable_t *fnc_symtable, symtable_t *symtable, struct tree_node *node);
/**
* @brief Traverse and process EXPRESSION node
*
* @param symtable symbol table stack with local hashtables(including main program)
* @param node EXPRESSION syntax tree node
* @return OK if success, else error enum
*/
enum error_state traverse_expression(symtable_t *symtable, struct tree_node *node);
/**
* @brief Traverse and process FUNC_CALL node
*
* @param fnc_symtable symbol table stack with function records
* @param symtable symbol table stack with local hashtables(including main program)
* @param node FUNC_CALL syntax tree node
* @return OK if success, else error enum
*/
enum error_state traverse_func_call(symtable_t *fnc_symtable, symtable_t *symtable, struct tree_node *node);
/**
* @brief Traverse and process operator node
*
* @param symtable symbol table stack with local hashtables(including main program)
* @param node opearator syntax tree node
* @return OK if success, else error enum
*/
enum error_state traverse_operator(symtable_t *symtable, struct tree_node *node);
/**
* @brief Traverse and process OPERAND node
*
* @param symtable symbol table stack with local hashtables(including main program)
* @param node OPERAND syntax tree node
* @return OK if success, else error enum
*/
enum error_state traverse_operand(symtable_t *symtable, struct tree_node *node);
/**
* @brief Traverse and process IF node
*
* @param fnc_symtable symbol table stack with function records
* @param symtable symbol table stack with local hashtables(including main program)
* @param node IF syntax tree node
* @param fnc_name functin name string
* @return OK if success, else error enum
*/
enum error_state traverse_if(symtable_t *fnc_symtable, symtable_t *symtable, struct tree_node *node, char *fnc_name);
/**
* @brief Traverse and process WHILE node
*
* @param fnc_symtable symbol table stack with function records
* @param symtable symbol table stack with local hashtables(including main program)
* @param node WHILE syntax tree node
* @param fnc_name functin name string
* @return OK if success, else error enum
*/
enum error_state traverse_while(symtable_t *fnc_symtable, symtable_t *symtable, struct tree_node *node, char *fnc_name);
/**
* @brief Traverse and process RETURN node
*
* @param fnc_symtable symbol table stack with function records
* @param node RETURN syntax tree node
* @param fnc_name functin name string
* @return OK if success, else error enum
*/
enum error_state traverse_return(symtable_t *fnc_symtable, struct tree_node *node, char *fnc_name);