From: Brendan Hansen Date: Wed, 1 Jul 2020 14:16:20 +0000 (-0500) Subject: Bug fixes and additions to globals X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9d88212801e7ca4aa8bccf2a20073db7082eeaae;p=onyx.git Bug fixes and additions to globals --- diff --git a/onyx b/onyx index 58c12bc7..eb5f5418 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/other.onyx b/progs/other.onyx index b699b444..253c1a48 100644 --- a/progs/other.onyx +++ b/progs/other.onyx @@ -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 { diff --git a/progs/test.onyx b/progs/test.onyx index 2cc07c3e..54069430 100644 --- a/progs/test.onyx +++ b/progs/test.onyx @@ -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); } } + diff --git a/src/onyx.c b/src/onyx.c index c63b3178..4d3fafca 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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; diff --git a/src/onyxparser.c b/src/onyxparser.c index af39eb91..3a698b6a 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -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; diff --git a/src/onyxtypecheck.c b/src/onyxtypecheck.c index 7730a486..b64bc427 100644 --- a/src/onyxtypecheck.c +++ b/src/onyxtypecheck.c @@ -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 { diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 55af1186..fd9690bf 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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); }