Skip to content

Commit

Permalink
impl sizeof
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 9, 2024
1 parent d9e61e8 commit d137a58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1659,8 +1659,25 @@ void parse_tenary(struct history *history) {
make_exp_node(cond_node, tenary_node, "?");
}

void parse_sizeof(struct history *history) {
expect_keyword("sizeof");
expect_op("(");
struct datatype dtype;
parse_datatype(&dtype);
node_create(&(struct node){
.type = NODE_TYPE_NUMBER,
.llnum = datatype_size(&dtype),
});
expect_sym(')');
}

void parse_keyword(struct history *history) {
struct token *token = token_peek_next();
if (S_EQ(token->sval, "sizeof")) {
parse_sizeof(history);
return;
}

if (is_keyword_variable_modifier(token->sval) ||
keyword_is_datatype(token->sval)) {
parse_variable_function_or_struct_union(history);
Expand Down
7 changes: 6 additions & 1 deletion test.c
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#include <stdarg_internal.h>
struct abc {
int x;
int y;
};

int main() { return sizeof(struct abc); }

0 comments on commit d137a58

Please sign in to comment.