diff options
author | kdx <kikoodx@paranoici.org> | 2023-12-27 04:44:28 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-12-27 05:47:52 +0100 |
commit | 3e68cd21e4bba1d2e287263be069dde46c8f893f (patch) | |
tree | 9e733b2c5d93fa8979f76b8f18b0671e9a40f68a | |
parent | 564af1690519d2aaa5bc3e9921974007a49bdb24 (diff) | |
download | o7c-3e68cd21e4bba1d2e287263be069dde46c8f893f.tar.gz |
finalize type checking
-rw-r--r-- | inc/coerce.h | 4 | ||||
-rw-r--r-- | inc/type.h | 35 | ||||
-rwxr-xr-x | run.sh | 3 | ||||
-rw-r--r-- | samples/types.o7 | 2 | ||||
-rw-r--r-- | src/coerce.c | 6 | ||||
-rw-r--r-- | src/main.c | 6 | ||||
-rw-r--r-- | src/type.c | 41 |
7 files changed, 69 insertions, 28 deletions
diff --git a/inc/coerce.h b/inc/coerce.h new file mode 100644 index 0000000..f15dbff --- /dev/null +++ b/inc/coerce.h @@ -0,0 +1,4 @@ +#pragma once +#include "type.h" + +void coerce(Node *expr, VecType *types); @@ -9,30 +9,29 @@ typedef struct { } TypePrimary; [[maybe_unused]] static const TypePrimary primary_types[] = { + // special type + { "void", 0, false }, // sized integers - { "i8", 8, true }, - { "i16", 16, true }, - { "i32", 32, true }, - { "i64", 64, true }, - { "u8", 8, false }, - { "u16", 16, false }, - { "u32", 32, false }, - { "u64", 64, false }, + { "i8", 8, true }, + { "i16", 16, true }, + { "i32", 32, true }, + { "i64", 64, true }, + { "isize", 64, true }, + { "u8", 8, false }, + { "u16", 16, false }, + { "u32", 32, false }, + { "u64", 64, false }, + { "usize", 64, false }, // boolean - { "bool", 1, false }, + { "bool", 1, false }, // sized floats - { "f32", 32, true }, - { "f64", 64, true }, - // empty types - { "any", 0, false }, - // nerd emoji - { "🤓", 0, false }, - - { nullptr, 0, false } + { "f32", 32, true }, + { "f64", 64, true }, + {0} }; typedef struct { - SliceU8 to; + Node *to; u64 ptr_depth; } TypeAlias; @@ -1,4 +1,3 @@ #!/bin/sh test "$1" == "" && exit 1 -#tup -q && build/o7c $@ | dot -Tpng | imv - -tup -q && build/o7c $@ +tup -q && build/o7c $@ #| dot -Tpng | imv - diff --git a/samples/types.o7 b/samples/types.o7 index 923ef2f..11f4a25 100644 --- a/samples/types.o7 +++ b/samples/types.o7 @@ -23,7 +23,7 @@ struct Table : Object { }; struct LinkedList { - v: *any = null, + v: *void = null, next: *LinkedList = null }; diff --git a/src/coerce.c b/src/coerce.c new file mode 100644 index 0000000..958c6af --- /dev/null +++ b/src/coerce.c @@ -0,0 +1,6 @@ +#include "coerce.h" + +void +coerce(Node *expr, VecType *types) +{ +} @@ -2,6 +2,7 @@ #include "lexer.h" #include "parser.h" #include "type.h" +#include "coerce.h" static SliceU8 _drain(const char *path) @@ -55,8 +56,11 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) VecType_push(types, type); } types_check_late(types); + //vec_log(types, "%.*s", X->name.len, X->name.loc); - vec_log(types, "%.*s", X->name.len, X->name.loc); + for (auto node = nodes; node; node = node->next) + if (node->type == NOD_FN) + coerce(node, types); return 0; } @@ -11,7 +11,7 @@ type_new_alias(Node *node) .tag = TYPE_ALIAS, .name = node->tok.s, .alias = { - .to = node->Lhs->tok.s, + .to = node->Lhs, .ptr_depth = node->Lhs->ptr_depth } }; @@ -116,12 +116,13 @@ type_check_early(Type this, VecType *types) this.name.len, this.name.loc); if (this.tag == TYPE_ALIAS) { - if (SliceU8_cmp(this.alias.to, + if (SliceU8_cmp(this.alias.to->tok.s, SliceU8_from_ptr((u8*)"fn", 2)) == 0) {} /* checked late */ - else if (!types_exist(types, this.alias.to)) + else if (!types_exist(types, this.alias.to->tok.s)) panic("alias to undeclared type '%.*s'", - this.alias.to.len, this.alias.to.loc); + this.alias.to->tok.s.len, + this.alias.to->tok.s.loc); } if (this.tag == TYPE_STRUCT && this.aggregate.parent.len) { @@ -148,13 +149,41 @@ type_check_early(Type this, VecType *types) } static void -_type_check_late(Type this, VecType *types) +_type_check_late(Node *this, VecType *types) { + if (SliceU8_cmp(this->tok.s, SliceU8_from_ptr((u8*)"fn", 2)) == 0) { + for (auto e = this->Lhs; e != nullptr; e = e->next) + _type_check_late(e, types); + if (this->Rhs != nullptr) + _type_check_late(this->Rhs, types); + return; + } + + if (!types_exist(types, this->tok.s)) + panic("undeclared type '%.*s'", + this->tok.s.len, this->tok.s.loc); } void types_check_late(VecType *types) { for (size_t i = 0; i < types->size; i++) - _type_check_late(types->p[i], types); + { + const auto e = &types->p[i]; + if (e->tag == TYPE_ALIAS && + SliceU8_cmp(e->alias.to->tok.s, + SliceU8_from_ptr((u8*)"fn", 2)) == 0) + { + _type_check_late(e->alias.to, types); + } + + if (type_is_aggregate(*e)) { + for (size_t i = 0; i < e->aggregate.elements->size; i++) { + auto elem = &e->aggregate.elements->p[i]; + if (!elem->type->ptr_depth) + continue; /* checked early */ + _type_check_late(elem->type, types); + } + } + } } |