Added field access to type signature so types can be from another package
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 24 Jul 2020 13:22:17 +0000 (08:22 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 24 Jul 2020 13:22:17 +0000 (08:22 -0500)
include/onyxastnodes.h
onyx
progs/ez.onyx
progs/print_funcs.onyx
src/onyxparser.c
src/onyxsymres.c
src/onyxutils.c

index 66985c1dae3627ad39c6fd0113a43c52d29e53e5..d35b05461080e15559393e416d95ebbc1810ab87 100644 (file)
@@ -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 84af5265d80d74ae66898702014ff5b4ce1c4445..0c456e0e69af43e9faf543b9e2890a03e216d548 100755 (executable)
Binary files a/onyx and b/onyx differ
index 69479551eddad03e5e0bb8c6aa34593aa1bb7869..e7d016778787241c72a401c998894115ebd833a8 100644 (file)
@@ -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
index 45d7e965d7e6f8aa3cce158eed65fa113f37efb5..3f48aab3d9c7008511d4405e5964d42f7d57db81 100644 (file)
@@ -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]);
 }
index bc1bf2b290fbbbc1f642d620ba8a00b376cccd88..0867da82eafae01e0f1da125a160417c783dfe07 100644 (file)
@@ -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;
         }
 
index 18e5df55c18070da95ef4d1288054c8f9507d844..2ed0ae792dee6a177df233bdbc61e5e20a272c3f 100644 (file)
@@ -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;
     }
 }
 
index 7031ee4ce6e836be84916672dbfb9425c5e1bd86..f5c27cd4a15545f662e4a46c2d8958b208324753 100644 (file)
@@ -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;
 }