Skip to content

Commit

Permalink
Add arrays of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Sep 28, 2020
1 parent c123e64 commit 5b2d571
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,16 @@ static Type *func_params(Token **rest, Token *tok, Type *ty) {
}

// type-suffix = "(" func-params
// | "[" num "]"
// | "[" num "]" type-suffix
// | ε
static Type *type_suffix(Token **rest, Token *tok, Type *ty) {
if (equal(tok, "("))
return func_params(rest, tok->next, ty);

if (equal(tok, "[")) {
int sz = get_number(tok->next);
*rest = skip(tok->next->next, "]");
tok = skip(tok->next->next, "]");
ty = type_suffix(rest, tok, ty);
return array_of(ty, sz);
}

Expand Down
7 changes: 7 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,11 @@ assert 3 'int main() { int x[3]; *x=3; *(x+1)=4; *(x+2)=5; return *x; }'
assert 4 'int main() { int x[3]; *x=3; *(x+1)=4; *(x+2)=5; return *(x+1); }'
assert 5 'int main() { int x[3]; *x=3; *(x+1)=4; *(x+2)=5; return *(x+2); }'

assert 0 'int main() { int x[2][3]; int *y=x; *y=0; return **x; }'
assert 1 'int main() { int x[2][3]; int *y=x; *(y+1)=1; return *(*x+1); }'
assert 2 'int main() { int x[2][3]; int *y=x; *(y+2)=2; return *(*x+2); }'
assert 3 'int main() { int x[2][3]; int *y=x; *(y+3)=3; return **(x+1); }'
assert 4 'int main() { int x[2][3]; int *y=x; *(y+4)=4; return *(*(x+1)+1); }'
assert 5 'int main() { int x[2][3]; int *y=x; *(y+5)=5; return *(*(x+1)+2); }'

echo OK

0 comments on commit 5b2d571

Please sign in to comment.