aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.c
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-05-07 23:37:41 +0900
committernsfisis <nsfisis@gmail.com>2025-05-07 23:37:41 +0900
commit4f88c6b1b511f2e84b0a0256b032c3e9764bcc3e (patch)
tree635d1f35d567ecd8e46f3a1aacaa0c25cce6526e /main.c
parentd33eb250f566dafda9cfe0019c8b61f3b25cbbf4 (diff)
downloadducc-4f88c6b1b511f2e84b0a0256b032c3e9764bcc3e.tar.gz
ducc-4f88c6b1b511f2e84b0a0256b032c3e9764bcc3e.tar.zst
ducc-4f88c6b1b511f2e84b0a0256b032c3e9764bcc3e.zip
support func params without names
Diffstat (limited to 'main.c')
-rw-r--r--main.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/main.c b/main.c
index 2ba5f7a..f0ec654 100644
--- a/main.c
+++ b/main.c
@@ -1,16 +1,16 @@
-int atoi();
-void* calloc();
-void exit();
-int getchar();
-int isalnum();
-int isalpha();
-int isdigit();
-int isspace();
-void* memcpy();
+int atoi(char*);
+void* calloc(long, long);
+void exit(int);
+int getchar(void);
+int isalnum(int);
+int isalpha(int);
+int isdigit(int);
+int isspace(int);
+void* memcpy(void*, void*, long);
int printf();
int sprintf();
-int strcmp();
-char* strstr();
+int strcmp(char*, char*);
+char* strstr(char*, char*);
#define NULL 0
@@ -1345,10 +1345,11 @@ void register_func(struct Parser* p, char* name, struct Type* ty) {
struct AstNode* parse_param(struct Parser* p) {
struct Type* ty = parse_type(p);
- if (!type_is_unsized(ty)) {
- fatal_error("parse_param: invalid type for variable");
+ char* name = NULL;
+ enum TokenKind tk = peek_token(p)->kind;
+ if (tk != TK_COMMA && tk != TK_PAREN_R) {
+ name = parse_ident(p);
}
- char* name = parse_ident(p);
struct AstNode* param = ast_new(AST_PARAM);
param->ty = ty;
param->name = name;
@@ -1356,9 +1357,11 @@ struct AstNode* parse_param(struct Parser* p) {
}
struct AstNode* parse_param_list(struct Parser* p) {
+ int has_void = 0;
struct AstNode* list = ast_new_list(6);
while (peek_token(p)->kind != TK_PAREN_R) {
struct AstNode* param = parse_param(p);
+ has_void = has_void || param->ty->kind == TY_VOID;
ast_append(list, param);
if (peek_token(p)->kind == TK_COMMA) {
next_token(p);
@@ -1369,6 +1372,12 @@ struct AstNode* parse_param_list(struct Parser* p) {
if (list->node_len > 6) {
fatal_error("too many parameters");
}
+ if (has_void) {
+ if (list->node_len != 1) {
+ fatal_error("invalid use of void param");
+ }
+ list->node_len = 0;
+ }
return list;
}