From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.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 6GF4Gtg/GmfCHwAA62LTzQ:P1 (envelope-from ) for ; Thu, 24 Oct 2024 14:38:48 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id 6GF4Gtg/GmfCHwAA62LTzQ (envelope-from ) for ; Thu, 24 Oct 2024 14:38:48 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=d0maEPfg; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=uE1ZPAhX; 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=1729773528; a=rsa-sha256; cv=pass; b=RnmDsN/RRiL9zftDAiePxNjBHTpjo4y5fk2NzQl7S+ml+WNpDn5YdZQ2pUnTx6aMIsI/qN fXMVDo8BGZKtr41F/sfXTlXgjm2v5iiQ9uWS/zqo26oKkGtrSlGBSlS1hIHzBPTDDXsPXN yUYs9Z1qHYFcXj1icWCTJ5HbPxK8otT8w0sztuNunDhJKV7lMLt3c0XyUkdSf/p9eM+CxV +3OyQPwmYVap/fONLaObX+LOrhpeys4BIB3dmuFTUbQRu9LDA1Q+6ce/1mti/7WB4pKVYH /yl8Nh8973N9ZntFjG5eejqsRENgPSWvw9TK7BQbBl3ynrOOKOSCpJIyPpy8Xw== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=d0maEPfg; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=uE1ZPAhX; 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=1729773528; 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=BY7gEpbC0PExc5RMhWYEYnkWkb2yZE7nud0dDduJzMA=; b=EFTU43lpq1haVroLBjudmc0xgmbzgA8GmN/m+7pe/PrBJMW2fFgsvUxswICS+z2R70gy4U sENfibJBe8XVIQMT2oBIDZ1Ux6DeAEQr4MK2jw5158mDg0OjwWQRqh5V/HfMgb/2Jzseq3 gi6vret3Xv9GVVvEicZpbJi4tIbwCwU7139oLE2qbt4hVmjWvbJG1Y0/fWq1i7JIapUPXG muBcFPUkB53VD23lnQ5ztXqG6YegZ7dojYVvC6Lgme+GUADTq7D1holj24HtC9sqJqJYur kLBlfaCX1Y9SxCG2mWHIv9F4zwt/F6ovfSIlV3dieqjbdriC3z55VzsS4Y0ciA== 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 5C7D067D3 for ; Thu, 24 Oct 2024 14:38:43 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=g6mk5DU0FfMMQpyF4CuXme/6+nnccGoJIpRsyqc+OZw=; 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=1729773523; v=1; b=d0maEPfgPxO0AIbp9PF5IsitXOFRaKDeiJR/IruYmC5+YBFJ227+ivc+u+Il4ewLTfFEda+Z F0HxYHmMAQLqbikcuAqs1Jv8mkjkhhmWGO4U4K780+KU5vxNYOpEQ553f36ISwoyJswV4PkKDdI CDNcmoezMH7W1mZmfaFTeCw/OHgu9rz9BkaDlLWWGTef0DztEW4LmniKGhOAAhNc6ZaUrW3IC6Z Dz1SlzqRjZCibnimW2a/PbsPf0YDsCh+mGzd9Y6Gv0NHGeko5M5ya+c+X2o+AryctFmDAWHmUb/ p4W3y8SxztRMaVLWX58+U9942zRBuk+vknv6nSo4PExbA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 7AF7620353 for ; Thu, 24 Oct 2024 12:38:43 +0000 (UTC) Received: from skyblue.cherry.relay.mailchannels.net (skyblue.cherry.relay.mailchannels.net [23.83.223.167]) by mail-a.sr.ht (Postfix) with ESMTPS id 458D120347 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 24 Oct 2024 12:38:39 +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 C595994601D for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 24 Oct 2024 12:38:37 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (100-99-0-159.trex-nlb.outbound.svc.cluster.local [100.99.0.159]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 34FF7946008 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 24 Oct 2024 12:38:37 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1729773517; a=rsa-sha256; cv=none; b=tVNUM4AwErEP/UQqDxnknyarIdgqVck5Wsn9mbYwAk6+Vp06ZotoymO5gIPxqH/UVBEMgy zs7UV4+JI83Alff6aKE9Naamd+IXLpsfk2BfhBpkXxc84ZmxRfoiIOxt7Wi+dhJwvB7mYL Zi/JYhfMmFe74uI6Nx/npCq/asDM8x9CsZdtHJCAruahGd+yYvHFuiSnV9flfn4FFkEjZQ RoxX+p1Z9p0ADHFcc1SkitNjMiy5S7J9ZukQI6Uk/MzY9T8JOem0D3SMhXOHJ56b+WN6/o tTJYAx2Xx2uTi1zhh22j86DhXn+lRK4LYGO6oXEeMm7shlRx2zkP9mYfslPbcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1729773517; 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=BY7gEpbC0PExc5RMhWYEYnkWkb2yZE7nud0dDduJzMA=; b=3VVdy2hY7VvY9fbjH3wdKm3XNwOsqp9ZgESE4IPPRVaWniwz+bRi37FiOifI7SGdjLL5vZ Uoa+/ZOGNyooRvO0klAmjNh8Awa5GlIyd2Y4pxFx2yDGPXAT7Lo0TEJmRNM1dF+VvR0EBh X9rchUfUqb0Wv0oFJLWkEiPOCqnORoFsVHvCJaYUoJjtom+lD4fwUaStRXv5D62e0BLtTw 4A86qrs0lYFg1aLwDQjA78VMzUgdBQDtOVNFtVlOMJ+00mQz59D7b86HLQfKSZ8SmdKbSE Xnm1ZwMI+SStV1Uzg1vuqNsl8Xt4CkAiijlV1+gyKC0a4qguxMnQNFY/SJS+EA== ARC-Authentication-Results: i=1; rspamd-7767f6b98-6kpf2; 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-Whistle-Gusty: 58f720ea6527d002_1729773517697_3851006993 X-MC-Loop-Signature: 1729773517697:3239788022 X-MC-Ingress-Time: 1729773517697 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.0.159 (trex/7.0.2); Thu, 24 Oct 2024 12:38:37 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1729773515; 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=BY7gEpbC0PExc5RMhWYEYnkWkb2yZE7nud0dDduJzMA=; b=uE1ZPAhXPZjLZxXpkd8oAJzQpObLTxt5orkVocVtFwPBLhkIU2yCkLuOWnSkxfEgO2NZ/y JRn+/8ouQbBUjImq2tIks4S/ppD/kf9/HGI4+h22R3JRN30JetYMJcHHC+laCpaJ+q/Xpb IdwgkndVCfIl7DU8BK6UDuZx3V2bz6GB3ZDbrXm7kS2nfeV2AQwG2IscFGlGMIB5BPIK3V GV24asrZGSHmynAMOYOLpsMe2EDijOVPXwqU2aQ+mp0K/YGtH/lxx9o93XEHnLJOexkJto QAXH0vBULLWq0KIVzvj6y9/BR9bNUKHkVQNPiIc1zAdfPJ5JcRhgGAQxArKHJA== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v1 1/6] ast: create the ast_id_t and apply it to var_def and ref Message-ID: <20241024123825.120390-2-carlos@maniero.me> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241024123825.120390-1-carlos@maniero.me> References: <20241024123825.120390-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Thu, 24 Oct 2024 12:38:35 +0000 (UTC) X-CM-Analysis: v=2.4 cv=VemlP0p9 c=1 sm=1 tr=0 ts=671a3fcb a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=NGT_zBBPsxoQ-nyd:21 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=FXxGotEPVFvnrxr-ttQA:9 a=BXDaF_L80NY05PYiAFlV:22 X-CM-Envelope: MS4xfH6sBEEqH9Ehi7o1VvXdsx00PPZG+jZkTZ2il/dXQ8x0WAFDFjNBlZdBlAzdUdtPdLOv53Fi6VIiUjG+sgQDPgKQ0Ck1tOzntglKggeSVUspQORpViw1 EPN0OwiThw1ROCD8KN7m0gHcxkNRhkVBSgyABKsq0t0hlrJn6eOCOrhDJ7XsWFIBBAZ58ZbZcyN3L7gK3qdR91CCJDpoWSRnK9i+S9/eyeLq78ncX0+d9COy 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.75 X-Spam-Score: -0.75 X-Migadu-Queue-Id: 5C7D067D3 X-Migadu-Scanner: mx12.migadu.com X-TUID: lZZcNvHwzq4B The ast_id_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 | 4 ++-- src/ast.h | 12 ++++++++---- src/codegen_x86_64.c | 12 ++++++------ src/pretty_print_ast.c | 4 ++-- src/type_checker.c | 19 ++++++++++++------- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/ast.c b/src/ast.c index b96a463..d4f6e87 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; @@ -174,7 +174,7 @@ ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id) node_ref->kind = AST_NODE_REF; node_ref->loc = loc; - node_ref->as_ref.id = id; + node_ref->as_ref.id.name = id; return node_ref; } diff --git a/src/ast.h b/src/ast.h index 7c5e9af..c55ecf1 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_id +{ + string_view_t name; + scope_t *scope; +} ast_id_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_id_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_id_t id; } ast_ref_t; typedef enum ast_binary_op_kind diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c index 9006250..c7122be 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.id.scope, ref.id.name); assert(symbol); size_t offset = codegen_x86_64_get_stack_offset(codegen, symbol); @@ -597,9 +597,9 @@ 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.id.scope; - symbol_t *symbol = scope_lookup(scope, ref.id); + symbol_t *symbol = scope_lookup(scope, ref.id.name); assert(symbol); size_t offset = codegen_x86_64_get_stack_offset( @@ -668,7 +668,7 @@ 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.id.scope, ref.id.name); assert(symbol); size_t offset = @@ -722,9 +722,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..fdc6b65 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,7 @@ 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.id.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..a2ffdd6 100644 --- a/src/type_checker.c +++ b/src/type_checker.c @@ -102,6 +102,15 @@ 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_id_t *id, type_t *type) +{ + id->scope = scope; + symbol_t *symbol = symbol_new(checker->arena, id->name, type); + + scope_insert(scope, symbol); +} + static void populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) { @@ -215,22 +224,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.id.scope = scope; return; } -- 2.46.1