diff options
author | kdx <kikoodx@paranoici.org> | 2023-12-19 04:47:34 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-12-19 04:47:34 +0100 |
commit | b8a93201041c8b0013bb4f859188c5cce62e8238 (patch) | |
tree | 6745fdd290749b1668bde14d930f6fd50f42c297 | |
parent | 77233f19e621ca7057c2194f869b9cbb7d89fb81 (diff) | |
download | o7c-b8a93201041c8b0013bb4f859188c5cce62e8238.tar.gz |
progress on the type system
-rw-r--r-- | inc/type.h | 54 | ||||
-rw-r--r-- | src/main.c | 10 | ||||
-rw-r--r-- | src/parser/type_.c (renamed from src/parser/type.c) | 0 | ||||
-rw-r--r-- | src/type.c | 16 |
4 files changed, 77 insertions, 3 deletions
diff --git a/inc/type.h b/inc/type.h new file mode 100644 index 0000000..27c384e --- /dev/null +++ b/inc/type.h @@ -0,0 +1,54 @@ +#pragma once +#include "util.h" +#include "node.h" + +static const char *primary_types[] = { + // sized integers + "i8", + "i16", + "i32", + "i64", + "u8", + "u16", + "u32", + "u64", + // boolean + "bool", + // sized floats + "f32", + "f64", + // empty types + "any", + // nerd emoji + "🤓", + nullptr +}; + +typedef struct { + SliceU8 from; + SliceU8 to; + u64 ptr_depth; +} TypeAlias; + +typedef struct { + SliceU8 name; +} TypeAggregate; + +typedef enum { + TYPE_ALIAS, + TYPE_STRUCT, + TYPE_UNION, +} TypeTag; + +typedef struct { + TypeTag tag; + union { + TypeAlias alias; + TypeAggregate aggregate; + }; +} Type; + +Type type_new_alias(Node *node); +Type type_new_struct(Node *node); + +vec_decl(Type, Type); @@ -1,6 +1,7 @@ #include "util.h" #include "lexer.h" #include "parser.h" +#include "type.h" static SliceU8 _drain(const char *path) @@ -34,18 +35,21 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) node_dot(nodes, 0, 0, 0); putchar('\n'); - auto types = VecNodeP_new(0); + auto types = VecType_new(0); for (auto node = nodes; node; node = node->next) { switch (node->type) { case NOD_ALIAS: + VecType_push(types, type_new_alias(node)); + break; case NOD_STRUCT: case NOD_UNION: - VecNodeP_push(types, node); break; default: break; } } - plog("%zu", types->size); + vec_log(types, "%.*s = %.*s %lu", + X->alias.from.len, X->alias.from.loc, + X->alias.to.len, X->alias.to.loc, X->alias.ptr_depth); return 0; } diff --git a/src/parser/type.c b/src/parser/type_.c index 18098c2..18098c2 100644 --- a/src/parser/type.c +++ b/src/parser/type_.c diff --git a/src/type.c b/src/type.c new file mode 100644 index 0000000..49c7cb1 --- /dev/null +++ b/src/type.c @@ -0,0 +1,16 @@ +#include "type.h" + +vec_impl(Type, Type); + +Type +type_new_alias(Node *node) +{ + return (Type){ + .tag = TYPE_ALIAS, + .alias = { + .from = node->tok.s, + .to = node->Lhs->tok.s, + .ptr_depth = node->Lhs->ptr_depth + } + }; +} |