From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v2 5/6] type: refactor: rename type.id to type.name
Date: Thu, 31 Oct 2024 03:13:22 +0000 (UTC) [thread overview]
Message-ID: <20241031031302.136553-6-carlos@maniero.me> (raw)
In-Reply-To: <20241031031302.136553-1-carlos@maniero.me>
Now that there is a struct called ast_ident_t the type.id may be
interpreted as an ast_id, to avoid confusion, it was renamed to name
which also is more meaningful since the id is not actually the type id
(as per TOKEN_ID) but the full type string representation. ie: "u8***".
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
src/pretty_print_ast.c | 6 +++---
src/type.c | 8 ++++----
src/type.h | 25 ++++++++++++++-----------
src/type_checker.c | 14 +++++++-------
4 files changed, 28 insertions(+), 25 deletions(-)
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 9501159..7fbed75 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -121,7 +121,7 @@ pretty_print_new_fn_param(ast_fn_param_t *param, arena_t *arena)
sprintf(name,
"Param_Definition <name:" SV_FMT "> <type:" SV_FMT ">",
SV_ARG(param->ident.name),
- SV_ARG(param->type->id));
+ SV_ARG(param->type->name));
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
return node;
@@ -158,7 +158,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
"Function_Definition <name:" SV_FMT "> <return:" SV_FMT
">%s",
SV_ARG(fn_def.id),
- SV_ARG(fn_def.return_type->id),
+ SV_ARG(fn_def.return_type->name),
fn_def._extern ? " <extern>" : "");
node->name =
(char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
@@ -290,7 +290,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
sprintf(name,
"Var_Definition <name:" SV_FMT "> <kind:" SV_FMT ">",
SV_ARG(var.ident.name),
- SV_ARG(var.type->id));
+ SV_ARG(var.type->name));
node->name =
(char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
diff --git a/src/type.c b/src/type.c
index 4a8d6f4..b146fb7 100644
--- a/src/type.c
+++ b/src/type.c
@@ -18,24 +18,24 @@
#include "assert.h"
type_t *
-type_new_unknown(arena_t *arena, string_view_t id)
+type_new_unknown(arena_t *arena, string_view_t name)
{
type_t *type = arena_alloc(arena, sizeof(type_t));
assert(type);
type->kind = TYPE_UNKNOWN;
- type->id = id;
+ type->name = name;
return type;
}
type_t *
-type_new_ptr(arena_t *arena, string_view_t id, type_t *ref_type)
+type_new_ptr(arena_t *arena, string_view_t name, type_t *ref_type)
{
type_t *type = arena_alloc(arena, sizeof(type_t));
assert(type);
type->kind = TYPE_PTR;
- type->id = id;
+ type->name = name;
type->as_ptr.type = ref_type;
return type;
}
diff --git a/src/type.h b/src/type.h
index d930a88..e5f2481 100644
--- a/src/type.h
+++ b/src/type.h
@@ -19,6 +19,16 @@
#include "arena.h"
#include "string_view.h"
+#define TYPE_BASE \
+ { \
+ type_kind_t kind; \
+ string_view_t name; \
+ }
+
+#define TYPE_HEAD \
+ type_kind_t base_kind; \
+ string_view_t name
+
typedef union type type_t;
typedef enum
@@ -38,32 +48,25 @@ typedef enum
typedef struct type_primitive
{
- type_kind_t _type_kind;
- string_view_t id;
+ TYPE_HEAD;
type_primitive_kind_t kind;
short size;
} type_primitive_t;
typedef struct type_unknown
{
- type_kind_t _type_kind;
- string_view_t id;
+ TYPE_HEAD;
} type_unknown_t;
typedef struct type_ptr
{
- type_kind_t _type_kind;
- string_view_t id;
+ TYPE_HEAD;
type_t *type;
} type_ptr_t;
typedef union type
{
- struct
- {
- type_kind_t kind;
- string_view_t id;
- };
+ struct TYPE_BASE;
type_unknown_t as_unknown;
type_primitive_t as_primitive;
type_ptr_t as_ptr;
diff --git a/src/type_checker.c b/src/type_checker.c
index ad51002..34a8d05 100644
--- a/src/type_checker.c
+++ b/src/type_checker.c
@@ -43,29 +43,29 @@ checker_new(arena_t *arena)
}
static type_t
-type_from_id(string_view_t id)
+type_from_id(string_view_t name)
{
type_t type = { 0 };
- type.id = id;
- if (string_view_eq_to_cstr(id, "u8")) {
+ type.name = name;
+ if (string_view_eq_to_cstr(name, "u8")) {
type.kind = TYPE_PRIMITIVE;
type.as_primitive.size = 1;
type.as_primitive.kind = TYPE_U8;
return type;
}
- if (string_view_eq_to_cstr(id, "u16")) {
+ if (string_view_eq_to_cstr(name, "u16")) {
type.kind = TYPE_PRIMITIVE;
type.as_primitive.size = 2;
type.as_primitive.kind = TYPE_U16;
return type;
}
- if (string_view_eq_to_cstr(id, "u32")) {
+ if (string_view_eq_to_cstr(name, "u32")) {
type.kind = TYPE_PRIMITIVE;
type.as_primitive.size = 4;
type.as_primitive.kind = TYPE_U32;
return type;
}
- if (string_view_eq_to_cstr(id, "u64")) {
+ if (string_view_eq_to_cstr(name, "u64")) {
type.kind = TYPE_PRIMITIVE;
type.as_primitive.size = 8;
type.as_primitive.kind = TYPE_U64;
@@ -84,7 +84,7 @@ type_resolve(type_t *type)
{
switch (type->kind) {
case TYPE_UNKNOWN:
- *type = type_from_id(type->as_unknown.id);
+ *type = type_from_id(type->as_unknown.name);
break;
case TYPE_PTR:
type_resolve(type->as_ptr.type);
--
2.46.1
next prev parent reply other threads:[~2024-10-31 3:13 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-31 3:13 [PATCH olang v2 0/6] Remove symbol lookups from codegen Carlos Maniero
2024-10-31 3:13 ` [PATCH olang v2 1/6] ast: create the ast_ident_t and apply it to var_def and ref Carlos Maniero
2024-10-31 3:13 ` [PATCH olang v2 2/6] semantics: resolve variable symbols Carlos Maniero
2024-10-31 3:13 ` [PATCH olang v2 3/6] semantics: refactor: use the ast_ident_t into the fn_call node Carlos Maniero
2024-10-31 3:13 ` [PATCH olang v2 4/6] semantics: refactor: use the ast_ident_t into the fn_def.params Carlos Maniero
2024-10-31 3:13 ` Carlos Maniero [this message]
2024-10-31 3:13 ` [PATCH olang v2 6/6] ast: remove dead code from var_assign ast node Carlos Maniero
2024-10-31 3:14 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-11-01 2:17 ` [PATCH olang v2 0/6] Remove symbol lookups from codegen 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=20241031031302.136553-6-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