summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-12-21 15:21:47 +0100
committerkdx <kikoodx@paranoici.org>2023-12-21 15:21:47 +0100
commitaa6b66237f4602d93f6eb2953af4d1c1ad9c4b73 (patch)
tree8462cbaf90b84ae1f0f1be670e92ef97bd0b4d78
parentd9664c8b3010410b575abf2daec4658382f5354c (diff)
downloado7c-aa6b66237f4602d93f6eb2953af4d1c1ad9c4b73.tar.gz
parse array types
-rw-r--r--inc/node.h12
-rw-r--r--samples/types.o72
-rw-r--r--src/node/dot.c5
-rw-r--r--src/parser/type_.c11
4 files changed, 22 insertions, 8 deletions
diff --git a/inc/node.h b/inc/node.h
index b1d5f01..8bd3093 100644
--- a/inc/node.h
+++ b/inc/node.h
@@ -42,12 +42,14 @@ struct Node {
NodeType type;
Token tok;
Node *next;
+
+ Node *Lhs;
+ Node *Rhs;
+ Node *rtype;
+
+ /* NOD_TYPE specifics */
u64 ptr_depth;
- struct {
- Node *Lhs;
- Node *Rhs;
- Node *rtype;
- };
+ Node *array_size;
};
vec_decl(NodeP, Node *);
diff --git a/samples/types.o7 b/samples/types.o7
index 989666f..067a820 100644
--- a/samples/types.o7
+++ b/samples/types.o7
@@ -8,7 +8,7 @@ struct Object {
cringe: *u8 = null,
real: Truth = undefined, // not sure if that should be allowed
len: Length = 0,
- //data: [128]ObjectData = default // does what you think //TODO arrays
+ data: [128]ObjectData = default // does what you think
};
// only one union field can (and must) have a default value
diff --git a/src/node/dot.c b/src/node/dot.c
index a89e415..0e8d6f9 100644
--- a/src/node/dot.c
+++ b/src/node/dot.c
@@ -36,6 +36,11 @@ node_dot(Node *root, u32 depth, u32 first, u32 branch)
const auto oid = node_dot(root->rtype, depth + 1, 0, ++_branch);
if (oid) _link(id, oid);
}
+ if (root->array_size) {
+ const auto oid =
+ node_dot(root->array_size, depth + 1, 0, ++_branch);
+ if (oid) _link(id, oid);
+ }
if (root->Lhs) {
const auto oid = node_dot(root->Lhs, depth + 1, 0, ++_branch);
if (oid) _link(id, oid);
diff --git a/src/parser/type_.c b/src/parser/type_.c
index 0f410ea..d400266 100644
--- a/src/parser/type_.c
+++ b/src/parser/type_.c
@@ -3,12 +3,20 @@
Node *
type(SliceToken *s)
{
+ auto node = node_new(NOD_TYPE, s->loc[0]);
u64 ptr_depth = 0;
+
+ if (equal(s->loc[ptr_depth], "[")) {
+ next(s);
+ node->array_size = expr(s);
+ skip(s, "]");
+ }
+
while (equal(s->loc[ptr_depth], "*"))
ptr_depth++;
+ node->tok = s->loc[ptr_depth];
if (ptr_depth && equal(s->loc[ptr_depth], "fn")) {
- auto node = node_new(NOD_TYPE, s->loc[ptr_depth]);
*s = SliceToken_sub(*s, ptr_depth + 1, -1);
skip(s, "(");
node->Lhs = fnargs(s, type);
@@ -21,7 +29,6 @@ type(SliceToken *s)
}
expect(s->loc[ptr_depth], TOK_WORD);
- auto node = node_new(NOD_TYPE, s->loc[ptr_depth]);
node->ptr_depth = ptr_depth;
*s = SliceToken_sub(*s, ptr_depth + 1, -1);