From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v1 2/5] ast: remove redundancy on ast_node base fields
Date: Wed, 23 Oct 2024 02:20:36 +0000 (UTC) [thread overview]
Message-ID: <20241023022022.38379-3-carlos@maniero.me> (raw)
In-Reply-To: <20241023022022.38379-1-carlos@maniero.me>
This solution was inspired on cython PyObject which uses macros to avoid
the redundancy.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
src/ast.h | 54 ++++++++++++++++++++++++++++++------------------------
1 file changed, 30 insertions(+), 24 deletions(-)
diff --git a/src/ast.h b/src/ast.h
index e7d8ed8..c7a25bf 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -26,6 +26,21 @@
#include "string_view.h"
#include "type.h"
+#define AST_NODE_BASE \
+ { \
+ ast_node_kind_t kind; \
+ token_loc_t loc; \
+ }
+
+/**
+ * Every node within the *ast_node* union must include the base segment.
+ *
+ * This macro must be declared first within any node definition to ensure the
+ * ast_node base segment's integrity.
+ */
+
+#define AST_NODE_HEAD struct AST_NODE_BASE base
+
typedef union ast_node ast_node_t;
typedef enum
@@ -45,21 +60,17 @@ typedef enum
AST_NODE_UNKNOWN
} ast_node_kind_t;
-typedef struct ast_node_meta
-{
- ast_node_kind_t kind;
- token_loc_t loc;
-} ast_node_meta_t;
+typedef struct ast_node_base AST_NODE_BASE ast_node_base_t;
typedef struct ast_block
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
list_t *nodes;
} ast_block_t;
typedef struct ast_translation_unit
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
list_t *decls;
} ast_translation_unit_t;
@@ -71,7 +82,7 @@ typedef struct ast_fn_param
typedef struct ast_fn_definition
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
string_view_t id;
list_t *params;
type_t *return_type;
@@ -82,7 +93,7 @@ typedef struct ast_fn_definition
typedef struct ast_fn_call
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
string_view_t id;
list_t *args;
scope_t *scope;
@@ -90,7 +101,7 @@ typedef struct ast_fn_call
typedef struct ast_var_definition
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
string_view_t id;
type_t *type;
ast_node_t *value;
@@ -104,7 +115,7 @@ typedef enum
typedef struct ast_literal
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
ast_literal_kind_t kind;
union
{
@@ -114,7 +125,7 @@ typedef struct ast_literal
typedef struct ast_ref
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
string_view_t id;
scope_t *scope;
} ast_ref_t;
@@ -144,7 +155,7 @@ typedef enum ast_binary_op_kind
typedef struct ast_binary_op
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
ast_binary_op_kind_t kind;
ast_node_t *lhs;
ast_node_t *rhs;
@@ -162,27 +173,27 @@ typedef enum ast_unary_op_kind
typedef struct ast_unary_op
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
ast_unary_op_kind_t kind;
ast_node_t *expr;
} ast_unary_op_t;
typedef struct ast_var_assign_stmt
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
ast_node_t *ref;
ast_node_t *expr;
} ast_var_assign_stmt_t;
typedef struct ast_return_stmt
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
ast_node_t *expr;
} ast_return_stmt_t;
typedef struct ast_if_stmt
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
ast_node_t *cond;
ast_node_t *then;
ast_node_t *_else;
@@ -190,19 +201,14 @@ typedef struct ast_if_stmt
typedef struct ast_while_stmt
{
- ast_node_meta_t meta;
+ AST_NODE_HEAD;
ast_node_t *cond;
ast_node_t *then;
} ast_while_stmt_t;
typedef union ast_node
{
- // inlined ast_node_meta_t struct.
- struct
- {
- ast_node_kind_t kind;
- token_loc_t loc;
- };
+ struct AST_NODE_BASE;
ast_translation_unit_t as_translation_unit;
ast_fn_definition_t as_fn_def;
ast_fn_call_t as_fn_call;
--
2.46.1
next prev parent reply other threads:[~2024-10-23 2:20 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-23 2:20 [PATCH olang v1 0/5] Refactors to support semantics Carlos Maniero
2024-10-23 2:20 ` [PATCH olang v1 1/5] type_checker: rename checker to type_checker Carlos Maniero
2024-10-23 2:20 ` Carlos Maniero [this message]
2024-10-23 2:20 ` [PATCH olang v1 3/5] ast: unary: rename ast_unary.expr to ast_unary.operand Carlos Maniero
2024-10-23 2:20 ` [PATCH olang v1 4/5] ast: remove expr reference from return and var assign nodes Carlos Maniero
2024-10-23 2:20 ` [PATCH olang v1 5/5] semantics: add scope to all nodes Carlos Maniero
2024-10-23 2:21 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-23 17:36 ` [PATCH olang v1 5/5] semantics: add scope to all nodes Johnny Richard
2024-10-23 17:34 ` [PATCH olang v1 0/5] Refactors to support semantics Johnny Richard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241023022022.38379-3-carlos@maniero.me \
--to=carlos@maniero.me \
--cc=~johnnyrichard/olang-devel@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox