diff options
author | kdx <kikoodx@paranoici.org> | 2023-12-21 14:53:58 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-12-21 15:13:14 +0100 |
commit | d9664c8b3010410b575abf2daec4658382f5354c (patch) | |
tree | b369019413b73eaaff0b15e131bd4e80f1b0a3ca | |
parent | 215f7bbfcefb6b7a84504edd2928a361f95941cf (diff) | |
download | o7c-d9664c8b3010410b575abf2daec4658382f5354c.tar.gz |
parse union & struct declaration
-rw-r--r-- | inc/type.h | 9 | ||||
-rwxr-xr-x | run.sh | 3 | ||||
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/parser/structdef.c | 4 | ||||
-rw-r--r-- | src/parser/uniondef.c | 5 | ||||
-rw-r--r-- | src/type.c | 29 |
6 files changed, 45 insertions, 8 deletions
@@ -32,7 +32,16 @@ typedef struct { typedef struct { SliceU8 name; + Node *type; + Node *init; +} AggregateElement; + +vec_decl(AggregateElement, AggregateElement); + +typedef struct { + SliceU8 name; SliceU8 parent; + VecAggregateElement *elements; } TypeAggregate; typedef enum { @@ -1,3 +1,4 @@ #!/bin/sh test "$1" == "" && exit 1 -tup -q && build/o7c $@ | dot -Tpng | imv - +#tup -q && build/o7c $@ | dot -Tpng | imv - +tup -q && build/o7c $@ @@ -32,8 +32,7 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) //vec_log(tokens, "%s %.*s", toktype_str(X->type), X->s.len, X->s.loc); auto nodes = parse(SliceToken_from_ptr(tokens->p, tokens->size)); - node_dot(nodes, 0, 0, 0); - putchar('\n'); + //node_dot(nodes, 0, 0, 0); auto types = VecType_new(0); for (auto node = nodes; node; node = node->next) { diff --git a/src/parser/structdef.c b/src/parser/structdef.c index 2904110..8b35f29 100644 --- a/src/parser/structdef.c +++ b/src/parser/structdef.c @@ -6,7 +6,7 @@ structdef(SliceToken *s) skip(s, "struct"); expect(s->loc[0], TOK_WORD); - auto node = node_new(NOD_UNION, s->loc[0]); + auto node = node_new(NOD_STRUCT, s->loc[0]); *s = SliceToken_next(*s); if (equal(s->loc[0], ":")) { @@ -26,7 +26,7 @@ structdef(SliceToken *s) skip(s, ":"); cur->Lhs = type(s); skip(s, "="); - node->Rhs = expr(s); + cur->Rhs = expr(s); if (!equal(s->loc[0], ",")) break; *s = SliceToken_next(*s); diff --git a/src/parser/uniondef.c b/src/parser/uniondef.c index 816b325..c397f60 100644 --- a/src/parser/uniondef.c +++ b/src/parser/uniondef.c @@ -19,13 +19,16 @@ uniondef(SliceToken *s) cur->Lhs = type(s); if (equal(s->loc[0], "=")) { *s = SliceToken_next(*s); - node->Rhs = expr(s); + assert(node->Rhs == nullptr); + cur->Rhs = expr(s); + node->Rhs = cur->Rhs; } if (!equal(s->loc[0], ",")) break; *s = SliceToken_next(*s); } assert(head.next != nullptr); + assert(node->Rhs != nullptr); node->Lhs = head.next; skip(s, "}"); @@ -1,6 +1,7 @@ #include "type.h" vec_impl(Type, Type); +vec_impl(AggregateElement, AggregateElement); Type type_new_alias(Node *node) @@ -15,6 +16,17 @@ type_new_alias(Node *node) }; } +static AggregateElement +_aggregateelement(Node *e) +{ + assert(e->Lhs != nullptr); + return (AggregateElement){ + .name = e->tok.s, + .type = e->Lhs, + .init = e->Rhs + }; +} + Type type_new_struct(Node *node) { @@ -22,13 +34,20 @@ type_new_struct(Node *node) .tag = TYPE_STRUCT, .aggregate = { .name = node->tok.s, - .parent = SliceU8_from_ptr(nullptr, 0) + .parent = SliceU8_from_ptr(nullptr, 0), + .elements = VecAggregateElement_new(0), } }; if (node->Lhs != nullptr) this.aggregate.parent = node->Lhs->tok.s; + for (auto e = node->Rhs; e != nullptr; e = e->next) { + auto ae = _aggregateelement(e); + assert(ae.init != nullptr); + VecAggregateElement_push(this.aggregate.elements, ae); + } + return this; } @@ -39,9 +58,15 @@ type_new_union(Node *node) .tag = TYPE_UNION, .aggregate = { .name = node->tok.s, - .parent = SliceU8_from_ptr(nullptr, 0) + .parent = SliceU8_from_ptr(nullptr, 0), + .elements = VecAggregateElement_new(0), } }; + for (auto e = node->Lhs; e != nullptr; e = e->next) { + auto ae = _aggregateelement(e); + VecAggregateElement_push(this.aggregate.elements, ae); + } + return this; } |