Skip to content

Commit

Permalink
Merge pull request #125 from adamhutchings/clink
Browse files Browse the repository at this point in the history
Add empty function bodies
  • Loading branch information
adamhutchings authored Nov 13, 2022
2 parents 53cdf99 + 70f6771 commit 17f2814
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
6 changes: 5 additions & 1 deletion src/api/cst-dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ static void yf_dump_funcdecl(struct yfcs_funcdecl * node, FILE * out) {
yf_print_line(out, "return type: %s", node->ret.databuf);
yf_print_line(out, "function body");
indent();
yf_dump_cst(node->body, out);
if (node->body) {
yf_dump_cst(node->body, out);
} else {
yf_print_line(out, "[no body]");
}
dedent();
yf_print_line(out, "end function body");
dedent();
Expand Down
5 changes: 4 additions & 1 deletion src/gen/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ static void yf_gen_funcdecl(

fprintf(out, ") ");

yf_gen_node(node->body, out, i);
if (node->body == NULL)
fprintf(out, ";");
else
yf_gen_node(node->body, out, i);

}

Expand Down
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
1 change: 1 addition & 0 deletions tests/sem/func-forward.yf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo(x: int): int;

0 comments on commit 17f2814

Please sign in to comment.