summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-12-22 05:08:11 +0100
committerkdx <kikoodx@paranoici.org>2023-12-22 05:08:11 +0100
commit99e24b2551261d6f3d94a4d8b94de01eab982063 (patch)
treeb6b24b92890c848e11ee8f744d05f9dc8342c6e6
parentaa6b66237f4602d93f6eb2953af4d1c1ad9c4b73 (diff)
downloado7c-99e24b2551261d6f3d94a4d8b94de01eab982063.tar.gz
primary types
-rw-r--r--inc/type.h47
-rw-r--r--src/main.c2
-rw-r--r--src/type.c21
3 files changed, 48 insertions, 22 deletions
diff --git a/inc/type.h b/inc/type.h
index 224c668..9d13816 100644
--- a/inc/type.h
+++ b/inc/type.h
@@ -2,30 +2,36 @@
#include "util.h"
#include "node.h"
-[[maybe_unused]] static const char *primary_types[] = {
+typedef struct {
+ const char *name;
+ u64 bits;
+ bool signd;
+} TypePrimary;
+
+[[maybe_unused]] static const TypePrimary primary_types[] = {
// sized integers
- "i8",
- "i16",
- "i32",
- "i64",
- "u8",
- "u16",
- "u32",
- "u64",
+ { "i8", 8, true },
+ { "i16", 16, true },
+ { "i32", 32, true },
+ { "i64", 64, true },
+ { "u8", 8, false },
+ { "u16", 16, false },
+ { "u32", 32, false },
+ { "u64", 64, false },
// boolean
- "bool",
+ { "bool", 1, false },
// sized floats
- "f32",
- "f64",
+ { "f32", 32, true },
+ { "f64", 64, true },
// empty types
- "any",
+ { "any", 0, false },
// nerd emoji
- "🤓",
- nullptr
+ { "🤓", 0, false },
+
+ { nullptr, 0, false }
};
typedef struct {
- SliceU8 from;
SliceU8 to;
u64 ptr_depth;
} TypeAlias;
@@ -39,12 +45,12 @@ typedef struct {
vec_decl(AggregateElement, AggregateElement);
typedef struct {
- SliceU8 name;
SliceU8 parent;
VecAggregateElement *elements;
} TypeAggregate;
typedef enum {
+ TYPE_PRIMARY,
TYPE_ALIAS,
TYPE_STRUCT,
TYPE_UNION,
@@ -52,14 +58,17 @@ typedef enum {
typedef struct {
TypeTag tag;
+ SliceU8 name;
union {
+ TypePrimary primary;
TypeAlias alias;
TypeAggregate aggregate;
};
} Type;
+vec_decl(Type, Type);
+
+void type_add_primaries(VecType *v);
Type type_new_alias(Node *node);
Type type_new_struct(Node *node);
Type type_new_union(Node *node);
-
-vec_decl(Type, Type);
diff --git a/src/main.c b/src/main.c
index 82c0bd4..b7e7760 100644
--- a/src/main.c
+++ b/src/main.c
@@ -35,6 +35,7 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
//node_dot(nodes, 0, 0, 0);
auto types = VecType_new(0);
+ type_add_primaries(types);
for (auto node = nodes; node; node = node->next) {
switch (node->type) {
case NOD_ALIAS:
@@ -49,6 +50,7 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
default: break;
}
}
+ vec_log(types, "%.*s", X->name.len, X->name.loc);
return 0;
}
diff --git a/src/type.c b/src/type.c
index 250d0c8..42b34f7 100644
--- a/src/type.c
+++ b/src/type.c
@@ -8,8 +8,8 @@ type_new_alias(Node *node)
{
return (Type){
.tag = TYPE_ALIAS,
+ .name = node->tok.s,
.alias = {
- .from = node->tok.s,
.to = node->Lhs->tok.s,
.ptr_depth = node->Lhs->ptr_depth
}
@@ -27,13 +27,28 @@ _aggregateelement(Node *e)
};
}
+void
+type_add_primaries(VecType *v)
+{
+ assert(v != nullptr);
+ for (auto i = 0u; primary_types[i].name != nullptr; i++) {
+ const auto e = &primary_types[i];
+ const Type primary = {
+ .tag = TYPE_PRIMARY,
+ .name = SliceU8_from_ptr((u8*)e->name, strlen(e->name)),
+ .primary = *e
+ };
+ VecType_push(v, primary);
+ }
+}
+
Type
type_new_struct(Node *node)
{
Type this = {
.tag = TYPE_STRUCT,
+ .name = node->tok.s,
.aggregate = {
- .name = node->tok.s,
.parent = SliceU8_from_ptr(nullptr, 0),
.elements = VecAggregateElement_new(0),
}
@@ -56,8 +71,8 @@ type_new_union(Node *node)
{
Type this = {
.tag = TYPE_UNION,
+ .name = node->tok.s,
.aggregate = {
- .name = node->tok.s,
.parent = SliceU8_from_ptr(nullptr, 0),
.elements = VecAggregateElement_new(0),
}