From 7ad3f400dde57f06ef52c2a9cf788a18d1891b7d Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 15 Jun 2025 03:50:48 +0200 Subject: Remove confusion between function body and function with inline struct as return type Signed-off-by: Mel --- boot/parse.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'boot') diff --git a/boot/parse.c b/boot/parse.c index c06c5d3..9aeaf94 100644 --- a/boot/parse.c +++ b/boot/parse.c @@ -267,7 +267,16 @@ parser_function_header_node(struct Parser* p, struct Parser_Error* error) header.span = span_merge(open_parameters_token.span, close_parameters_token.span); struct Token next = parser_peek(p); - if (token_can_begin_type(&next)) { + struct Token further = parser_peek_further(p); + // NOTE: this is very uncomfortable. to avoid ambiguity between a no-return-type function body + // (i.e. `fun () {`) and a function with a return type of inline + // structure (i.e. `fun () {x int} {`), we require that inline structs are declared in one line, + // without a line-break, and function bodies always have a line-break after the opening brace. + // this means that we cannot have a single line function, and we can't have a function with an + // inline structure as a return type that is too long to fit on a single line. + // TODO: to allow single line functions, introduce a `:` token as a replacement for the + // opening brace. `fun (x int): x + 1` + if (token_can_begin_type(&next) && !token_is(&further, TOKEN_NEWLINE)) { header.return_type = CHECK_RETURN(parser_node_type(p, error), struct Function_Header_Node); header.span = span_merge(header.span, header.return_type->span); } -- cgit 1.4.1