Bug fixes and additions to globals
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 1 Jul 2020 14:16:20 +0000 (09:16 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 1 Jul 2020 14:16:20 +0000 (09:16 -0500)
onyx
progs/other.onyx
progs/test.onyx
src/onyx.c
src/onyxparser.c
src/onyxtypecheck.c
src/onyxwasm.c

diff --git a/onyx b/onyx
index 58c12bc7527a5e1e8d8915fb9b5ba72aae880c16..eb5f54188bf77f3602b68b7d3035a7919f85bbc0 100755 (executable)
Binary files a/onyx and b/onyx differ
index b699b444f2241a80cd4f5214f48d2c453d89405f..253c1a489c681017d7caf190005ab4ff1555b56e 100644 (file)
@@ -1,6 +1,6 @@
 
 other_value :: proc (n i32) -> i32 {
-    return 8675309 + something_else(n);
+    return 8675309 + something_else(n) + global_value;
 }
 
 export fib :: proc (n i32) -> i32 {
index 2cc07c3e70ab1768a5ebaf01a1311a9ef186196c..540694305104e24e123180d47ba711317dabc5e2 100644 (file)
@@ -11,7 +11,7 @@ print_i64 :: foreign "host" "print" proc (value i64) ---
 print_f64 :: foreign "host" "print" proc (value f64) ---
 
 something_else :: proc (n i32) -> i32 {
-    return 100 * n;
+    return 100 * n + global_value;
 }
 
 // symbol :: proc {}            Global function
@@ -20,16 +20,11 @@ something_else :: proc (n i32) -> i32 {
 // symbol :: foreign "" "" i32  Global foreign mutable i32
 // symbol :: 5                  Global constant value
 // symbol := 5                  Global mutable variable
-// symbol : i32                 Global mutable i32 (defaults to 0 initial value)
 
-global_value := 5;
-foreign_global :: foreign "dummy" "global" i32
+export global_value := 100
 
 // This is the entry point
-export main :: proc {
-    print_i32(global_value);
-    print_i32(foreign_global);
-
+export main2 :: proc {
     i := 0;
     while i < 10 {
         res :: fib(i);
@@ -65,14 +60,19 @@ export main :: proc {
     }
 }
 
-export main2 :: proc {
+export main :: proc {
     big_num := fib(factorial(4));
-    something :: other_value(3);
+    something :: other_value(0);
+
+    global_value = 1000;
+    something_else :: other_value(0);
 
     condition := big_num < something;
 
     if condition {
         print_i32(big_num);
         print_i32(something);
+        print_i32(something_else);
     }
 }
+
index c63b3178d7368ff305d162a191f5be9a910e0661..4d3fafca35ba8179becc88fd312b56a09964ddde 100644 (file)
@@ -221,7 +221,6 @@ int main(int argc, char *argv[]) {
 
         case ONYX_COMPILE_ACTION_COMPILE:
             compiler_progress = onyx_compile(&compile_opts, &compile_state);
-
             break;
 
         default: break;
index af39eb91cc4165f575cd54d3ec83b0cb2beed79c..3a698b6abc4fcc7023cd72577868f32caf9007e4 100644 (file)
@@ -278,6 +278,8 @@ static OnyxAstNode* parse_factor(OnyxParser* parser) {
     }
 
     if (parser->curr_token->type == TOKEN_TYPE_KEYWORD_CAST) {
+        parser_next_token(parser);
+
         OnyxAstNodeUnaryOp* cast_node = onyx_ast_node_new(parser->allocator, ONYX_AST_NODE_KIND_UNARY_OP);
         cast_node->operation = ONYX_UNARY_OP_CAST;
         cast_node->type = parse_type(parser);
@@ -784,11 +786,21 @@ static OnyxAstNode* parse_top_level_statement(OnyxParser* parser) {
 
                 expect(parser, TOKEN_TYPE_SYM_COLON);
 
+                OnyxTypeInfo* type = &builtin_types[ONYX_TYPE_INFO_KIND_UNKNOWN];
+
+                if (parser->curr_token->type == TOKEN_TYPE_SYMBOL) {
+                    type = parse_type(parser);
+                }
+
                 if (parser->curr_token->type == TOKEN_TYPE_SYM_COLON) {
                     parser_next_token(parser);
 
                     OnyxAstNode* node = parse_top_level_constant_symbol(parser);
 
+                    if (node->kind == ONYX_AST_NODE_KIND_GLOBAL) {
+                        node->type = type;
+                    }
+
                     if (node->kind == ONYX_AST_NODE_KIND_FOREIGN) {
                         node->as_foreign.import->token = symbol;
 
@@ -805,7 +817,7 @@ static OnyxAstNode* parse_top_level_statement(OnyxParser* parser) {
                     global->token = symbol;
                     global->flags |= ONYX_AST_FLAG_LVAL;
                     global->initial_value = parse_expression(parser);
-                    global->type = &builtin_types[ONYX_TYPE_INFO_KIND_UNKNOWN];
+                    global->type = type;
 
                     return (OnyxAstNode *) global;
 
index 7730a486f30e3b34332f79b2f446e0949a490b8b..b64bc42765bec7963d24dd8c19bdeb2f9a737aa8 100644 (file)
@@ -270,7 +270,7 @@ static void typecheck_global(OnyxSemPassState* state, OnyxAstNodeGlobal* global)
                         ONYX_MESSAGE_TYPE_GLOBAL_TYPE_MISMATCH,
                         global->token->pos,
                         global->token->token, global->token->length,
-                        global->type->name, global->initial_value->type);
+                        global->type->name, global->initial_value->type->name);
                 return;
             }
         } else {
index 55af1186b25874cbf8cc8ad6273c3e9f63a61c97..fd9690bf65452cb4538b18bd354bfc514f1c43ee 100644 (file)
@@ -766,6 +766,21 @@ static void compile_global_declaration(OnyxWasmModule* module, OnyxAstNodeGlobal
         return;
     }
 
+    if ((global->flags & ONYX_AST_FLAG_EXPORTED) != 0) {
+        onyx_token_null_toggle(*global->token);
+
+        i32 global_idx = (i32) bh_imap_get(&module->func_map, (u64) global);
+
+        WasmExport wasm_export = {
+            .kind = WASM_FOREIGN_GLOBAL,
+            .idx = global_idx,
+        };
+        bh_table_put(WasmExport, module->exports, global->token->token, wasm_export);
+        module->export_count++;
+
+        onyx_token_null_toggle(*global->token);
+    }
+
     compile_expression(module, &glob.initial_value, global->initial_value);
     bh_arr_push(module->globals, glob);
 }