-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_body.c
More file actions
62 lines (52 loc) · 1.66 KB
/
Copy pathfunction_body.c
File metadata and controls
62 lines (52 loc) · 1.66 KB
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
#include "bianyi.h"
void function_body() {
// type func_name (...) {...}
// -->| |<--
// ... {
// 1. local declarations
// 2. statements
// }
int pos_local; // position of local variables on the stack.
int type;
pos_local = index_of_bp;
while (token == Int || token == Char) {
// local variable declaration, just like global ones.
basetype = (token == Int) ? INT : CHAR;
match(token);
while (token != ';') {
type = basetype;
while (token == Mul) {
match(Mul);
type = type + PTR;
}
if (token != Id) {
// invalid declaration
printf("%d: bad local declaration\n", line);
exit(-1);
}
if (current_id[Class] == Loc) {
// identifier exists
printf("%d: duplicate local declaration\n", line);
exit(-1);
}
match(Id);
// store the local variable
current_id[BClass] = current_id[Class]; current_id[Class] = Loc;
current_id[BType] = current_id[Type]; current_id[Type] = type;
current_id[BValue] = current_id[Value]; current_id[Value] = ++pos_local; // index of current parameter
if (token == ',') {
match(',');
}
}
match(';');
}
// save the stack size for local variables
*++text = ENT;
*++text = pos_local - index_of_bp;
// statements
while (token != '}') {
statement();
}
// emit code for leaving the sub function
*++text = LEV;
}