Skip to content

Commit

Permalink
Handle extern declarations in a block
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent 006a45c commit 2764745
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ static Node *postfix(Token **rest, Token *tok);
static Node *unary(Token **rest, Token *tok);
static Node *primary(Token **rest, Token *tok);
static Token *parse_typedef(Token *tok, Type *basety);
static bool is_function(Token *tok);
static Token *function(Token *tok, Type *basety, VarAttr *attr);
static Token *global_variable(Token *tok, Type *basety, VarAttr *attr);

static void enter_scope(void) {
Scope *sc = calloc(1, sizeof(Scope));
Expand Down Expand Up @@ -1214,6 +1217,16 @@ static Node *compound_stmt(Token **rest, Token *tok) {
continue;
}

if (is_function(tok)) {
tok = function(tok, basety, &attr);
continue;
}

if (attr.is_extern) {
tok = global_variable(tok, basety, &attr);
continue;
}

cur = cur->next = declaration(&tok, tok, basety);
} else {
cur = cur->next = stmt(&tok, tok);
Expand Down
3 changes: 3 additions & 0 deletions test/common
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ void assert(int expected, int actual, char *code) {
static int static_fn() { return 5; }
int ext1 = 5;
int *ext2 = &ext1;
int ext3 = 7;
int ext_fn1(int x) { return x; }
int ext_fn2(int x) { return x; }
9 changes: 9 additions & 0 deletions test/extern.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ int main() {
ASSERT(5, ext1);
ASSERT(5, *ext2);

extern int ext3;
ASSERT(7, ext3);

int ext_fn1(int x);
ASSERT(5, ext_fn1(5));

extern int ext_fn2(int x);
ASSERT(8, ext_fn2(8));

printf("OK\n");
return 0;
}

0 comments on commit 2764745

Please sign in to comment.