bugfixes and improvements to random
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 8 Oct 2020 13:36:52 +0000 (08:36 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 8 Oct 2020 13:36:52 +0000 (08:36 -0500)
core/random.onyx
onyx
src/onyxchecker.c
src/onyxparser.c
src/onyxsymres.c
src/onyxwasm.c

index 3ff7aae221eec776d50cb217f6380c5fd1639569..8b27089e8fe5dc7ca31546e132ba9a6720499294 100644 (file)
@@ -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 59f54e4cacfdfa6df99198957a863f6a1a4ef24c..77f2b16c3d606c74b7f55fa8039287fb7ad6ceb3 100755 (executable)
Binary files a/onyx and b/onyx differ
index 407a60bcb0f4f0c68ed2bff46b9baf9772018ffa..bbd7fcf3099ca6820e58b5bd389dc8e18c495850 100644 (file)
@@ -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) {
index a8180c3ed10248634e6f20e29134ab14b295aeb8..cc389240c357eb6a5e336b04e06ecb1ed0993705 100644 (file)
@@ -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);
 
index af88d6f9f22d8252446208cb2a27864e2372718f..34aa440b1311aa3a3db303c7b0cbad5dfcae238b 100644 (file)
@@ -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;
         }
index 23ff7745dec8533511283ba4efed31c0948b15b0..3d5acfc1e4f31c7563bf0af8110476ab7b9511fc 100644 (file)
@@ -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: