From: Brendan Hansen Date: Fri, 19 Jun 2020 17:37:49 +0000 (-0500) Subject: Bugfixes for foreign function calls X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=4e3100f89d90277b1719f13002b18a596089db66;p=onyx.git Bugfixes for foreign function calls --- diff --git a/include/onyxparser.h b/include/onyxparser.h index b80e2cb9..df4adab6 100644 --- a/include/onyxparser.h +++ b/include/onyxparser.h @@ -57,6 +57,7 @@ typedef enum OnyxAstNodeKind { ONYX_AST_NODE_KIND_LITERAL, ONYX_AST_NODE_KIND_CAST, ONYX_AST_NODE_KIND_PARAM, + ONYX_AST_NODE_KIND_ARGUMENT, ONYX_AST_NODE_KIND_CALL, ONYX_AST_NODE_KIND_ASSIGNMENT, ONYX_AST_NODE_KIND_RETURN, diff --git a/onyx b/onyx index be18df10..86ccaf9e 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/minimal.onyx b/progs/minimal.onyx index def47486..0218c517 100644 --- a/progs/minimal.onyx +++ b/progs/minimal.onyx @@ -1,5 +1,7 @@ -print :: foreign "host" "print" proc (value i32) --- +print :: foreign "host" "print" proc (value i32) --- +print_float :: foreign "host" "print" proc (value f32) --- +print_if :: foreign "host" "print" proc (i i32, f f32) --- foo :: proc -> i32 { return 10 as i32; @@ -21,10 +23,18 @@ export do_stuff :: proc -> i32 { res := diff_square((4 + 5) as i32, (2 + 3) as i32); res = res + foo(); // res should be 66 - return res; + return res * (0 - 1); +} + +export float_test :: proc -> f32 { + return (1.2f + 5.3f); } export main :: proc { - output := do_stuff() * (0 - 2); + output := do_stuff(); + print(output); + print_float(float_test()); + + print_if(output, float_test()); } diff --git a/src/onyxparser.c b/src/onyxparser.c index c98c1609..4a65939a 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -23,6 +23,7 @@ static const char* ast_node_names[] = { "LITERAL", "CAST", "PARAM", + "ARGUMENT", "CALL", "ASSIGN", "RETURN", @@ -269,7 +270,9 @@ static OnyxAstNode* parse_factor(OnyxParser* parser) { OnyxAstNode** prev = &call_node->arguments; OnyxAstNode* curr = NULL; while (parser->curr_token->type != TOKEN_TYPE_CLOSE_PAREN) { - curr = parse_expression(parser); + curr = onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_ARGUMENT); + curr->left = parse_expression(parser); + curr->type = curr->left->type; if (curr != NULL && curr->kind != ONYX_AST_NODE_KIND_ERROR) { *prev = curr; diff --git a/src/onyxwasm.c b/src/onyxwasm.c index e4577803..27760df3 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -373,7 +373,7 @@ static void process_expression(OnyxWasmModule* mod, WasmFunc* func, OnyxAstNode* { OnyxAstNodeCall* call = &expr->as_call; forll (OnyxAstNode, arg, call->arguments, next) { - process_expression(mod, func, arg); + process_expression(mod, func, arg->left); } i32 func_idx = (i32) bh_imap_get(&mod->func_map, (u64) call->callee); @@ -553,6 +553,8 @@ static void process_foreign(OnyxWasmModule* module, OnyxAstNodeForeign* foreign) if (foreign->import->kind == ONYX_AST_NODE_KIND_FUNCDEF) { i32 type_idx = generate_type_idx(module, &foreign->import->as_funcdef); + bh_imap_put(&module->func_map, (u64) &foreign->import->as_funcdef, module->next_import_func_idx++); + WasmImport import = { .kind = WASM_FOREIGN_FUNCTION, .idx = type_idx,