From: Brendan Hansen Date: Thu, 16 Jul 2020 21:21:02 +0000 (-0500) Subject: small changes and improvements X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=796e3560b05f44571ea30ea7587daa40061a0532;p=onyx.git small changes and improvements --- diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 9a80bb6c..189a3f0f 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -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; diff --git a/include/onyxtypes.h b/include/onyxtypes.h index 1aa719de..99af695c 100644 --- a/include/onyxtypes.h +++ b/include/onyxtypes.h @@ -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 ceca3e23..9d590f0d 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/other.onyx b/progs/other.onyx index cb576406..70b9a52e 100644 --- a/progs/other.onyx +++ b/progs/other.onyx @@ -1,3 +1,5 @@ +use "progs/test" + other_value :: proc (n: i32) -> i32 { return 8675309 + something_else(n) + global_value; } diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 120e5514..51e3d552 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -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; diff --git a/src/onyxparser.c b/src/onyxparser.c index e0260c1a..48799935 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -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); diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 10746fdf..44605492 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -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; +} diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 8733c75b..66123bff 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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