From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id IHSgBxtVEGdauwAAqHPOHw:P1 (envelope-from ) for ; Thu, 17 Oct 2024 02:06:51 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id IHSgBxtVEGdauwAAqHPOHw (envelope-from ) for ; Thu, 17 Oct 2024 02:06:51 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; none Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 092403412C for ; Thu, 17 Oct 2024 02:06:51 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=toCjGr/gFNR/yhZbydhCFhZVq1Jl+cth02tFmWZaJew=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1729123610; v=1; b=ed2k9pM5W+3IYH3S8/is5NvXUolJj2usdr418nrQHseWyJt/ZFocVQZzhWUm7a9guMSSIePl KtnxCBbgfORC6LLoO1fd2ossxbLeLpNcjSkErpmlIr7qBWM/wmqqcYlBikWa/oA+NJrTZalUSiw xmLDSXmSBkKTqfnm0uJp1XVpv0CFXR6tJawyxoCZ0EQ6yv6pxPRwY/TtVGRCo6wNYwVddefXVe4 I8k09HcFYBOHOs7oe58kAGqh06siiX3UzQHZFRwrhkBmgnjrryi8gJC2Eny3HCzwTWGI6udWjwx AMWWMmzBK7AcKW60j/CmU55+Y45BtD6Blpjaz6o7GDD5w== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 3FFD5202E8 for ; Thu, 17 Oct 2024 00:06:50 +0000 (UTC) Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) by mail-a.sr.ht (Postfix) with ESMTPS id A73EF20272 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 17 Oct 2024 00:06:49 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1729123609; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LUxa890VYWyCYWJSgdPIigcKK29qoG0jo54w8T+MDNM=; b=MuRJ9OrRzz7OnV1R3z1KvaKveklmjnB2jXZ1zKiRJB2qsxeJnIc+ETHRjG9u7akoobt3C8 +yXKweExBWsroGVzke1EkA5LG1wVB9b7HAw2+ueRfEaE4He9y6rODJ2bQOhWrf15HJjlDN WztnkiCYxlYl0QQfc/cnCJMmxWIoC3AigiyBufxYrOgDXN7AjvUz+SNduxVOzQU9PWvA2S 4eUaCjPUWIZnJ+iQdPYhHrWhsL8fXUw6DISs0HcE6Ykj3rgOlry6vk4a7j9pk8wnei7OZn imNeAdiK6FmA7O+CSwPMbgfKE2ZewogCIxQZCgpt34LbX4lFPJI8SsmfICgkMw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 2/3] parser: support extern function definition Date: Thu, 17 Oct 2024 04:04:43 +0200 Message-ID: <20241017020603.551431-3-johnny@johnnyrichard.com> In-Reply-To: <20241017020603.551431-1-johnny@johnnyrichard.com> References: <20241017020603.551431-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Sourcehut-Patchset-Status: UNKNOWN List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 092403412C X-Migadu-Scanner: mx10.migadu.com X-TUID: dR/wTN+fWXzQ Signed-off-by: Johnny Richard --- src/ast.c | 3 ++- src/ast.h | 2 ++ src/parser.c | 20 +++++++++++++++++--- src/pretty_print_ast.c | 14 +++++++++----- tests/olc/0037_variable_overflow.ol | 2 +- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/ast.c b/src/ast.c index 800239e..d0c6b37 100644 --- a/src/ast.c +++ b/src/ast.c @@ -45,11 +45,11 @@ ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, type_t *return_type, + bool _extern, ast_node_t *block) { assert(arena); assert(params); - assert(block); ast_node_t *node_fn_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); @@ -61,6 +61,7 @@ ast_new_node_fn_def(arena_t *arena, fn_def->id = id; fn_def->return_type = return_type; + fn_def->_extern = _extern; fn_def->block = block; fn_def->params = params; diff --git a/src/ast.h b/src/ast.h index 217a264..e7d8ed8 100644 --- a/src/ast.h +++ b/src/ast.h @@ -75,6 +75,7 @@ typedef struct ast_fn_definition string_view_t id; list_t *params; type_t *return_type; + bool _extern; ast_node_t *block; scope_t *scope; } ast_fn_definition_t; @@ -226,6 +227,7 @@ ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, type_t *return_type, + bool _extern, ast_node_t *block); ast_node_t * diff --git a/src/parser.c b/src/parser.c index 35cf017..d26f266 100644 --- a/src/parser.c +++ b/src/parser.c @@ -467,6 +467,16 @@ parser_parse_fn_params(parser_t *parser) ast_node_t * parser_parse_fn_definition(parser_t *parser) { + bool _extern = false; + + token_t _extern_token; + lexer_peek_next(parser->lexer, &_extern_token); + + if (_extern_token.kind == TOKEN_EXTERN) { + _extern = true; + skip_next_token(parser); + } + if (!skip_expected_token(parser, TOKEN_FN)) { return NULL; } @@ -494,9 +504,12 @@ parser_parse_fn_definition(parser_t *parser) skip_line_feeds(parser->lexer); - ast_node_t *block = parser_parse_block(parser); - if (block == NULL) { - return NULL; + ast_node_t *block = NULL; + if (!_extern) { + block = parser_parse_block(parser); + if (block == NULL) { + return NULL; + } } return ast_new_node_fn_def(parser->arena, @@ -504,6 +517,7 @@ parser_parse_fn_definition(parser_t *parser) fn_name_token.value, params, ret_type, + _extern, block); } diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c index d2164eb..d476370 100644 --- a/src/pretty_print_ast.c +++ b/src/pretty_print_ast.c @@ -155,9 +155,11 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) char name[256]; sprintf(name, - "Function_Definition ", + "Function_Definition %s", SV_ARG(fn_def.id), - SV_ARG(fn_def.return_type->id)); + SV_ARG(fn_def.return_type->id), + fn_def._extern ? " " : ""); node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); @@ -169,9 +171,11 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) param = list_next(param); } - pretty_print_node_t *block = - ast_node_to_pretty_print_node(fn_def.block, arena); - list_append(node->children, block); + if (fn_def.block != NULL) { + pretty_print_node_t *block = + ast_node_to_pretty_print_node(fn_def.block, arena); + list_append(node->children, block); + } return node; } case AST_NODE_FN_CALL: { diff --git a/tests/olc/0037_variable_overflow.ol b/tests/olc/0037_variable_overflow.ol index 0e3c302..2b7c8c2 100644 --- a/tests/olc/0037_variable_overflow.ol +++ b/tests/olc/0037_variable_overflow.ol @@ -23,7 +23,7 @@ fn main(): u32 { # # XTEST test_run_binary(exit_code=0) # -# XTEST test_ast WITH +# TEST test_ast WITH # Translation_Unit # |-Function_Definition # | `-Param_Definition -- 2.46.0