From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id iCfvJJDw92WLGgAAqHPOHw:P1 (envelope-from ) for ; Mon, 18 Mar 2024 08:43:12 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id iCfvJJDw92WLGgAAqHPOHw (envelope-from ) for ; Mon, 18 Mar 2024 08:43:12 +0100 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 868353FE2E for ; Mon, 18 Mar 2024 08:43:12 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=Sym5pHa78VsX8HXZU0pdG1fcyb72s/sDwfVSZl59be0=; 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=1710747792; v=1; b=JnbdsfZHlQVifnUXWS4WQXP9DokkB3+1y2uCABZfN1d+I6ps7FuV7Yd4lleo8KnsSe+jjsWR iNWSBJMfgr5+cbZcNTttULshYYZxjyk1lIFQfCcG4iaS09jtqQ5rGZ2wXVnqIcz4BfFPeQIIerR uvHsTMdRaOhtWEqjAnNSl7pkiMzCYaqRl1SF/9L5EjoEwrLYpo6/bsnx8MTjWOBPC8y550+JpPY 2ku5Bih7dnFtxSE1Vz/Iv3j0NcYIkAOAVUTF95TJdQNbeGQwy9KL+w/k7w4brD9NbIZyPDO/9fE u9aluhJzUYBFjA0Gm4w2P56DIaRXB8yboEgakvNrB20Ew== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 5ADD3200F4 for ; Mon, 18 Mar 2024 07:43:12 +0000 (UTC) Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [IPv6:2001:41d0:1004:224b::b9]) by mail-a.sr.ht (Postfix) with ESMTPS id C24E320082 for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 18 Mar 2024 07:43:11 +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=1710747791; 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=cPWzN608ZJ118MWzFX4TWBQHOhw6tx0hjk8klSM/cCc=; b=zmsBk/kzA8aCkPJajR9FaODFlPo2kw3dhJgSnhzwECAhf+pM/IfYmEr8p2zpaxjcQwSXjj tR+CBawxqS88OOU2I1ZVYTEtvwYIkSplscsLwd9g+llHT6ptNa1PL59Xb3YtomJFOSE7RR Mt99rfoGlk4nMNg0Wwi3DaoCKypcaJ462gH5q9mQ1qlnPHNHulhh5iKj31qO3+StXfhuuA gbhQkaJRxyE06RWN4UQegrNmN5fZw1jbUl0VYdb+VQtGemtH315BkVe7jEsPw6W+HRaAn1 mucMZK1Yh/VRUsNlhr/sFhocIzDzuIyxmQXIyvbwEdiBUmhcgik78zKhXOyaTQ== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v3 2/3] ast: create binary operation ast node Date: Mon, 18 Mar 2024 09:39:52 +0100 Message-ID: <20240318084254.142417-3-johnny@johnnyrichard.com> In-Reply-To: <20240318084254.142417-1-johnny@johnnyrichard.com> References: <20240318084254.142417-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-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 868353FE2E X-Migadu-Scanner: mx11.migadu.com X-TUID: izyU1Dw+oPy6 Signed-off-by: Johnny Richard --- v2: Map all binary operation kind possibles at this moment src/ast.c | 14 ++++++++++++++ src/ast.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/ast.c b/src/ast.c index ab56c96..c9f33a4 100644 --- a/src/ast.c +++ b/src/ast.c @@ -52,6 +52,20 @@ ast_new_node_fn_def(arena_t *arena, string_view_t identifier, type_t return_type return node_fn_def; } +ast_node_t * +ast_new_node_bin_op(arena_t *arena, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs) +{ + ast_node_t *node_bin_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + assert(node_bin_op); + + node_bin_op->kind = AST_NODE_BINARY_OP; + node_bin_op->data.as_bin_op.kind = kind; + node_bin_op->data.as_bin_op.lhs = lhs; + node_bin_op->data.as_bin_op.rhs = rhs; + + return node_bin_op; +} + ast_node_t * ast_new_node_literal_u32(arena_t *arena, uint32_t value) { diff --git a/src/ast.h b/src/ast.h index 2b42781..4e146f8 100644 --- a/src/ast.h +++ b/src/ast.h @@ -30,6 +30,7 @@ typedef enum AST_NODE_PROGRAM, AST_NODE_BLOCK, AST_NODE_FN_DEF, + AST_NODE_BINARY_OP, AST_NODE_RETURN_STMT, AST_NODE_LITERAL, AST_NODE_UNKNOWN @@ -73,6 +74,35 @@ typedef struct ast_literal ast_literal_value_t value; } ast_literal_t; +typedef enum ast_binary_op_kind +{ + AST_BINOP_ADDITION, + AST_BINOP_SUBTRACTION, + AST_BINOP_MULTIPLICATION, + AST_BINOP_DIVISION, + AST_BINOP_REMINDER, + AST_BINOP_BITWISE_LSHIFT, + AST_BINOP_BITWISE_RSHIFT, + AST_BINOP_BITWISE_XOR, + AST_BINOP_BITWISE_AND, + AST_BINOP_BITWISE_OR, + AST_BINOP_CMP_LT, + AST_BINOP_CMP_GT, + AST_BINOP_CMP_LEQ, + AST_BINOP_CMP_GEQ, + AST_BINOP_CMP_EQ, + AST_BINOP_CMP_NEQ, + AST_BINOP_LOGICAL_AND, + AST_BINOP_LOGICAL_OR, +} ast_binary_op_kind_t; + +typedef struct ast_binary_op +{ + ast_binary_op_kind_t kind; + ast_node_t *lhs; + ast_node_t *rhs; +} ast_binary_op_t; + typedef struct ast_return_stmt { ast_node_t *data; @@ -82,6 +112,7 @@ typedef union { ast_program_t as_program; ast_fn_definition_t as_fn_def; + ast_binary_op_t as_bin_op; ast_literal_t as_literal; ast_block_t as_block; ast_return_stmt_t as_return_stmt; @@ -99,6 +130,9 @@ ast_new_program(arena_t *arena, ast_node_t *fn_def); ast_node_t * ast_new_node_fn_def(arena_t *arena, string_view_t identifier, type_t return_type, ast_node_t *block); +ast_node_t * +ast_new_node_bin_op(arena_t *arena, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs); + ast_node_t * ast_new_node_literal_u32(arena_t *arena, uint32_t value); -- 2.44.0