-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.c
76 lines (64 loc) · 1.36 KB
/
global.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
#include "./georgecc.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// グローバル変数
GVar *gvar;
GVar *find_gvar(Token *tok) {
for (GVar *var = gvar; var; var = var->next) {
if (var->len == tok->len && !memcmp(tok->str, var->name, var->len)) {
return var;
}
}
return NULL;
}
void add_global_variable(GVar *add_g) {
if (gvar == NULL) {
gvar = add_g;
return;
}
for (GVar *g = gvar; g; g = g->next) {
if (g->next == NULL) {
g->next = add_g;
return;
}
}
}
// Gvarを以下から作成
// int x;
// int *x;
// int *x[];
// int x[];
Node *generate_gvar_node(Type *type) {
for (;;) {
if (is_("*")) {
type = connect_deref(type, PTR, 0);
consume("*");
} else {
break;
}
}
if (token->kind != TK_IDENT) {
error("変数名が定義されていません。");
}
GVar *gvar = calloc(1, sizeof(GVar));
gvar->name = token->str;
gvar->len = token->len;
token = token->next;
if (is_("[")) {
expect("[");
int num = expect_number();
type = connect_deref(type, ARRAY, num);
expect("]");
}
gvar->type = type;
add_global_variable(gvar);
Node *node = calloc(1, sizeof(Node));
node->kind = ND_GVAR_DEF;
node->type = type;
expect(";");
return node;
}