i32 used_argument_count;
};
+typedef struct ForeignReference {
+ AstTyped *module_name;
+ AstTyped *import_name;
+} ForeignReference;
+
// Base Nodes
#define AstNode_base \
char* name;
- OnyxToken* foreign_module;
- OnyxToken* foreign_name;
+ ForeignReference foreign;
};
struct AstParam {
// HACK CLEANUP: This does not need to have a local buried inside of it.
union {
OnyxToken* intrinsic_name;
- // NOTE: Used when the function is declared as foreign
- struct {
- OnyxToken* foreign_module;
- OnyxToken* foreign_name;
- };
+ ForeignReference foreign;
};
struct Entity* entity_header;
AstTyped_base;
Scope* scope;
- OnyxToken *module_name;
+ AstTyped *module_name;
bh_arr(struct Entity *) captured_entities;
u32 foreign_block_number;
AstNumLit* make_int_literal(bh_allocator a, i64 value);
AstNumLit* make_float_literal(bh_allocator a, f64 value);
AstRangeLiteral* make_range_literal(bh_allocator a, AstTyped* low, AstTyped* high);
+AstStrLit* make_string_literal(bh_allocator a, OnyxToken *token);
AstBinaryOp* make_binary_op(bh_allocator a, BinaryOp operation, AstTyped* left, AstTyped* right);
AstArgument* make_argument(bh_allocator a, AstTyped* value);
AstFieldAccess* make_field_access(bh_allocator a, AstTyped* node, char* field);
return rl;
}
+AstStrLit* make_string_literal(bh_allocator a, OnyxToken *token) {
+ AstStrLit *str = onyx_ast_node_new(a, sizeof(AstStrLit), Ast_Kind_StrLit);
+ str->flags |= Ast_Flag_Comptime;
+ str->type_node = builtin_string_type;
+ str->token = token;
+ return str;
+}
+
AstBinaryOp* make_binary_op(bh_allocator a, BinaryOp operation, AstTyped* left, AstTyped* right) {
AstBinaryOp* binop_node = onyx_ast_node_new(a, sizeof(AstBinaryOp), Ast_Kind_Binary_Op);
binop_node->left = left;
func->type = type_build_function_type(context.ast_alloc, func);
if (func->type == NULL) YIELD(func->token->pos, "Waiting for function type to be constructed");
+ if (func->foreign.import_name) {
+ CHECK(expression, &func->foreign.module_name);
+ CHECK(expression, &func->foreign.import_name);
+ }
+
return Check_Success;
}
}
else if (parse_possible_directive(parser, "foreign")) {
- func_def->foreign_module = expect_token(parser, Token_Type_Literal_String);
- func_def->foreign_name = expect_token(parser, Token_Type_Literal_String);
+ func_def->foreign.module_name = parse_expression(parser, 0);
+ func_def->foreign.import_name = parse_expression(parser, 0);
func_def->is_foreign = 1;
}
fb->uses_dyncall = 1;
}
- fb->module_name = expect_token(parser, Token_Type_Literal_String);
+ fb->module_name = parse_expression(parser, 0);
//
// This has a fun implication that there cannot be foreign blocks in the builtin
SYMRES(expression, (AstTyped **) &func->deprecated_warning);
}
+ if (func->foreign.import_name) {
+ SYMRES(expression, &func->foreign.module_name);
+ SYMRES(expression, &func->foreign.import_name);
+ }
+
scope_leave();
return Symres_Success;
if (fb->scope == NULL)
fb->scope = scope_create(context.ast_alloc, current_scope, fb->token->pos);
+ SYMRES(expression, &fb->module_name);
+
+ if (fb->module_name->kind != Ast_Kind_StrLit) {
+ onyx_report_error(fb->token->pos, Error_Critical, "Expected module name to be a compile-time string literal.");
+ return Symres_Error;
+ }
+
bh_arr_each(Entity *, pent, fb->captured_entities) {
Entity *ent = *pent;
if (ent->type == Entity_Type_Function_Header) {
return Symres_Error;
}
- ent->function->foreign_name = ent->function->intrinsic_name; // Hmm... This might not be right?
- ent->function->foreign_module = fb->module_name;
+ ent->function->foreign.import_name = make_string_literal(context.ast_alloc, ent->function->intrinsic_name);
+ ent->function->foreign.module_name = fb->module_name;
ent->function->is_foreign = 1;
ent->function->is_foreign_dyncall = fb->uses_dyncall;
ent->function->entity = NULL;
i32 type_idx = generate_type_idx(mod, fd->type);
char *module, *name;
+ OnyxToken *foreign_module = fd->foreign.module_name->token;
+ OnyxToken *foreign_import = fd->foreign.import_name->token;
if (fd->is_foreign_dyncall) {
- module = bh_aprintf(global_heap_allocator, "dyncall:%b", fd->foreign_module->text, fd->foreign_module->length);
+ module = bh_aprintf(global_heap_allocator, "dyncall:%b", foreign_module->text, foreign_module->length);
char type_encoding[65] = {0};
encode_type_as_dyncall_symbol(type_encoding, fd->type->Function.return_type);
encode_type_as_dyncall_symbol(type_encoding, param->local->type);
}
- name = bh_aprintf(global_heap_allocator, "%b:%s", fd->foreign_name->text, fd->foreign_name->length, type_encoding);
+ name = bh_aprintf(global_heap_allocator, "%b:%s", foreign_import->text, foreign_import->length, type_encoding);
} else {
- module = bh_aprintf(global_heap_allocator, "%b", fd->foreign_module->text, fd->foreign_module->length);
- name = bh_aprintf(global_heap_allocator, "%b", fd->foreign_name->text, fd->foreign_name->length);
+ module = bh_aprintf(global_heap_allocator, "%b", foreign_module->text, foreign_module->length);
+ name = bh_aprintf(global_heap_allocator, "%b", foreign_import->text, foreign_import->length);
}
WasmImport import = {
AstFunction *func = (AstFunction *) fb->scope->symbols[i].value;
if (func->kind != Ast_Kind_Function) continue;
+ OnyxToken *import_name = func->foreign.import_name->token;
u32 func_name_base = foreign_buffer.length;
- u32 func_name_length = func->foreign_name->length;
- bh_buffer_append(&foreign_buffer, func->foreign_name->text, func_name_length);
+ u32 func_name_length = import_name->length;
+ bh_buffer_append(&foreign_buffer, import_name->text, func_name_length);
name_offsets[funcs_length] = func_name_base;
name_lengths[funcs_length] = func_name_length;
bh_buffer_write_u32(&foreign_buffer, func_types[i]);
}
+ OnyxToken *module_name = fb->module_name->token;
u32 name_base = foreign_buffer.length;
- u32 name_length = fb->module_name->length;
- bh_buffer_append(&foreign_buffer, fb->module_name->text, name_length);
+ u32 name_length = module_name->length;
+ bh_buffer_append(&foreign_buffer, module_name->text, name_length);
bh_buffer_align(&foreign_buffer, 8);
foreign_info[index] = foreign_buffer.length;
-#load "core/std"
-
use core {*}
Cell :: u8
boards: [..] Board;
while !io.reader_empty(&reader) {
- array.insert_empty(&boards, boards.count);
- board := &boards[boards.count - 1];
+ board := array.alloc_one(&boards);
board.has_won = false;
memory.set(&board.marked, 0, sizeof typeof board.marked);