small changes and improvements
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 16 Jul 2020 21:21:02 +0000 (16:21 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 16 Jul 2020 21:21:02 +0000 (16:21 -0500)
include/onyxastnodes.h
include/onyxtypes.h
onyx
progs/other.onyx
src/onyxchecker.c
src/onyxparser.c
src/onyxtypes.c
src/onyxwasm.c

index 9a80bb6c2db20e08780e6a1c92a7a67aa371eaf3..189a3f0f14bc13f2d8e28bf9db1d46d1cde81fa0 100644 (file)
@@ -188,8 +188,11 @@ struct AstFunction      {
     AstBlock *body;
     AstLocal *params;
 
-    // NOTE: Used when a function is exported with a specific name
-    char* exported_name;
+    union {
+        // NOTE: Used when a function is exported with a specific name
+        OnyxToken* exported_name;
+        OnyxToken* intrinsic_name;
+    };
 
     // NOTE: Used when the function is declared as foreign
     OnyxToken* foreign_module;
index 1aa719def55682ba832a130237fad2bc7baeffa8..99af695c490d26f1662bb56ff5dded1f32a541c4 100644 (file)
@@ -89,5 +89,6 @@ Type* type_build_from_ast(bh_allocator alloc, struct AstType* type_node);
 const char* type_get_name(Type* type);
 
 b32 type_is_pointer(Type* type);
+b32 type_is_bool(Type* type);
 
 #endif // #ifndef ONYX_TYPES
diff --git a/onyx b/onyx
index ceca3e2326da7225053462373ae7903b2a73f873..9d590f0dfd7a1948f0226e39658a488e6c90b6b0 100755 (executable)
Binary files a/onyx and b/onyx differ
index cb576406458a4bf7ea25b0c93e17d4a04f3684f2..70b9a52e273ef12228f83f8cc807fec053851c31 100644 (file)
@@ -1,3 +1,5 @@
+use "progs/test"
+
 other_value :: proc (n: i32) -> i32 {
     return 8675309 + something_else(n) + global_value;
 }
index 120e551490d0065588e74f6d99f31f7e5795c8ba..51e3d552947b6522809e7ca1cc9c9937c941787e 100644 (file)
@@ -89,10 +89,7 @@ static b32 check_return(OnyxSemPassState* state, AstReturn* retnode) {
 static b32 check_if(OnyxSemPassState* state, AstIf* ifnode) {
     if (check_expression(state, ifnode->cond)) return 1;
 
-    if (ifnode->cond->type == NULL
-            || ifnode->cond->type->kind != Type_Kind_Basic
-            || ifnode->cond->type->Basic.kind != Basic_Kind_Bool) {
-
+    if (!type_is_bool(ifnode->cond->type)) {
         onyx_message_add(state->msgs,
                 ONYX_MESSAGE_TYPE_LITERAL,
                 ifnode->cond->token->pos,
@@ -109,10 +106,7 @@ static b32 check_if(OnyxSemPassState* state, AstIf* ifnode) {
 static b32 check_while(OnyxSemPassState* state, AstWhile* whilenode) {
     if (check_expression(state, whilenode->cond)) return 1;
 
-    if (whilenode->cond->type == NULL
-            || whilenode->cond->type->kind != Type_Kind_Basic
-            || whilenode->cond->type->Basic.kind != Basic_Kind_Bool) {
-
+    if (!type_is_bool(whilenode->cond->type)) {
         onyx_message_add(state->msgs,
                 ONYX_MESSAGE_TYPE_LITERAL,
                 whilenode->cond->token->pos,
@@ -152,9 +146,9 @@ static b32 check_call(OnyxSemPassState* state, AstCall* call) {
         call->base.kind = Ast_Kind_Intrinsic_Call;
         call->callee = NULL;
 
-        onyx_token_null_toggle(callee->base.token);
+        onyx_token_null_toggle(callee->intrinsic_name);
 
-        char* intr_name = callee->base.token->text;
+        char* intr_name = callee->intrinsic_name->text;
         OnyxIntrinsic intrinsic = ONYX_INTRINSIC_UNDEFINED;
 
         if (!strcmp("memory_size", intr_name))       intrinsic = ONYX_INTRINSIC_MEMORY_SIZE;
@@ -206,7 +200,7 @@ static b32 check_call(OnyxSemPassState* state, AstCall* call) {
 
         ((AstIntrinsicCall *)call)->intrinsic = intrinsic;
 
-        onyx_token_null_toggle(callee->base.token);
+        onyx_token_null_toggle(callee->intrinsic_name);
     }
 
     call->base.type = callee->base.type->Function.return_type;
@@ -508,6 +502,14 @@ static b32 check_function(OnyxSemPassState* state, AstFunction* func) {
                     "exporting a inlined function");
             return 1;
         }
+
+        if (func->exported_name == NULL) {
+            onyx_message_add(state->msgs,
+                    ONYX_MESSAGE_TYPE_LITERAL,
+                    func->base.token->pos,
+                    "exporting function without a name");
+            return 1;
+        }
     }
 
     state->expected_return_type = func->base.type->Function.return_type;
index e0260c1ab183ae3f3875bbf8a9a444a3a8b79197..4879993591e9fc40f110fe59cdfde0cff6bf2b35 100644 (file)
@@ -734,13 +734,18 @@ static b32 parse_possible_directive(OnyxParser* parser, const char* dir) {
 }
 
 static AstFunction* parse_function_definition(OnyxParser* parser) {
-    expect(parser, Token_Type_Keyword_Proc);
 
     AstFunction* func_def = make_node(AstFunction, Ast_Kind_Function);
+    func_def->base.token = expect(parser, Token_Type_Keyword_Proc);
 
     while (parser->curr_token->type == '#') {
         if (parse_possible_directive(parser, "intrinsic")) {
             func_def->base.flags |= Ast_Flag_Intrinsic;
+
+            if (parser->curr_token->type == Token_Type_Literal_String) {
+                OnyxToken* str_token = expect(parser, Token_Type_Literal_String);
+                func_def->intrinsic_name = str_token;
+            }
         }
 
         else if (parse_possible_directive(parser, "inline")) {
@@ -759,11 +764,7 @@ static AstFunction* parse_function_definition(OnyxParser* parser) {
 
             if (parser->curr_token->type == Token_Type_Literal_String) {
                 OnyxToken* str_token = expect(parser, Token_Type_Literal_String);
-                func_def->exported_name =
-                    bh_aprintf(global_heap_allocator,
-                            "%b",
-                            str_token->text,
-                            str_token->length);
+                func_def->exported_name = str_token;
             }
         }
 
@@ -884,7 +885,10 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) {
                 AstTyped* node = parse_top_level_constant_symbol(parser);
 
                 if (node->kind == Ast_Kind_Function) {
-                    node->token = symbol;
+                    AstFunction* func = (AstFunction *) node;
+
+                    if (func->exported_name == NULL)
+                        func->exported_name = symbol;
                 } else {
                     // HACK
                     bh_arr_push(parser->results.nodes_to_process, (AstNode *) node);
index 10746fdf5777222fd5161795daf82f692a7fcc16..44605492b2502642bcc5061772999268a5f6e1e7 100644 (file)
@@ -106,3 +106,7 @@ const char* type_get_name(Type* type) {
 b32 type_is_pointer(Type* type) {
     return type->kind == Type_Kind_Pointer || (type->Basic.flags & Basic_Flag_Pointer) != 0;
 }
+
+b32 type_is_bool(Type* type) {
+    return type != NULL && type->kind == Type_Kind_Basic && type->Basic.kind == Basic_Kind_Bool;
+}
index 8733c75b12be7f5b658a2ce3eb8674683a65681c..66123bff807320364dc7159a24f804302419887d 100644 (file)
@@ -826,7 +826,7 @@ static void compile_function(OnyxWasmModule* mod, AstFunction* fd) {
     bh_arr_new(mod->allocator, wasm_func.code, 4);
 
     if (fd->base.flags & Ast_Flag_Exported) {
-        onyx_token_null_toggle(fd->base.token);
+        onyx_token_null_toggle(fd->exported_name);
 
         i32 func_idx = (i32) bh_imap_get(&mod->func_map, (u64) fd);
 
@@ -834,10 +834,10 @@ static void compile_function(OnyxWasmModule* mod, AstFunction* fd) {
             .kind = WASM_FOREIGN_FUNCTION,
             .idx = func_idx,
         };
-        bh_table_put(WasmExport, mod->exports, fd->base.token->text, wasm_export);
+        bh_table_put(WasmExport, mod->exports, fd->exported_name->text, wasm_export);
         mod->export_count++;
 
-        onyx_token_null_toggle(fd->base.token);
+        onyx_token_null_toggle(fd->exported_name);
     }
 
     // If there is no body then don't process the code