Skip to content

Commit

Permalink
Add frontend for empty function bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhutchings committed Nov 13, 2022
1 parent a1756b5 commit 70f6771
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
11 changes: 11 additions & 0 deletions src/parser/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
41 changes: 24 additions & 17 deletions src/semantics/validate/validate-func.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 70f6771

Please sign in to comment.