From 4e591092dba65df28557cf77b749609bbd9458d5 Mon Sep 17 00:00:00 2001 From: Yaossg Date: Thu, 12 Dec 2024 23:21:34 +0800 Subject: [PATCH] pos array size --- boot.c | 64 +++++++++++++++++++++++++++++++------------------ test/enum.c | 6 ++--- test/enum.out | 2 +- test/overflow.c | 2 +- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/boot.c b/boot.c index 7c8641d..bd2bdb0 100644 --- a/boot.c +++ b/boot.c @@ -38,27 +38,18 @@ enum { // constants enum { - TOKEN_EOF, - TOKEN_SEMICOLON, + TOKEN_EOF = -1, TOKEN_ADD, TOKEN_SUB, TOKEN_MUL, TOKEN_DIV, TOKEN_REM, - TOKEN_ASSIGN, - TOKEN_COMMA, - TOKEN_LSHIFT, - TOKEN_RSHIFT, TOKEN_AND, TOKEN_OR, TOKEN_XOR, - TOKEN_COMPL, - TOKEN_NOT, - TOKEN_LAND, - TOKEN_LOR, - TOKEN_ELLIPSIS, - TOKEN_INC, - TOKEN_DEC, + TOKEN_LSHIFT, + TOKEN_RSHIFT, + TOKEN_ASSIGN, TOKEN_ADD_ASSIGN, TOKEN_SUB_ASSIGN, TOKEN_MUL_ASSIGN, @@ -69,15 +60,23 @@ enum { TOKEN_XOR_ASSIGN, TOKEN_LSHIFT_ASSIGN, TOKEN_RSHIFT_ASSIGN, + TOKEN_INV, + TOKEN_NOT, + TOKEN_LAND, + TOKEN_LOR, + TOKEN_INC, + TOKEN_DEC, TOKEN_QUESTION, TOKEN_COLON, + TOKEN_SEMICOLON, + TOKEN_COMMA, + TOKEN_ELLIPSIS, TOKEN_EQ, TOKEN_NE, TOKEN_LT, TOKEN_GT, TOKEN_LE, TOKEN_GE, - TOKEN_PAREN_LEFT = 50, TOKEN_PAREN_RIGHT, TOKEN_BRACKET_LEFT, @@ -544,7 +543,7 @@ void next_token() { token_type = TOKEN_XOR; } } else if (ch == '~') { - token_type = TOKEN_COMPL; + token_type = TOKEN_INV; } else if (ch == '\'') { token_type = TOKEN_NUMBER; token_data = getchar(); @@ -641,14 +640,31 @@ char is_const[ID_LUT_SIZE]; int expect_const() { next_token(); + int value = 1; + if (token_type == TOKEN_ADD) { + next_token(); + } else if (token_type == TOKEN_SUB) { + value = -1; + next_token(); + } if (token_type == TOKEN_NUMBER) { - return token_data; + value *= token_data; + } else if (token_type == TOKEN_ID && !local_table[token_data] && is_const[token_data]) { + value *= const_table[token_data]; + } else { + fprintf(stderr, "expecting a constant\n"); + exit(1); } - if (token_type == TOKEN_ID && !local_table[token_data] && is_const[token_data]) { - return const_table[token_data]; + return value; +} + +int expect_array_size() { + int size = expect_const(); + if (size <= 0) { + fprintf(stderr, "array size must be positive\n"); + exit(1); } - fprintf(stderr, "expecting a constant\n"); - exit(1); + return size; } void reset_reg() { @@ -1272,10 +1288,12 @@ int parse_prefix_expr() { exit(1); } return dereference(reg); + } else if (token_type == TOKEN_ADD) { + return parse_prefix_expr(); } else if (token_type == TOKEN_SUB) { int reg = parse_prefix_expr(); return asm_r_arith("neg", reg); - } else if (token_type == TOKEN_COMPL) { + } else if (token_type == TOKEN_INV) { int reg = parse_prefix_expr(); return asm_r_arith("not", reg); } else if (token_type == TOKEN_NOT) { @@ -1620,7 +1638,7 @@ void parse_local_variable(int type) { fprintf(stderr, "array of pointers is not supported\n"); exit(1); } - int size = expect_const(); + int size = expect_array_size(); expect_token(TOKEN_BRACKET_RIGHT); declare_local_array(id, type, size); return; @@ -1886,7 +1904,7 @@ void parse_global_variable(int id, char* name, int type) { fprintf(stderr, "array of pointers is not supported\n"); exit(1); } - int size = expect_const(); + int size = expect_array_size(); expect_token(TOKEN_BRACKET_RIGHT); int array_size = array_size_of(type, size); printf(" .zero %d\n", array_size); diff --git a/test/enum.c b/test/enum.c index 7b53339..f285543 100644 --- a/test/enum.c +++ b/test/enum.c @@ -1,8 +1,8 @@ enum { - A, B, C = 1, D, E = A, F, + A, B, C = -B, D, E = +1, F, }; int main() { - return A + B + C + D + E + F; - // 0 + 1 + 1 + 2 + 0 + 1 = 5 + return A + B + C + D + E + F; + // 0 + 1 + -1 + 0 + 1 + 2 = 3 } \ No newline at end of file diff --git a/test/enum.out b/test/enum.out index 7ed6ff8..00750ed 100644 --- a/test/enum.out +++ b/test/enum.out @@ -1 +1 @@ -5 +3 diff --git a/test/overflow.c b/test/overflow.c index 1d12c64..6e4019d 100644 --- a/test/overflow.c +++ b/test/overflow.c @@ -37,5 +37,5 @@ int main() { char placeholder[4096]; int a = 1; dummy((a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(! !0))))))))))))))))))))), 3, a, a, a, a, a, a); - return (a=(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(~ ~0)))))))))))))))))))))), (a = a); + return (a=(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(~ ~0)))))))))))))))))))))), (a = +a); }