From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id iGyrI4JU8WbwZQAAe85BDQ:P1 (envelope-from ) for ; Mon, 23 Sep 2024 13:44:02 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id iGyrI4JU8WbwZQAAe85BDQ (envelope-from ) for ; Mon, 23 Sep 2024 13:44:02 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=U5vFYwfb; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=dIqVRm7V; dmarc=pass (policy=none) header.from=maniero.me; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1727091842; h=from:from:sender:sender: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:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=ut+CSm2WvTcXnU6W8VnCGru6mzkHyw3hnP+5mqCe3G0=; b=bcpl0sdT9ta/jD/OPL5HD9iY+vOFtxoD8wQuDNIx2Gqxf8/YajMvugNTHOyeyaVrwC7XqY K1rkp26T20WBWs48oLD81KRrv97sQTuLoFHXNH0IRQ/R/6y2UwUQQzNQM3uP3GoEJLV0V6 VtauzQF0QbETLgqwIwZ7tYP8nlInQuIfwclLKCvuGETBI4Ui3JSfAKwhbTv31qAwegLYdz PHR9SqSXZnfYL9YqSg0XnjQX13oMbcycskmeP/91I1isxn1BTGTu1hVidpZ7bBaY24UJzc 0ed4RGx1ytjLK3jOUwq6vrmPBl5/fNYoPLJqK7qPl4v5XCzzRRwz9W2LNbcD4w== ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1727091842; a=rsa-sha256; cv=pass; b=C2YuNY9zMVhegAb63QFmplFEGhtnr2xQ3v/D+WLvjlJkrpqUJZWeRc07GecuKBPi+V1S+w siVF+W/kV44FzwLzupR+ZQjugz9LCwSFb2R7EjL1B77YtOJe3lXnAjQKOznOxro/EDApcY i5DMYnu6KAfgTPV8GyUduj4vcFLItE5ddyH4WdyRrL5uuIJJRaXRMtU7Iv3/ifPk/Z+2kA zoL3If5X47uuAHEu660lZHo1GqT6bWkbowbGt8eajfy2NZAsRL6ZTpFt0zKaA7MA/kB0La K2RVV72FibOyxVfGgYvT9KiLA2l2v3xJT3QY9EmJPWrM2OMBbJE73qvqraAeTA== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=U5vFYwfb; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=dIqVRm7V; dmarc=pass (policy=none) header.from=maniero.me; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1") 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 7C736FFCD for ; Mon, 23 Sep 2024 13:44:01 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=fJLhx+pfexZrkgpM2XKMnkDJcZNpOg17ZuIChe5dATY=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:In-Reply-To:References:Date:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1727091841; v=1; b=U5vFYwfbR48Ty7PcYgm1XTa1KgJ2A11jEh1QMeTq6se4vjIdrV14Bkza8YoJDDyASHKHdQ2F lQNCNSLjdblewokKtb5lMO3mldBo2a+aWBoiWwf8AKD0evl8bzN4B27reO0Kdkq1HAge7M4KjUw pSQuRyE5oTJnZVUm1KCDvKPv6OTGGjSUMkTOTK5nZmU5tfwEjWeXnW8cq4Re3cQAI/3eOPoi2kU pEL7uZM0f5U4jaQ9pQnKLmmW/kT1bmWONN0nW5FQruJbio5x4pqfbevwzp03+N0uYfL9Sk5B7X9 ONXePnZkIFacbEBQWv7SPrByvLIeeoeAtXLD1ak/fFNCg== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 0F751201D7 for ; Mon, 23 Sep 2024 11:44:01 +0000 (UTC) Received: from hamster.birch.relay.mailchannels.net (hamster.birch.relay.mailchannels.net [23.83.209.80]) by mail-a.sr.ht (Postfix) with ESMTPS id E00692015B for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 23 Sep 2024 11:43:56 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 1DEE8C311E for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 23 Sep 2024 11:43:54 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (100-99-147-189.trex-nlb.outbound.svc.cluster.local [100.99.147.189]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 884DCC3182 for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 23 Sep 2024 11:43:53 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1727091833; a=rsa-sha256; cv=none; b=CBESW0SSaH3FmRI3ONH8vpBing42F7+78922oLH/Ta0jM8ExrUeMJU1jS3UF9B2UUKHY+6 /+e7kSBFZOgfUcA0bhzolo5NO5rJ/66PRLK/iS+PpQ12dm0Re+KvE0xZaf5roeBCqFpUmN 47LFAaqnyOqXZmNBELJ9NlKvtA1uRiKgoKctBElCs5C/IlJn85hAR/71UbdzJk6x2wtff5 JWeBKsAWgdrzyA1cGq/qx95Z72PUelM/9X/DhWmBR5Q7WYtfLmIE7R+ho3zbjM2HvzNNnA SlX2QO4bnEEhfnXMK7cF5y6NqO/koq6QDNuhZmRrBW14iAe/4rGIRhYP41qiyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1727091833; 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:dkim-signature; bh=ut+CSm2WvTcXnU6W8VnCGru6mzkHyw3hnP+5mqCe3G0=; b=p10pV/aqrFZmqEhinBEi8LASKCc1nHYKAr15RoLvx+0/46Dr8v3FULMqtw1Clb1Uxd0t+u 2av9kPHW7KdWzkiXFKQ53BoIFrzKPr0lW3arA9Uk5ZoTCPT9/X9WEUHJNvx8AvUEMQMxpR kbs7kWgsUSZ5i8pM3bnlXfzvfVNf6i8zETJx/QTIxtUpn4zynlMTS6pgJ0+AuWPcBweTCG fq6gvFL6PuXtmM/iZ+0uWnQBeXzfglu5PI44aUskPJoc1HCF9YTCv0aQeHkpj34SX77FaW Ysskgq1awpof6vvK8ipufZOZblBo7uzpmZ0fDqOs/AQUwwQhNzQwS5bFAWjEyw== ARC-Authentication-Results: i=1; rspamd-5b46bcd97f-9tw6t; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Squirrel-Cure: 7e8bba917c657dbc_1727091834027_476159601 X-MC-Loop-Signature: 1727091834027:4086055553 X-MC-Ingress-Time: 1727091834026 Received: from nl-srv-smtpout1.hostinger.io (nl-srv-smtpout1.hostinger.io [145.14.150.87]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.99.147.189 (trex/7.0.2); Mon, 23 Sep 2024 11:43:54 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1727091827; 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=ut+CSm2WvTcXnU6W8VnCGru6mzkHyw3hnP+5mqCe3G0=; b=dIqVRm7VGAskPTevA9wLTImU0p0nimQuWhqGjEP4iC/MExAPSkP7vZHVHMoBaAQGSn1foV t1+E2AWXcGWD6fsK3fWUBqBjgULO/LDADkBfYOBjgQocR2qK0x/xMAdMTnOokCKB5FbEdc vZX9FiU6+tqTA22yx1leex6yLjPExqEZaycj5ZseJmLb9HukzMvqAaxQT3QaVJcgd0fCyO s7BtHrSMbFXu99Mg8hNOwuCBr14KZfwIjheJPwknFI99M0R9xg8AgpXwaUyBvOY1w0P8ha QljsIQd6eD7iWf8XdmeE8Za28B0ovqxr1O8p/kvzFoKX5tfcDyl2hSAHHe02dA== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang 2/2] ast: permit multi declarations on translation unit Message-Id: <20240923114330.120923-3-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240923114330.120923-1-carlos@maniero.me> References: <20240923114330.120923-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Mon, 23 Sep 2024 11:43:40 +0000 (UTC) X-CM-Envelope: MS4xfOnJ8xIUvNtp8ewb5I+ryKzh8tdLqZmHfGkt9ECKxLFKPSGdwHMgnmYjKB9itkcZpe4HKV5BAOrVuUQusin5ct57bTpqLiEspkJIRhpdwDgy5B+a+e6J go6o0qC55biJNE/ztkP78BKxgxeBTpM0CDkA06ct+gnklvE71+nNqa8oP52mJOBqNAu+ek8hQT+//HIIc/rdDBfFkjmnDB7PYTZhcwKfJx+dA6zFYblU4LPY X-CM-Analysis: v=2.4 cv=b6rg4cGx c=1 sm=1 tr=0 ts=66f15473 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=yCCFyLwRQ5KtlvuK6IoA:9 a=BXDaF_L80NY05PYiAFlV:22 X-AuthUser: carlos@maniero.me X-Sourcehut-Patchset-Status: PROPOSED 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: -0.16 X-Spam-Score: -0.16 X-Migadu-Scanner: mx13.migadu.com X-Migadu-Queue-Id: 7C736FFCD X-TUID: TWLjrhtZmB2d At this point the parser still parsing only a single function, but the ast is ready to support multiple declarations. Signed-off-by: Carlos Maniero --- src/ast.c | 7 +++++-- src/ast.h | 4 ++-- src/checker.c | 7 ++++++- src/codegen_linux_aarch64.c | 22 +++++++++++++++++++--- src/codegen_linux_x86_64.c | 22 +++++++++++++++++++--- src/parser.c | 6 +++++- src/pretty_print_ast.c | 13 +++++++++++-- tests/unit/parser_test.c | 11 ++++++++--- 8 files changed, 75 insertions(+), 17 deletions(-) diff --git a/src/ast.c b/src/ast.c index f5fa483..bb74679 100644 --- a/src/ast.c +++ b/src/ast.c @@ -23,7 +23,7 @@ #include "string_view.h" ast_node_t * -ast_new_translation_unit(arena_t *arena, ast_node_t *fn_def) +ast_new_translation_unit(arena_t *arena) { ast_node_t *node = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node); @@ -31,7 +31,10 @@ ast_new_translation_unit(arena_t *arena, ast_node_t *fn_def) node->kind = AST_NODE_TRANSLATION_UNIT; ast_translation_unit_t *translation_unit = &node->as_translation_unit; - translation_unit->fn = fn_def; + translation_unit->decls = (list_t *)arena_alloc(arena, sizeof(list_t)); + assert(translation_unit->decls); + + list_init(translation_unit->decls, arena); return node; } diff --git a/src/ast.h b/src/ast.h index 718c80f..6cfbfc0 100644 --- a/src/ast.h +++ b/src/ast.h @@ -48,7 +48,7 @@ typedef struct ast_block typedef struct ast_translation_unit { - ast_node_t *fn; + list_t *decls; } ast_translation_unit_t; typedef struct ast_fn_definition @@ -146,7 +146,7 @@ typedef struct ast_node } ast_node_t; ast_node_t * -ast_new_translation_unit(arena_t *arena, ast_node_t *fn_def); +ast_new_translation_unit(arena_t *arena); ast_node_t * ast_new_node_fn_def(arena_t *arena, string_view_t id, string_view_t return_type, ast_node_t *block); diff --git a/src/checker.c b/src/checker.c index e9bfacb..814d052 100644 --- a/src/checker.c +++ b/src/checker.c @@ -54,7 +54,12 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) { switch (ast->kind) { case AST_NODE_TRANSLATION_UNIT: { - populate_scope(checker, scope, ast->as_translation_unit.fn); + list_item_t *item = list_head(ast->as_translation_unit.decls); + + while (item != NULL) { + populate_scope(checker, scope, (ast_node_t *)item->value); + item = list_next(item); + } return; } diff --git a/src/codegen_linux_aarch64.c b/src/codegen_linux_aarch64.c index e8ae729..d8187ab 100644 --- a/src/codegen_linux_aarch64.c +++ b/src/codegen_linux_aarch64.c @@ -49,10 +49,26 @@ codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *node) assert(node->kind == AST_NODE_TRANSLATION_UNIT); ast_translation_unit_t translation_unit = node->as_translation_unit; - ast_fn_definition_t fn = translation_unit.fn->as_fn_def; + list_item_t *item = list_head(translation_unit.decls); - assert(string_view_eq_to_cstr(fn.id, "main")); - codegen_linux_aarch64_emit_function(out, &fn); + bool main_found = false; + + while (item != NULL) { + ast_node_t *decl = (ast_node_t *)item->value; + + if (decl->kind == AST_NODE_FN_DEF) { + ast_fn_definition_t fn = decl->as_fn_def; + codegen_linux_aarch64_emit_function(out, &fn); + + main_found = main_found || string_view_eq_to_cstr(fn.id, "main"); + } else { + assert(0 && "translation unit only supports function declarations"); + } + + item = list_next(item); + } + + assert(main_found && "main function is required."); } static void diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 37d4575..0173443 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -63,10 +63,26 @@ codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, ast_node_t assert(node->kind == AST_NODE_TRANSLATION_UNIT); ast_translation_unit_t translation_unit = node->as_translation_unit; - ast_fn_definition_t fn = translation_unit.fn->as_fn_def; + list_item_t *item = list_head(translation_unit.decls); - assert(string_view_eq_to_cstr(fn.id, "main")); - codegen_linux_x86_64_emit_function(codegen, &fn); + bool main_found = false; + + while (item != NULL) { + ast_node_t *decl = (ast_node_t *)item->value; + + if (decl->kind == AST_NODE_FN_DEF) { + ast_fn_definition_t fn = decl->as_fn_def; + codegen_linux_x86_64_emit_function(codegen, &fn); + + main_found = main_found || string_view_eq_to_cstr(fn.id, "main"); + } else { + assert(0 && "translation unit only supports function declarations"); + } + + item = list_next(item); + } + + assert(main_found && "main function is required."); } static void diff --git a/src/parser.c b/src/parser.c index 9332f6e..c79f3bd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -80,7 +80,11 @@ parser_parse_translation_unit(parser_t *parser) return NULL; } - return ast_new_translation_unit(parser->arena, fn); + ast_node_t *translation_unit_node = ast_new_translation_unit(parser->arena); + + list_append(translation_unit_node->as_translation_unit.decls, fn); + + return translation_unit_node; } static ast_binary_op_kind_t diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c index db646c5..8116e60 100644 --- a/src/pretty_print_ast.c +++ b/src/pretty_print_ast.c @@ -116,8 +116,17 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) pretty_print_node_t *node = pretty_print_node_new(arena); node->name = "Translation_Unit"; - pretty_print_node_t *fn_node = ast_node_to_pretty_print_node(ast->as_translation_unit.fn, arena); - list_append(node->children, fn_node); + list_item_t *item = list_head(ast->as_translation_unit.decls); + + while (item != NULL) { + ast_node_t *decl = (ast_node_t *)item->value; + + pretty_print_node_t *fn_node = ast_node_to_pretty_print_node(decl, arena); + list_append(node->children, fn_node); + + item = list_next(item); + } + return node; } case AST_NODE_FN_DEF: { diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c index 4e229be..a7c60d1 100644 --- a/tests/unit/parser_test.c +++ b/tests/unit/parser_test.c @@ -46,10 +46,15 @@ parse_translation_unit_test(const MunitParameter params[], void *user_data_or_fi assert_uint(translation_unit_node->kind, ==, AST_NODE_TRANSLATION_UNIT); ast_translation_unit_t translation_unit = translation_unit_node->as_translation_unit; - assert_not_null(translation_unit.fn); - assert_uint(translation_unit.fn->kind, ==, AST_NODE_FN_DEF); - ast_fn_definition_t fn = translation_unit.fn->as_fn_def; + assert_uint(list_size(translation_unit.decls), ==, 1); + + ast_node_t *fn_node = (ast_node_t *)list_head(translation_unit.decls)->value; + + assert_not_null(fn_node); + assert_uint(fn_node->kind, ==, AST_NODE_FN_DEF); + + ast_fn_definition_t fn = fn_node->as_fn_def; assert_memory_equal(fn.id.size, fn.id.chars, "main"); assert_memory_equal(fn.return_type.size, fn.return_type.chars, "u32"); -- 2.34.1