From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms1.migadu.com with LMTPS id iB3ZIdP1Ime0MwEAqHPOHw:P1 (envelope-from ) for ; Thu, 31 Oct 2024 04:13:23 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id iB3ZIdP1Ime0MwEAqHPOHw (envelope-from ) for ; Thu, 31 Oct 2024 04:13:23 +0100 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=b8ZkZDMd; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=EZ62KL9T; 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-Seal: i=2; s=key1; d=johnnyrichard.com; t=1730344403; a=rsa-sha256; cv=pass; b=rjgJIqFf5AzuP0NiIZThwa6Lqqwf+Rf42Thm9fM9q/l/GpWonswyVOPYp07eP2aJ6Rxs+T 9NJTXx7ngtmubIRx/3B9ks1E/jpGdpHcdaISSxjz9gO6zfhUYGoow/U+hP33qTz0m9CtgD SadKerZWi0WFH4tb/u3h0p04BTOckTstVU+hAyoL/g0gFpcBdq1zVqGbqrn9okY8lYNlTA KqJZKSM0d5ZPi7IiXlhGE4ti2vduHZ/sMhKHL5WsB4j3WA502gWERu9AUa4d9iPdZB9djC +GyO7pdJHrmtmVTqWPSexweDjWP5KjkNHZQ7Aje2VcebhFB3Hs7GUZC+GO5HQA== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=b8ZkZDMd; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=EZ62KL9T; 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=1730344403; 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=r+ZPpOsKs4Zb3YqDwfVtLHv8bRDQ8Dy2UVgJ66mZbOc=; b=viT2b4x/u+rHA/gSFnk52Dn5QGp8XGF/crtw2JlzuCLUhYCJ8d7xVK2uyfB4aH7lrZKMd+ ji2OohATwryqOo3NDCAHHdoNo3F+xk/jK5Gxnqiz30A0u+nySzNADjCA530JfpPqwUtiGM GEfJ+OsaLlI94FWDXxYUK2rR9HttVC/iZ+gBFcBJ3oB6c4pbsA9Jo+YQVyk/cHcMK5dVSQ z/SlhEA7OQsnnM0lvIt7WQ3T1rBJjqRIj6jRzf1bSGGPXv9r6Dc4G6yULQ4kdJrrPs4WFJ BSelCPIbA0vE5mhHeQSqIsiV1SP/fyEt9rK88lmzLRabCFa3hyNpi+aXvlOtsg== 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 3A43F1E8C for ; Thu, 31 Oct 2024 04:13:20 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=Ksi4G/fLLvLsFeduvn2FJvhCWqd6roW+GfC1AIJHoFg=; 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=1730344400; v=1; b=b8ZkZDMdkd7fHAOoHkVKQMVzItT0Y+k+tanAxkTvo0wAo1yFR30k2AXPcW7FWON5oyjBq9kW 4vrMzABce9dZFEGoQBeSHUlXydIenBtJZrmIN/+UmtEdEBA86+BR2Uu3mxsDOiY3asQo14q/2hx y0hhOAhQ6cZg71KYAYBwYKrDsUirXzG272LDRcyP08HfjfVdwCaAZynz/CUtqS2Nf4mTkt9aDzn 4nuyXLmaNr0hXMYYLkTP5C9E3x2hjnrURyXe9lWvHj57ykXME0wADkdYcEe5zwWi6LCR81IL5lY 2T1u/l6+FN8KSU0svLIFkhCMxuFxHa4bbKqrkR11AYUIg== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id ED71C20383 for ; Thu, 31 Oct 2024 03:13:19 +0000 (UTC) Received: from weasel.tulip.relay.mailchannels.net (weasel.tulip.relay.mailchannels.net [23.83.218.247]) by mail-a.sr.ht (Postfix) with ESMTPS id 90F3D20383 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 31 Oct 2024 03:13:15 +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 BDC551A2E57 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 31 Oct 2024 03:13:13 +0000 (UTC) Received: from fr-int-smtpout5.hostinger.io (100-103-220-112.trex-nlb.outbound.svc.cluster.local [100.103.220.112]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id E7B261A2E0C for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 31 Oct 2024 03:13:12 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1730344393; a=rsa-sha256; cv=none; b=ZHBNYezClsH4CMnKA/jvoaPmlbFojSCKLiJLxUPrONL8zpM1slSkOhIz/fx6JNRXo0fN8F SdS4XnpEJ8KVWXIj2pPCvRGn/TLjStjASAAivJ043RT9wswZdLCY16pCQvgER1ycxa8jDs CCg59pIq2Nh3fpJvEhhMQreCDo8hewu2RGjZoIg+4VNswMFdv1qf1NfBOLQCtX0TWIAlLx d6bUOrFP0iPYPEHG/ZeK9djdD//vOMutmzUlwgHyxs5MRE640W15Zpj2ICANfVGI8fxjpm Zc9Ygq9rJBIKhjr2WqB2h+HOSxR/CRgN6As8eVbzSTc3ISKJ/D1ZT+GauOnS1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1730344393; 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=r+ZPpOsKs4Zb3YqDwfVtLHv8bRDQ8Dy2UVgJ66mZbOc=; b=V0sT0c+SM9+VS0VdwTAjfK39+MId5m9zabVMYgckBDj79bZqEk9mU5kUbErYuFBELn0xFy O6iMnxiHqQebrYwtNvlIRZnteQx1hCB3wJ6bTKw9s7ztqdOU4qNDhZsH4jmSFkb3caQ2hB ZszjnTRUZ37r3C8/0Ut0CojYUMzlvzk3m/0brPn+VRHg39EY8z+2d74SUPz4UnVZ3Qd7RA 4pG8AhHhSnlnO4oNtoK4/YeluvgoZBBq3lHA1a8hBwOHT29hhwvRx3kZpJF6Boho3yKyZJ A7e7dM4bVALUycbdumH9WhUxiWZrPJ5XaRrXB7SPB1LDA4N8nk7/x61QdF8bSw== ARC-Authentication-Results: i=1; rspamd-65cf4487d9-sk7bb; 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-Lyrical-Towering: 4232374d12eb3336_1730344393459_600900997 X-MC-Loop-Signature: 1730344393459:1979455885 X-MC-Ingress-Time: 1730344393459 Received: from fr-int-smtpout5.hostinger.io (fr-int-smtpout5.hostinger.io [89.116.146.168]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.103.220.112 (trex/7.0.2); Thu, 31 Oct 2024 03:13:13 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1730344391; 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=r+ZPpOsKs4Zb3YqDwfVtLHv8bRDQ8Dy2UVgJ66mZbOc=; b=EZ62KL9T9zTrDu/YbbZpUdHKIUmSkekxaBsisuICF+7BOOcfhbeDfE7kMuMOKezHW6TPN9 8+I1yUKYvU1gJmzXRR3YLpRsGGE+b8XQF06mDKn160nAljKOHKYdCbCq6PgDGd4gdEHbC2 bUEFFwQzsUcDkSai6dbYrOIGjZHhRllm81/1jf0zK8UxLnFilpFWDCxzTBfyWwKlmH3vat vGlBRiLsth8asHlGr/STkZDOanpeT2B9aIsR7iwp+MGTKqVr1m6SrDyOOenCkke6m40a4y t1aqv/Hfv5hPnVjECdNkQ2/GKRjO1P3AcQqmXvcIBz2drtQIAu7YU23iTowBOg== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v2 1/6] ast: create the ast_ident_t and apply it to var_def and ref Message-ID: <20241031031302.136553-2-carlos@maniero.me> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241031031302.136553-1-carlos@maniero.me> References: <20241031031302.136553-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Thu, 31 Oct 2024 03:13:10 +0000 (UTC) X-CM-Envelope: MS4xfGCcVTRp9HgN94TXB1gIiSrjCOYNvMD+jbzfDpzncDCGWevMVIvbg9dR4+7YyZVKEa12HZZcStx695Xl51kyTa6fzZyXmpRIAaqj6Up6uZycGdkq7lfN xcT0UtT7AY7HDAanVrccuIO9hT4ebcdE4wzHFhl/K2ai9h6eN1ohvbN4eHS1/rW3QKfN8TjhuBTzRKKy593nw5ooWs2BB3ZnJuTMyvnPahAZ0YGFzFVj49qd X-CM-Analysis: v=2.4 cv=Z6G+H2RA c=1 sm=1 tr=0 ts=6722f5c6 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=NGT_zBBPsxoQ-nyd:21 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=m1E6NSZHyzKGdRhk6bMA:9 a=BXDaF_L80NY05PYiAFlV:22 X-AuthUser: carlos@maniero.me 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-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Spam-Score: -0.72 X-Spam-Score: -0.72 X-Migadu-Queue-Id: 3A43F1E8C X-Migadu-Scanner: mx12.migadu.com X-TUID: NWc6TpYGq5wi The ast_ident_t is a base structure that will be used for symbol resolution. Instead of having the id string_view and the scope in all nodes that need to be added to or resolved in the symbol table, these fields were added into a single struct, this will help to remove the redundancy into the semantics level which will lock to a common structure for all nodes. Signed-off-by: Carlos Maniero --- src/ast.c | 6 +++--- src/ast.h | 14 +++++++++----- src/codegen_x86_64.c | 14 ++++++++------ src/pretty_print_ast.c | 5 +++-- src/type_checker.c | 22 +++++++++++++++------- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/ast.c b/src/ast.c index b96a463..7bb5277 100644 --- a/src/ast.c +++ b/src/ast.c @@ -106,7 +106,7 @@ ast_new_node_var_def(arena_t *arena, node_var_def->loc = loc; ast_var_definition_t *var_def = &node_var_def->as_var_def; - var_def->id = id; + var_def->id.name = id; var_def->type = type; var_def->value = value; @@ -167,14 +167,14 @@ ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value) } ast_node_t * -ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id) +ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t ident_name) { ast_node_t *node_ref = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_ref); node_ref->kind = AST_NODE_REF; node_ref->loc = loc; - node_ref->as_ref.id = id; + node_ref->as_ref.ident.name = ident_name; return node_ref; } diff --git a/src/ast.h b/src/ast.h index 7c5e9af..33496a8 100644 --- a/src/ast.h +++ b/src/ast.h @@ -68,6 +68,12 @@ typedef struct ast_block list_t *nodes; } ast_block_t; +typedef struct ast_ident +{ + string_view_t name; + scope_t *scope; +} ast_ident_t; + typedef struct ast_translation_unit { AST_NODE_HEAD; @@ -102,10 +108,9 @@ typedef struct ast_fn_call typedef struct ast_var_definition { AST_NODE_HEAD; - string_view_t id; + ast_ident_t id; type_t *type; ast_node_t *value; - scope_t *scope; } ast_var_definition_t; typedef enum @@ -126,8 +131,7 @@ typedef struct ast_literal typedef struct ast_ref { AST_NODE_HEAD; - string_view_t id; - scope_t *scope; + ast_ident_t ident; } ast_ref_t; typedef enum ast_binary_op_kind @@ -266,7 +270,7 @@ ast_node_t * ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value); ast_node_t * -ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id); +ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t ident_name); ast_node_t * ast_new_node_var_assign_stmt(arena_t *arena, diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c index 9006250..b695cc0 100644 --- a/src/codegen_x86_64.c +++ b/src/codegen_x86_64.c @@ -143,7 +143,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) case AST_NODE_REF: { ast_ref_t ref = expr_node->as_ref; - symbol_t *symbol = scope_lookup(ref.scope, ref.id); + symbol_t *symbol = scope_lookup(ref.ident.scope, ref.ident.name); assert(symbol); size_t offset = codegen_x86_64_get_stack_offset(codegen, symbol); @@ -597,9 +597,10 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) switch (bin_op.lhs->kind) { case AST_NODE_REF: { ast_ref_t ref = bin_op.lhs->as_ref; - scope_t *scope = ref.scope; + scope_t *scope = ref.ident.scope; - symbol_t *symbol = scope_lookup(scope, ref.id); + symbol_t *symbol = + scope_lookup(scope, ref.ident.name); assert(symbol); size_t offset = codegen_x86_64_get_stack_offset( @@ -668,7 +669,8 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) ast_ref_t ref = unary_op.operand->as_ref; - symbol_t *symbol = scope_lookup(ref.scope, ref.id); + symbol_t *symbol = + scope_lookup(ref.ident.scope, ref.ident.name); assert(symbol); size_t offset = @@ -722,9 +724,9 @@ codegen_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) case AST_NODE_VAR_DEF: { ast_var_definition_t var_def = node->as_var_def; - scope_t *scope = var_def.scope; + scope_t *scope = var_def.id.scope; - symbol_t *symbol = scope_lookup(scope, var_def.id); + symbol_t *symbol = scope_lookup(scope, var_def.id.name); assert(symbol); size_t type_size = type_to_bytes(symbol->type); diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c index bc5d119..9c0e607 100644 --- a/src/pretty_print_ast.c +++ b/src/pretty_print_ast.c @@ -288,7 +288,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) char name[256]; sprintf(name, "Var_Definition ", - SV_ARG(var.id), + SV_ARG(var.id.name), SV_ARG(var.type->id)); node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); @@ -305,7 +305,8 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) ast_ref_t ref = ast->as_ref; char name[256]; - sprintf(name, "Reference ", SV_ARG(ref.id)); + sprintf( + name, "Reference ", SV_ARG(ref.ident.name)); node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); diff --git a/src/type_checker.c b/src/type_checker.c index 235d711..abf7ae9 100644 --- a/src/type_checker.c +++ b/src/type_checker.c @@ -102,6 +102,18 @@ checker_check(checker_t *checker, ast_node_t *ast) // TODO: traverse the ast tree to verify semantics } +static void +register_id(checker_t *checker, + scope_t *scope, + ast_ident_t *ident, + type_t *type) +{ + ident->scope = scope; + symbol_t *symbol = symbol_new(checker->arena, ident->name, type); + + scope_insert(scope, symbol); +} + static void populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) { @@ -215,22 +227,18 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) } case AST_NODE_VAR_DEF: { - string_view_t id = ast->as_var_def.id; type_resolve(ast->as_var_def.type); - symbol_t *symbol = - symbol_new(checker->arena, id, ast->as_var_def.type); - - scope_insert(scope, symbol); - ast->as_var_def.scope = scope; + register_id( + checker, scope, &ast->as_var_def.id, ast->as_var_def.type); populate_scope(checker, scope, ast->as_var_def.value); return; } case AST_NODE_REF: { - ast->as_ref.scope = scope; + ast->as_ref.ident.scope = scope; return; } -- 2.46.1