-
Notifications
You must be signed in to change notification settings - Fork 0
/
c++2python.h
290 lines (227 loc) · 6.71 KB
/
c++2python.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/****************************************************************************
c++2python.h:
Declares the data structure of ast_node, type_node, scope_node, sym_node and
other functions prototypes which are used through out the building of the compiler.
*****************************************************************************/
#ifndef cpp2python_H_
#define cpp2python_H_
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "c++2python.tab.h"
/** Parse functions **/
extern int yylex(void);
extern void yyerror(char const *s);
extern int yyparse(void);
/** AST nodes **/
typedef struct ast_node {
enum {
AST_LIST,
AST_FUNCDEF, AST_FUNCHEAD, AST_PARA,
AST_VAR,
AST_COMPOUND_STMT,AST_IN_STMT, AST_OUT_STMT, AST_EXPR_STMT, AST_IF_STMT, AST_FOR_STMT, AST_DO_STMT, AST_WHILE_STMT,
AST_RETURN_STMT,
AST_DEF, AST_S_DEF, AST_F_DEF, AST_DEC, AST_STR, AST_REAL, AST_ASS,
AST_BINOP, AST_PREFIX, AST_POSTFIX,
AST_FUNC_CALL,
AST_ID, AST_CONST,
AST_TYPE_LIMIT
} type;
union {
struct {
int type;
struct ast_node *head;
struct ast_node *tail;
} list;
struct {
int ret_type;
struct ast_node *funchead;
struct ast_node *compound_stmt;
} funcdef;
struct {
int sym_name;
struct ast_node *paras;
} funchead;
struct {
int para_type;
struct ast_node *var;
} para;
struct {
int sym_name;
int constant;
} var;
struct {
struct ast_node *defs;
struct ast_node *stmts;
struct ast_node *expr;
} compound_stmt;
struct {
int sym_name;
} in_stmt;
struct {
int sym_name;
char* costante;
} out_stmt;
struct {
struct ast_node *expr;
} expr_stmt;
struct {
struct ast_node *cond;
struct ast_node *then;
struct ast_node *els;
} if_stmt;
struct {
struct ast_node *init;
struct ast_node *cond;
struct ast_node *incr;
struct ast_node *body;
} for_stmt;
struct {
struct ast_node *stmt;
struct ast_node *expr;
} do_stmt;
struct {
struct ast_node *expr;
struct ast_node *stmt;
} while_stmt;
struct {
struct ast_node *retval;
} return_stmt;
struct {
int def_type;
struct ast_node *decs;
} def;
struct {
int def_type;
struct ast_node *str;
} def_s;
struct {
int def_type;
struct ast_node *real;
} def_f;
struct {
struct ast_node *var;
struct ast_node *init;
} dec;
struct {
struct ast_node *var;
char* stringa;
} str;
struct {
struct ast_node *var;
float real;
} real;
struct {
int op;
struct ast_node *lhs;
struct ast_node *rhs;
} binop;
struct {
int op;
struct ast_node *unary;
} prefix;
struct {
int op;
struct ast_node *unary;
} postfix;
struct {
struct ast_node *postfix;
struct ast_node *args;
} func_call;
struct {
int sym_name;
} id;
struct {
int number;
} constant;
};
int type_id;
char fg;
} ast_node;
typedef ast_node ast_expr;
typedef ast_node ast_list;
typedef ast_node ast_stmt;
struct sym_node
{
char str[1024];
int sym_id;
struct sym_node *next;
};
extern struct sym_node *sym_table;
extern int sym(char const *s);
extern char * symname(int sym_id);
extern int parse_int(char const *s);
extern float parse_float(char const *s);
struct vscope
{
struct vscope *parent;
struct vscope_node *list;
};
struct vscope_node
{
struct vscope *scope;
int sym_id;
int type_id;
struct vscope_node *next;
};
extern struct vscope *current_vscope;
extern void begin_vscope();
extern void vscope_addnode(struct vscope *v, int sym_id, int type_id);
extern void end_vscope();
enum TYPE {FUNC};
struct type_node
{
int type_id;
enum TYPE ty;
struct
{
int ret_type;
int para_num;
int para_type[1000];
} as_func;
struct type_node *next;
};
extern int int_type(void);
extern int float_type(void);
extern int string_type(void);
extern int func_type(ast_node *funchead);
extern struct type_node* find_type_node(int type_id);
extern struct vscope_node * find_vnode(int sym_id);
extern ast_list *list_new(int type, ast_node *head, ast_list *tail);
extern void list_append(ast_list *first, ast_list *second);
extern ast_node *funcdef_new(int ret_type, ast_node *funchead, ast_stmt *compound_stmt);
extern ast_node *funchead_new(int sym_name, ast_list *paras);
extern ast_node *para_new(int para_type, ast_node *var);
extern ast_node *var_new(int sym_name,int constant);
extern ast_stmt *compound_stmt_new(ast_list *defs, ast_list *stmts, ast_expr *expr);
extern ast_stmt *in_stmt_new(int sym_name);
extern ast_stmt *out_stmt_new(int sym_name, char* costante);
extern ast_stmt *expr_stmt_new(ast_expr *expr);
extern ast_stmt *if_stmt_new(ast_expr *cond, ast_stmt *then, ast_stmt *els);
extern ast_stmt *for_stmt_new(ast_expr *init, ast_expr *cond, ast_expr *incr, ast_stmt *body);
extern ast_stmt *do_stmt_new(ast_stmt *stmt, ast_expr *expr);
extern ast_stmt *while_stmt_new(ast_expr *expr,ast_stmt *stmt );
extern ast_stmt *return_stmt_new(ast_expr *retval);
extern ast_node *def_new(int def_type, ast_list *decs);
extern ast_node *def_s_new(int def_type, ast_node *str);
extern ast_node *def_f_new(int def_type, ast_node *real);
extern ast_node *dec_new(ast_node *var, ast_node *init);
extern ast_node *str_new(ast_node *var, char* stringa);
extern ast_node *real_new(ast_node *var, float real);
extern ast_expr *binop_new(int op, ast_expr *lhs, ast_expr *rhs);
extern ast_expr *prefix_new(int op, ast_expr *unary);
extern ast_expr *postfix_new(int op, ast_expr *unary);
extern ast_expr *func_call_new(ast_expr *postfix, ast_list *args);
extern ast_expr *id_new(int sym_name);
extern ast_expr *const_new(int number);
/** Declarations **/
extern void set_parse_tree(ast_node *tree);
extern void print_ast(ast_node *n);
extern void report_error(const char *fmt, ...);
extern void check_ast(ast_node *n);
extern void check_semantics(ast_node *ast);
extern void trans_ast(ast_node *ast);
extern void transtext_ast(ast_node *ast);
extern int binop_reuse(int op);
#endif