diff options
| author | 2022-08-06 05:25:16 +0200 | |
|---|---|---|
| committer | 2022-08-06 05:25:16 +0200 | |
| commit | 3083cf126cf3989b257546aa3b1b69608aac7923 (patch) | |
| tree | ae6b62edb201d7cbcfe4b1fed528bf875b9919b1 | |
| parent | b8f676ae7ffe12877d013c5256a785d35915c491 (diff) | |
sizeof
| -rw-r--r-- | bootstrap/all.h | 1 | ||||
| -rw-r--r-- | bootstrap/parse.c | 11 | ||||
| -rw-r--r-- | bootstrap/test.cff | 4 |
3 files changed, 16 insertions, 0 deletions
diff --git a/bootstrap/all.h b/bootstrap/all.h index 33ac7e7..15f932c 100644 --- a/bootstrap/all.h +++ b/bootstrap/all.h @@ -48,6 +48,7 @@ struct span { _(not) \ _(or) \ _(return) \ + _(sizeof) \ _(static) \ _(struct) \ _(switch) \ diff --git a/bootstrap/parse.c b/bootstrap/parse.c index d55339d..4372cdc 100644 --- a/bootstrap/parse.c +++ b/bootstrap/parse.c @@ -922,6 +922,17 @@ pexprimary(struct parser *P) { ex = parsestructini(P, P->targty); else if (P->targty->t == TYarr) ex = parsearrini(P, P->targty); + } else if (lexmatch(P, &tok, TKkw_sizeof)) { + ex.t = Eintlit; + ex.ty = ty_usize; + if (lexmatch(P, &tok, '(')) { + struct expr exp = parseexpr(P); + ex.i = exp.ty->size; + lexexpect(P, ')'); + } else { + const struct type *ty = parsetype(P); + ex.i = ty->size; + } } else { experr: fatal(P, tok.span, "expected expression (near %s)", tok2str(tok)); diff --git a/bootstrap/test.cff b/bootstrap/test.cff index fb1741d..011d386 100644 --- a/bootstrap/test.cff +++ b/bootstrap/test.cff @@ -46,5 +46,9 @@ extern fn main (argc int, argv **u8) int { printf("%d\n", is[i]); } + printf("sizeof(is) = %zu\n", sizeof(is)); + printf("sizeof *void = %zu\n", sizeof *void); + + return 0; } |