other_value :: proc (n i32) -> i32 {
- return 8675309 + something_else(n);
+ return 8675309 + something_else(n) + global_value;
}
export fib :: proc (n i32) -> i32 {
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
// 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);
}
}
-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);
}
}
+
case ONYX_COMPILE_ACTION_COMPILE:
compiler_progress = onyx_compile(&compile_opts, &compile_state);
-
break;
default: break;
}
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);
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;
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;
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 {
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);
}