-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.h
194 lines (165 loc) · 4.56 KB
/
gen.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
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file gen.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 IFJ22 code generator
*/
#ifndef __GEN_H__
#define __GEN_H__
#include "str.h"
#include "symtable.h"
#include "token.h"
#include "token_term.h"
typedef struct {
str_t header; // Global header (init, definition of global variables)
str_t global; // Global code
str_t functions; // All functions
str_t function_header; // Current function header (definition of local variables)
str_t function; // Current function code
str_t function_name; // Current function name
str_t variable; // Current variable name
str_t params; // Params for funcion calls
int param_count; // Number of call params
str_t* current; // Pointer to current string (function or global)
str_t* current_header; // Pointer to current header (function or global)
} gen_t;
/**
* @brief Initialize new generator
*
* @return Initiazed generator
*/
gen_t gen_new();
/**
* @brief Emit final code to stdout
*
* @param gen Generator instance
*/
void gen_emit(gen_t* gen);
/**
* @brief Generate program header
*
* @param gen Generator instance
*/
void gen_header(gen_t* gen);
/**
* @brief Generate program footer
*
* @param gen Generator instance
*/
void gen_footer(gen_t* gen);
/**
* @brief Free existing generator
*
*/
void gen_free(gen_t* gen);
/**
* @brief Generate if statement
*
* @param gen Generator instance
*/
void gen_if(gen_t* gen, int construct_count);
/**
* @brief Generate else statement (+ jump to end of if)
*
* @param gen Generator instance
*/
void gen_else(gen_t* gen, int construct_count);
/**
* @brief Generate end of if-else statement
*
* @param gen Generator instance
*/
void gen_if_else_end(gen_t* gen, int construct_count);
/**
* @brief Generate while statement
*
* @param gen Generator instance
*/
void gen_loop(gen_t* gen, int construct_count);
/**
* @brief Generate while condition jump
*
* @param gen Generator instance
* @param construct_count
*/
void gen_loop_exit(gen_t* gen, int construct_count);
/**
* @brief Generate end of while statement
*
* @param gen Generator instance
*/
void gen_loop_end(gen_t* gen, int construct_count);
/**
* @brief Generate function declaration
*
* @param gen Generator instance
* @param token Token with function name
*/
void gen_function(gen_t* gen, token_t* token);
/**
* @brief Generate function end
*
* @param gen Generator instance
* @param function Pointer to function data in table
*/
void gen_function_end(gen_t* gen, htab_fun_t* function, char* function_name);
/**
* @brief Generate function call
*
* @param gen Generator instance
* @param in_function Whether are we in function scope
*/
void gen_function_call(gen_t* gen, bool in_function);
/**
* @brief Generate function call frame
*
* @param gen Generator instance
* @param token Token with function name
*/
void gen_function_call_frame(gen_t* gen, token_t* token);
/**
* @brief Generate one function call parameter
*
* @param gen Generator instance
* @param token Token with variable name
* @param in_function Whether are we in function scope
*/
void gen_function_call_param(gen_t* gen, token_t* token, bool in_function);
/**
* @brief Generate variable definition (e.g. $var =)
*
* @param gen Generator instance
* @param token Token with variable name
* @param in_function Whether are we in function scope
*/
void gen_variable_def(gen_t* gen, token_t* token, bool in_function);
/**
* @brief Generate expression from expression tree generated by exp.c
*
* @param gen Generator instance
* @param root Root node
* @param in_function Wheter are we in function scope
*/
void gen_exp(gen_t* gen, token_term_t* root, bool in_function);
/**
* @brief Generate return (with value - from expression)
*
* @param gen Generator instance
*/
void gen_return(gen_t* gen, htab_fun_t* function, int construct_count);
/**
* @brief Generator return (without value)
*
* @param gen Generator instance
*/
void gen_return_void(gen_t* gen, htab_fun_t* function);
void gen_for_modify_start(gen_t* gen, int construct_count);
void gen_for_modify_end(gen_t* gen, int construct_count);
void gen_for_end(gen_t* gen, int construct_count);
void gen_break(gen_t* gen, int construct_count);
void gen_continue(gen_t* gen, int construct_count);
#endif // __GEN_H__