From: Brendan Hansen Date: Thu, 8 Oct 2020 13:36:52 +0000 (-0500) Subject: bugfixes and improvements to random X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=10cf01eac28ae09ce852d79b2bc4a5de3eb84db3;p=onyx.git bugfixes and improvements to random --- diff --git a/core/random.onyx b/core/random.onyx index 3ff7aae2..8b27089e 100644 --- a/core/random.onyx +++ b/core/random.onyx @@ -1,11 +1,14 @@ package core -#private -seed := 8675309 +#private_file seed := 8675309 + +#private_file RANDOM_MULTIPLIER :: 1664525 +#private_file RANDOM_INCREMENT :: 1013904223 +// #private_file RANDOM_MODULUS :: 1 << 32 random_seed :: proc (s: u32) do seed = s; random :: proc (s := ^seed) -> u32 { - *s = *s * 5831 + 102847; + *s = *s * RANDOM_MULTIPLIER + RANDOM_INCREMENT; return *s; } diff --git a/onyx b/onyx index 59f54e4c..77f2b16c 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 407a60bc..bbd7fcf3 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -41,20 +41,71 @@ static inline void fill_in_type(AstTyped* node) { node->type = type_build_from_ast(semstate.allocator, node->type_node); } +/* +// NOTE: Returns 1 if the conversion was successful. +static b32 convert_numlit_to_type(AstNumLit* num, Type* type) { + fill_in_type((AstTyped *) num); + assert(num->type); + + if (types_are_compatible(num->type, type)) return 1; + + if (!type_is_numeric(type)) return 0; + + if (num->type->Basic.flags & Basic_Flag_Integer) { + + // + // Integer literal auto cast rules: + // - Up in size always works + // - Down in size only works if value is in range of smaller type. + // - Cast to float only works if value is less than the maximum precise value for float size. + // + + if (type->Basic.flags & Basic_Flag_Integer) { + if (num->type->Basic.size < type->Basic.size) { + num->value.l = (i64) num->value.i; + num->type = type; + return 1; + } + } + + if (type->Basic.flags & Basic_Flag_Float) { + + } + + } + else if (num->type->Basic.flags & Basic_Flag_Float) { + // NOTE: Floats don't cast to integers implicitly. + if ((type->Basic.flags & Basic_Flag_Float) == 0) return 0; + + if (num->type->Basic.kind == Basic_Kind_F32 && type->Basic.kind == Basic_Kind_F64) num->value.d = (f64) num->value.f; + else return 0; + + num->type = type; + + return 1; + } + + return 0; +} */ + // NOTE: Returns 0 if it was not possible to make the types compatible. static b32 type_check_or_auto_cast(AstTyped* node, Type* type) { assert(type != NULL); assert(node != NULL); if (types_are_compatible(node->type, type)) return 1; - if (!node_is_auto_cast((AstNode *) node)) return 0; - - // If the node is an auto cast, we convert it to a cast node which will reports errors if - // the cast is illegal in the code generation. - ((AstUnaryOp *) node)->type = type; - ((AstUnaryOp *) node)->operation = Unary_Op_Cast; + if (node_is_auto_cast((AstNode *) node)) { + // If the node is an auto cast, we convert it to a cast node which will reports errors if + // the cast is illegal in the code generation. + ((AstUnaryOp *) node)->type = type; + ((AstUnaryOp *) node)->operation = Unary_Op_Cast; + return 1; + } + else if (node->kind == Ast_Kind_NumLit) { + // if (convert_numlit_to_type((AstNumLit *) node, type)) return 1; + } - return 1; + return 0; } b32 check_return(AstReturn* retnode) { diff --git a/src/onyxparser.c b/src/onyxparser.c index a8180c3e..cc389240 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -408,6 +408,7 @@ static AstTyped* parse_factor(OnyxParser* parser) { AstStrLit* str_node = make_node(AstStrLit, Ast_Kind_StrLit); str_node->token = expect_token(parser, Token_Type_Literal_String); str_node->addr = 0; + str_node->flags |= Ast_Flag_Comptime; add_node_to_process(parser, (AstNode *) str_node); diff --git a/src/onyxsymres.c b/src/onyxsymres.c index af88d6f9..34aa440b 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -796,6 +796,7 @@ void onyx_resolve_symbols() { case Entity_Type_Enum: symres_enum(entity->enum_type); break; case Entity_Type_Memory_Reservation: symres_memres(&entity->mem_res); break; case Entity_Type_Polymorphic_Proc: symres_polyproc(entity->poly_proc); break; + case Entity_Type_String_Literal: symres_expression(&entity->expr); break; default: break; } diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 23ff7745..3d5acfc1 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -2879,6 +2879,16 @@ static void emit_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) { static void emit_raw_data(OnyxWasmModule* mod, ptr data, AstTyped* node) { switch (node->kind) { + case Ast_Kind_StrLit: { + AstStrLit* sl = (AstStrLit *) node; + + // NOTE: This assumes the address and the length fields have been filled out + // by emit_string_literal. + u32* sdata = (u32 *) data; + sdata[0] = sl->addr; + sdata[1] = sl->length; + break; + } case Ast_Kind_NumLit: { switch (node->type->Basic.kind) { case Basic_Kind_Bool: