Bugfixes for foreign function calls
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 19 Jun 2020 17:37:49 +0000 (12:37 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 19 Jun 2020 17:37:49 +0000 (12:37 -0500)
include/onyxparser.h
onyx
progs/minimal.onyx
src/onyxparser.c
src/onyxwasm.c

index b80e2cb97937aecd14e304bf70a2b4fbb0dd15e5..df4adab62000b4944947b8e6ca1cfd5d5122efe6 100644 (file)
@@ -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 be18df102da38964c6521c4c42c8cebd0f0ac8d6..86ccaf9ebe9972c33c0e141faf14cae4bed9c0cb 100755 (executable)
Binary files a/onyx and b/onyx differ
index def47486d713ee147048974c0903db0e99820def..0218c5172249a6f08f3fedccd8e07cc8960c19d9 100644 (file)
@@ -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());
 }
index c98c1609200e87e4ab59376bedfaf19205a0258e..4a65939a6a1733d5a6a05c250c17c6bc80d8322a 100644 (file)
@@ -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;
index e457780359b1d22101c966cc0db0499a1c74644d..27760df304365c0f8dd63af4839d7d8cda53e41d 100644 (file)
@@ -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,