summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-12-21 14:53:58 +0100
committerkdx <kikoodx@paranoici.org>2023-12-21 15:13:14 +0100
commitd9664c8b3010410b575abf2daec4658382f5354c (patch)
treeb369019413b73eaaff0b15e131bd4e80f1b0a3ca
parent215f7bbfcefb6b7a84504edd2928a361f95941cf (diff)
downloado7c-d9664c8b3010410b575abf2daec4658382f5354c.tar.gz
parse union & struct declaration
-rw-r--r--inc/type.h9
-rwxr-xr-xrun.sh3
-rw-r--r--src/main.c3
-rw-r--r--src/parser/structdef.c4
-rw-r--r--src/parser/uniondef.c5
-rw-r--r--src/type.c29
6 files changed, 45 insertions, 8 deletions
diff --git a/inc/type.h b/inc/type.h
index d7accb8..224c668 100644
--- a/inc/type.h
+++ b/inc/type.h
@@ -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 {
diff --git a/run.sh b/run.sh
index d6af87e..95c558f 100755
--- a/run.sh
+++ b/run.sh
@@ -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 $@
diff --git a/src/main.c b/src/main.c
index dfc0290..82c0bd4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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, "}");
diff --git a/src/type.c b/src/type.c
index 1f4d494..250d0c8 100644
--- a/src/type.c
+++ b/src/type.c
@@ -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;
}