From 70f67710416e3471b8e0f17734da698002b77632 Mon Sep 17 00:00:00 2001 From: adamhutchings Date: Sat, 12 Nov 2022 23:52:24 -0500 Subject: [PATCH] Add frontend for empty function bodies --- src/parser/func.c | 11 +++++++ src/semantics/validate/validate-func.c | 41 +++++++++++++++----------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/parser/func.c b/src/parser/func.c index 0270a75..c04c4aa 100644 --- a/src/parser/func.c +++ b/src/parser/func.c @@ -88,6 +88,17 @@ int yfp_funcdecl(struct yf_parse_node * node, struct yf_lexer * lexer) { bodyp: node->type = YFCS_FUNCDECL; + + struct yf_token sctest; /* semicolon test */ + yfl_lex(lexer, &sctest); + if (sctest.type == YFT_SEMICOLON) { + /* No body for function. */ + node->funcdecl.body = NULL; + return 0; + } else { + yfl_unlex(lexer, &sctest); + } + node->funcdecl.body = yf_malloc(sizeof (struct yf_parse_node)); return yfp_bstmt(node->funcdecl.body, lexer); diff --git a/src/semantics/validate/validate-func.c b/src/semantics/validate/validate-func.c index 1195d8c..ea3679c 100644 --- a/src/semantics/validate/validate-func.c +++ b/src/semantics/validate/validate-func.c @@ -67,29 +67,36 @@ int validate_funcdecl( return 1; } - a->body = yf_malloc(sizeof (struct yf_ast_node)); - if (!a->body) - return 2; - - /* Now, validate the body. */ - if (validate_bstmt(validator, c->body, a->body, a->ret, &returns)) { - yf_free(a->body); + if (c->body == NULL) { a->body = NULL; - return 1; + } else { + a->body = yf_malloc(sizeof (struct yf_ast_node)); + if (!a->body) + return 2; + + /* Now, validate the body. */ + if (validate_bstmt(validator, c->body, a->body, a->ret, &returns)) { + yf_free(a->body); + a->body = NULL; + return 1; + } } + /* Close the scope. */ exit_scope(validator); - if (returns == 0 && a->ret->primitive.size != 0) { - YF_PRINT_ERROR( - "%s %d:%d: Function '%s' does not always return a value", - cin->loc.file, - cin->loc.line, - cin->loc.column, - c->name.name - ); - return 1; + if (a->body != NULL) { + if (returns == 0 && a->ret->primitive.size != 0) { + YF_PRINT_ERROR( + "%s %d:%d: Function '%s' does not always return a value", + cin->loc.file, + cin->loc.line, + cin->loc.column, + c->name.name + ); + return 1; + } } return 0;