-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstmt.c
302 lines (253 loc) · 7.43 KB
/
stmt.c
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
291
292
293
294
295
296
297
298
299
300
301
302
/* $Source: /home/CVSROOT/c2ada/stmt.c,v $ */
/* $Revision: 1.2 $ $Date: 1999/02/03 19:45:04 $ */
/*
* This module implements the "stmt_t" type, which represents statements.
* It contains the statement creation routines called from the grammar.
*/
#include <assert.h>
#include "c2ada.h"
/* from scan.c */
extern file_pos_t yypos;
extern comment_block_pt fetch_comment_block(void);
/**************** allocation of stmt structs *************************/
stmt_t* new_stmt(stmt_kind_t stmt_kind, file_pos_t pos, bool default_com, comment_block_pt com)
{
static stmt_t* free = NULL;
static int free_index;
stmt_t* stmt;
if(free == NULL || free_index > 63)
{
free = (stmt_t*)allocate(sizeof(stmt_t) * 64);
free_index = 0;
}
stmt = &free[free_index++];
stmt->stmt_def = pos;
stmt->stmt_kind = stmt_kind;
if(default_com)
{
stmt->comment = fetch_comment_block();
}
else
{
stmt->comment = com;
}
stmt->scope = current_scope();
return stmt;
}
/************************ create new stmt *********************************/
/* These routines are all called from grammar.y */
stmt_pt new_stmt_Labelled(node_pt label, comment_block_pt com, stmt_pt s)
{
stmt_pt stmt = new_stmt(_Labelled, label->node_def, false, com);
stmt->stmt.label.id = label;
stmt->stmt.label.stmt = s;
return stmt;
}
stmt_pt new_stmt_Case(file_pos_t pos, comment_block_pt com, node_pt expr, stmt_pt s)
{
stmt_pt stmt = new_stmt(_Case, pos, false, com);
stmt->stmt.label.id = expr;
stmt->stmt.label.stmt = s;
return stmt;
}
stmt_pt new_stmt_Default(file_pos_t pos, comment_block_pt com, stmt_pt s)
{
stmt_pt stmt = new_stmt(_Default, pos, false, com);
stmt->stmt.default_stmt = s;
return stmt;
}
stmt_pt new_stmt_Null(file_pos_t pos)
{
return new_stmt(_Null, pos, true, 0);
}
stmt_pt new_stmt_Expr(node_pt expr)
{
stmt_pt stmt = new_stmt(_Expr, expr->node_def, true, 0);
stmt->stmt.expr = expr;
return stmt;
}
stmt_pt new_stmt_Compound(file_pos_t pos, symbol_t* decls, stmt_pt s)
{
stmt_pt stmt = new_stmt(_Compound, pos, true, 0);
stmt->stmt.compound.decls = decls;
stmt->stmt.compound.stmts = s;
return stmt;
}
stmt_pt new_stmt_If(file_pos_t pos, comment_block_pt com, node_pt cond, stmt_pt s)
{
stmt_pt stmt = new_stmt(_If, pos, false, com);
stmt->stmt.controlled.expr = cond;
stmt->stmt.controlled.stmt = s;
return stmt;
}
stmt_pt new_stmt_Ifelse(file_pos_t pos, comment_block_pt com, node_pt cond, stmt_pt then_stmt, stmt_pt else_stmt)
{
stmt_pt stmt = new_stmt(_Ifelse, pos, false, com);
stmt->stmt.if_else_stmt.expr = cond;
stmt->stmt.if_else_stmt.then_stmt = then_stmt;
stmt->stmt.if_else_stmt.else_stmt = else_stmt;
return stmt;
}
stmt_pt new_stmt_Switch(file_pos_t pos, comment_block_pt com, node_pt cond, stmt_pt s)
{
stmt_pt stmt = new_stmt(_Switch, pos, false, com);
stmt->stmt.controlled.expr = cond;
stmt->stmt.controlled.stmt = s;
return stmt;
}
stmt_pt new_stmt_While(file_pos_t pos, comment_block_pt com, node_pt expr, stmt_pt s)
{
stmt_pt stmt = new_stmt(_While, pos, false, com);
stmt->stmt.controlled.expr = expr;
stmt->stmt.controlled.stmt = s;
return stmt;
}
stmt_pt new_stmt_Do(file_pos_t pos, comment_block_pt com, node_pt expr, stmt_pt s)
{
stmt_pt stmt = new_stmt(_Do, pos, false, com);
stmt->stmt.controlled.expr = expr;
stmt->stmt.controlled.stmt = s;
return stmt;
}
stmt_pt new_stmt_For(file_pos_t pos, comment_block_pt com, node_pt e1, node_pt e2, node_pt e3, stmt_pt s)
{
stmt_pt stmt = new_stmt(_For, pos, false, com);
stmt->stmt.for_stmt.e1 = e1;
stmt->stmt.for_stmt.e2 = e2;
stmt->stmt.for_stmt.e3 = e3;
stmt->stmt.for_stmt.stmt = s;
return stmt;
}
stmt_pt new_stmt_Goto(file_pos_t pos, node_pt label)
{
stmt_pt stmt = new_stmt(_Goto, pos, true, 0);
stmt->stmt.goto_label = label;
return stmt;
}
stmt_pt new_stmt_Continue(file_pos_t pos)
{
return new_stmt(_Continue, pos, true, 0);
}
stmt_pt new_stmt_Break(file_pos_t pos)
{
return new_stmt(_Break, pos, true, 0);
}
stmt_pt new_stmt_MacroBody(file_pos_t pos, char* body)
{
stmt_pt stmt = new_stmt(_MacroBody, pos, true, 0);
stmt->stmt.macro_body = body;
return stmt;
}
stmt_pt new_stmt_Return(file_pos_t pos, node_pt expr)
{
stmt_pt s = new_stmt(_Return, pos, true, 0);
s->stmt.return_value = expr;
return s;
}
/********************* statement lists *********************************/
stmt_pt new_stmt_list(stmt_pt stmt)
{
stmt_pt stmts = new_stmt(_SList, stmt->stmt_def, true, 0);
stmts->stmt.stmt_list.first = stmt;
stmts->stmt.stmt_list.rest = 0;
return stmts;
}
stmt_pt append_stmt(stmt_pt stmts, stmt_pt stmt)
{
/* Append stmt to the end of stmts */
stmt_pt s1, s2;
/* find the end of the statement list */
for(s1 = stmts; (s2 = s1->stmt.stmt_list.rest); s1 = s2)
{
}
s1->stmt.stmt_list.rest = new_stmt_list(stmt);
return stmts;
}
stmt_pt concat_stmts(stmt_pt s1, stmt_pt s2)
{
stmt_pt sx1;
stmt_pt sx2;
stmt_pt si1, si2;
if(!s1)
return s2;
if(!s2)
return s1;
sx1 = (s1->stmt_kind == _List) ? s1 : new_stmt_list(s1);
sx2 = (s2->stmt_kind == _List) ? s2 : new_stmt_list(s2);
/* find end of first statment list */
for(si1 = sx1; (si2 = si1->stmt.stmt_list.rest); si1 = si2)
{
}
si1->stmt.stmt_list.rest = sx2;
return sx1;
}
void set_stmts_scope(stmt_pt stmt, scope_id_t scope)
{
if(stmt->stmt_kind == _List)
{
stmt->stmt.stmt_list.first->scope = scope;
set_stmts_scope(stmt->stmt.stmt_list.rest, scope);
}
stmt->scope = scope;
}
/********************* function definition *****************************/
symbol_t* new_func(symbol_t* decl, stmt_pt body)
{
decl->has_initializer = 1;
decl->sym_value.body = body;
grok_func_param_decls(decl);
set_scope_symbol(body->scope, decl);
return decl;
}
void define_func(symbol_t* funcdef, comment_block_pt comment)
{
assert(funcdef->sym_kind == func_symbol);
assert(funcdef->has_initializer);
assert(funcdef->sym_value.body->stmt_kind == _Compound);
function_def(funcdef);
funcdef->comment = comment;
}
/* gen_funcdef: print a function definition. */
void gen_funcdef(symbol_t* funcdef, int indent)
{
/* s is the compound statement which is the function def */
stmt_pt s = funcdef->sym_value.body;
symbol_t* sym;
int scope_id;
if(s->stmt.compound.decls)
{
/* Emit relevant declarations, indented (indent+4) deep. */
for(sym = s->stmt.compound.decls, scope_id = sym->sym_scope_id; sym;
sym = sym->sym_parse_list)
{
if(sym->sym_kind == var_symbol && sym->sym_scope_id == scope_id
&& !sym->is_static)
{
gen_var_or_field(sym, indent + 4, 20, -1, NULL, 0);
}
else if(sym->sym_kind == pkg_symbol)
{
gen_pkg_def(sym, indent + 4);
}
}
}
indent_to(indent);
put_string("begin");
new_line();
if(s->stmt.compound.stmts)
{
gen_stmt(s->stmt.compound.stmts, indent + 4);
}
else
{
indent_to(indent + 4);
put_string("null;");
new_line();
}
indent_to(indent);
put_string("end ");
/* put_string(funcdef->sym_ident->node.id.name); */
put_string(funcdef->sym_ada_name);
put_char(';');
new_line();
}