void check

This commit is contained in:
Yaossg 2024-11-17 17:23:30 +08:00
parent e97820d7ef
commit fb54211c71
2 changed files with 22 additions and 1 deletions

View File

@ -62,6 +62,7 @@ $ sh boot.sh
- 函数名字本身建议仅用于直接调用,其它行为可视为 UB。 - 函数名字本身建议仅用于直接调用,其它行为可视为 UB。
- 数组只支持一维数组,且数组的元素不能是指针类型。 - 数组只支持一维数组,且数组的元素不能是指针类型。
- 全局变量不能是指针类型。 - 全局变量不能是指针类型。
- 整数和字符字面量的类型是 `int`,字符串字面量的类型是 `char*`
### 支持的流程控制 ### 支持的流程控制

22
boot.c
View File

@ -1080,6 +1080,10 @@ int parse_expr() {
} }
void parse_local_variable(int type) { void parse_local_variable(int type) {
if (type == TYPE_VOID) {
eprintf("local variable of void type is not supported\n");
exit(1);
}
expect_token(TOKEN_ID); expect_token(TOKEN_ID);
int id = token_data; int id = token_data;
next_token(); next_token();
@ -1252,7 +1256,19 @@ void parse_function(const char* name) {
expect_token(TOKEN_PAREN_RIGHT); expect_token(TOKEN_PAREN_RIGHT);
break; break;
} }
if (token_type == TOKEN_VOID) {
if (arg != 0) {
eprintf("void should be the only argument\n");
exit(1);
}
expect_token(TOKEN_PAREN_RIGHT);
break;
}
int arg_type = parse_type(); int arg_type = parse_type();
if (arg_type < 0 || arg_type == TYPE_VOID) {
eprintf("unexpected a non-void argument type");
exit(1);
}
expect_token(TOKEN_ID); expect_token(TOKEN_ID);
int arg_name = token_data; int arg_name = token_data;
next_token(); next_token();
@ -1322,6 +1338,10 @@ void parse_function(const char* name) {
} }
void parse_global_variable(int id, const char* name, int type) { void parse_global_variable(int id, const char* name, int type) {
if (type == TYPE_VOID) {
eprintf("global variable of void type is not supported\n");
exit(1);
}
if (type & TYPE_PTR_MASK) { if (type & TYPE_PTR_MASK) {
eprintf("global variable of pointer is not supported\n"); eprintf("global variable of pointer is not supported\n");
exit(1); exit(1);
@ -1353,7 +1373,7 @@ void parse_global_variable(int id, const char* name, int type) {
void parse_global_declaration() { void parse_global_declaration() {
int type = parse_type(); int type = parse_type();
if (type < 0) { if (type < 0) {
eprintf("unexpected token: %d\n", token_type); eprintf("expecting type for global declaration\n");
exit(1); exit(1);
} }
expect_token(TOKEN_ID); expect_token(TOKEN_ID);