pos array size

This commit is contained in:
Yaossg 2024-12-12 23:21:34 +08:00
parent 5784a90d7e
commit 4e591092db
4 changed files with 46 additions and 28 deletions

64
boot.c
View File

@ -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;
}
if (token_type == TOKEN_ID && !local_table[token_data] && is_const[token_data]) {
return const_table[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);
}
return value;
}
int expect_array_size() {
int size = expect_const();
if (size <= 0) {
fprintf(stderr, "array size must be positive\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);

View File

@ -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
// 0 + 1 + -1 + 0 + 1 + 2 = 3
}

View File

@ -1 +1 @@
5
3

View File

@ -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);
}