diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-05 00:58:16 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-05 08:56:15 -0500 |
| commit | 3da421417167d30ef6291342b19b08419049faae (patch) | |
| tree | 0b7e685b25f8d4cf64db32e09d6c52b3951006fa /src | |
| parent | Add ability to initialize global variables (diff) | |
| download | cup-3da421417167d30ef6291342b19b08419049faae.tar.xz cup-3da421417167d30ef6291342b19b08419049faae.zip | |
Add `sizeof(<type>)` operator that can return the size of a type.
Diffstat (limited to 'src')
| -rw-r--r-- | src/parser.c | 8 | ||||
| -rw-r--r-- | src/tokens.h | 1 |
2 files changed, 9 insertions, 0 deletions
diff --git a/src/parser.c b/src/parser.c index 81c90da..99926d0 100644 --- a/src/parser.c +++ b/src/parser.c @@ -541,6 +541,14 @@ Node *parse_factor(Lexer *lexer) Lexer_next(lexer); expr = parse_expression(lexer); assert_token(Lexer_next(lexer), TOKEN_CLOSE_PAREN); + + // FIXME: This should probably go somewhere else more appropriate + } else if (token.type == TOKEN_SIZEOF) { + Lexer_next(lexer); + assert_token(Lexer_next(lexer), TOKEN_OPEN_PAREN); + Type *type = parse_type(lexer); + assert_token(Lexer_next(lexer), TOKEN_CLOSE_PAREN); + expr = Node_from_int_literal(size_for_type(type)); } else if (is_literal_token(token.type)) { expr = parse_literal(lexer); } else if (token.type == TOKEN_IDENTIFIER) { diff --git a/src/tokens.h b/src/tokens.h index d0f216a..3fe5719 100644 --- a/src/tokens.h +++ b/src/tokens.h @@ -59,6 +59,7 @@ F(TOKEN_LET, "let") \ F(TOKEN_RETURN, "return") \ F(TOKEN_STRUCT, "struct") \ + F(TOKEN_SIZEOF, "sizeof") \ F(TOKEN_UNION, "union") \ F(TOKEN_VOID, "void") \ F(TOKEN_WHILE, "while") \ |