From: Brendan Hansen Date: Fri, 24 Jul 2020 13:22:17 +0000 (-0500) Subject: Added field access to type signature so types can be from another package X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=6dbe6a3e2b3f6ade79c6385d0f08a718f6cd36f7;p=onyx.git Added field access to type signature so types can be from another package --- diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 66985c1d..d35b0546 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -67,7 +67,6 @@ typedef enum AstKind { Ast_Kind_Binding, Ast_Kind_Function, Ast_Kind_Overloaded_Function, - Ast_Kind_Foreign, Ast_Kind_Block, Ast_Kind_Local_Group, Ast_Kind_Local, @@ -307,7 +306,6 @@ struct AstStructMember { AstTyped_base; u64 offset; }; // Top level nodes struct AstBinding { AstTyped_base; AstNode* node; }; -struct AstForeign { AstNode_base; OnyxToken *mod_token, *name_token; AstNode *import; }; struct AstIncludeFile { AstNode_base; OnyxToken *filename; }; struct AstUsePackage { AstNode_base; @@ -450,4 +448,8 @@ static inline b32 binop_is_assignment(AstBinaryOp* binop) { && binop->operation <= Binary_Op_Assign_End); } +static inline b32 node_is_type(AstNode* node) { + return (node->kind > Ast_Kind_Type_Start) && (node->kind < Ast_Kind_Type_End); +} + #endif // #ifndef ONYXASTNODES_H diff --git a/onyx b/onyx index 84af5265..0c456e0e 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/ez.onyx b/progs/ez.onyx index 69479551..e7d01677 100644 --- a/progs/ez.onyx +++ b/progs/ez.onyx @@ -1,24 +1,34 @@ use "progs/print_funcs" -use package printing +use package printing as p { + print_f32, PrintableArray +} Foo :: struct { - x : i32; - y : i32; + x : i32; + y : i32; } foo_sum :: proc (foo: ^Foo) -> i32 { - return foo.x + foo.y; + return foo.x + foo.y; +} + +asdf :: proc (pa: PrintableArray) { + for i: 0, pa.len p.print(pa.data[i] as i32); } proc #export "main" { - foo := __heap_start as ^Foo; - foo.x = 123; - foo.y = 321; - print(foo.foo_sum()); + foo := __heap_start as ^Foo; + foo.x = 123; + foo.y = 321; + p.print(foo.foo_sum()); + pa := (__heap_start as i32 + 8) as ^p.PrintableArray; + pa.data = __heap_start as ^u8; + pa.len = 5; + asdf(*pa); - print(1234); + p.print(1234); - print_f32(123.0f); + print_f32(123.0f); } \ No newline at end of file diff --git a/progs/print_funcs.onyx b/progs/print_funcs.onyx index 45d7e965..3f48aab3 100644 --- a/progs/print_funcs.onyx +++ b/progs/print_funcs.onyx @@ -6,6 +6,11 @@ print_f32 :: proc #foreign "host" "print" (value: f32) --- print_i64 :: proc #foreign "host" "print" (value: i64) --- print_f64 :: proc #foreign "host" "print" (value: f64) --- +PrintableArray :: struct { + data: ^u8; + len: i32; +} + print_i32arr :: proc (arr: [] i32, len: i32) { for i: 0, len print(arr[i]); } diff --git a/src/onyxparser.c b/src/onyxparser.c index bc1bf2b2..0867da82 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -798,7 +798,18 @@ static AstType* parse_type(OnyxParser* parser) { else if (parser->curr->type == Token_Type_Symbol) { AstNode* symbol_node = make_node(AstNode, Ast_Kind_Symbol); symbol_node->token = expect_token(parser, Token_Type_Symbol); - *next_insertion = (AstType *) symbol_node; + + if (parser->curr->type == '.') { + consume_token(parser); + AstFieldAccess* field = make_node(AstFieldAccess, Ast_Kind_Field_Access); + field->token = expect_token(parser, Token_Type_Symbol); + field->expr = (AstTyped *) symbol_node; + + *next_insertion = (AstType *) field; + } else { + *next_insertion = (AstType *) symbol_node; + } + next_insertion = NULL; } diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 18e5df55..2ed0ae79 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -77,6 +77,19 @@ static AstType* symres_type(AstType* type) { return (AstType *) symbol_resolve(semstate.curr_scope, ((AstNode *) type)->token); } + if (type->kind == Ast_Kind_Field_Access) { + AstFieldAccess* field = (AstFieldAccess *) type; + symres_field_access(&field); + + if (!node_is_type((AstNode *) field)) { + onyx_message_add(Msg_Type_Literal, + type->token->pos, + "field access did not result in a type"); + } + + return (AstType *) field; + } + // NOTE: Already resolved if (type->kind == Ast_Kind_Basic_Type) return type; @@ -159,6 +172,7 @@ static void symres_size_of(AstSizeOf* so) { } static void symres_field_access(AstFieldAccess** fa) { + if ((*fa)->expr == NULL) return; symres_expression(&(*fa)->expr); if ((*fa)->expr == NULL) return; @@ -195,8 +209,6 @@ static void symres_expression(AstTyped** expr) { *expr = (AstTyped *) symbol_resolve(semstate.curr_scope, ((AstNode *) *expr)->token); break; - // NOTE: This is a good case, since it means the symbol is already resolved - case Ast_Kind_Local: break; case Ast_Kind_Function: case Ast_Kind_NumLit: @@ -214,9 +226,9 @@ static void symres_expression(AstTyped** expr) { symres_expression(&((AstArrayAccess *)(*expr))->expr); break; - default: - DEBUG_HERE; - break; + // NOTE: This is a good case, since it means the symbol is already resolved + case Ast_Kind_Local: + default: break; } } diff --git a/src/onyxutils.c b/src/onyxutils.c index 7031ee4c..f5c27cd4 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -18,7 +18,6 @@ static const char* ast_node_names[] = { "BINDING", "FUNCTION", "OVERLOADED_FUNCTION", - "FOREIGN", "BLOCK", "SCOPE", "LOCAL", @@ -105,7 +104,7 @@ Scope* scope_create(bh_allocator a, Scope* parent) { scope->parent = parent; scope->symbols = NULL; - bh_table_init(global_heap_allocator, scope->symbols, 16); + bh_table_init(global_heap_allocator, scope->symbols, 64); return scope; }