allow for 'sizeof' in array size; simplified binary reader
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 23 Jan 2021 16:32:48 +0000 (10:32 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 23 Jan 2021 16:32:48 +0000 (10:32 -0600)
bin/onyx
core/io/binary.onyx
include/onyxastnodes.h
onyx.exe
src/onyxastnodes.c
src/onyxtypes.c

index cbc214887d5c0fbae532ca68e8e5fc8868ae5d62..f4d0eb568cb89ffd6a7743904b2c8a29498c13b2 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index dbdd9925b7a10e3bc8935fee168ea7283c9912e2..63f5199d259b28650c733cf5923e79fb26256c01 100644 (file)
@@ -1,5 +1,7 @@
 package core.io
 
+use package core
+
 BinaryWriter :: struct {
     stream: ^Stream;
 }
@@ -14,43 +16,13 @@ binary_write_byte :: (use bw: ^BinaryWriter, byte: u8) {
     stream_write_byte(stream, byte);
 }
 
-binary_write_i16 :: (use bw: ^BinaryWriter, i: u16) {
-    v := i; 
-    bytes := *cast(^[2] u8) ^v;
-
-    stream_write(stream, ~~ bytes);
-}
-
-binary_write_i32 :: (use bw: ^BinaryWriter, i: u32) {
-    v := i; 
-    bytes := *cast(^[4] u8) ^v;
-
-    stream_write(stream, ~~ bytes);
-}
-
-binary_write_i64 :: (use bw: ^BinaryWriter, i: u64) {
-    v := i; 
-    bytes := *cast(^[8] u8) ^v;
-
-    stream_write(stream, ~~ bytes);
-}
-
-binary_write_f32 :: (use bw: ^BinaryWriter, f: f32) {
-    v := f; 
-    bytes := *cast(^[4] u8) ^v;
-
-    stream_write(stream, ~~ bytes);
-}
-
-binary_write_f64 :: (use bw: ^BinaryWriter, f: f64) {
-    v := f; 
-    bytes := *cast(^[8] u8) ^v;
-
-    stream_write(stream, ~~ bytes);
+binary_write :: (use bw: ^BinaryWriter, $T: type_expr, v: T) {
+    tmp_for_data := v;
+    stream_write(stream, <[] u8>.{ ~~ ^tmp_for_data, sizeof T });
 }
 
 binary_write_slice :: (use bw: ^BinaryWriter, sl: [] $T, output_size := false) {
-    if output_size do binary_write_i32(bw, sl.count);
+    if output_size do binary_write(bw, i32, sl.count);
 
     bytes := <[] u8>.{
         data = cast(^u8) ^sl.data[0],
@@ -79,39 +51,11 @@ binary_read_byte :: (use br: ^BinaryReader) -> u8 {
     return byte;
 }
 
-binary_read_i16 :: (use br: ^BinaryReader) -> u16 {
-    buf: [2] u8;
+binary_read :: (use br: ^BinaryReader, $T: type_expr) -> T {
+    buf: [sizeof T] u8;
     _, bytes_read := stream_read(stream, ~~ buf);
 
-    return *(cast(^u16) buf);
-}
-
-binary_read_i32 :: (use br: ^BinaryReader) -> u32 {
-    buf: [4] u8;
-    _, bytes_read := stream_read(stream, ~~ buf);
-
-    return *(cast(^u32) buf);
-}
-
-binary_read_i64 :: (use br: ^BinaryReader) -> u64 {
-    buf: [8] u8;
-    _, bytes_read := stream_read(stream, ~~ buf);
-
-    return *(cast(^u64) buf);
-}
-
-binary_read_f32 :: (use br: ^BinaryReader) -> f32 {
-    buf: [4] u8;
-    _, bytes_read := stream_read(stream, ~~ buf);
-
-    return *(cast(^f32) buf);
-}
-
-binary_read_f64 :: (use br: ^BinaryReader) -> f64 {
-    buf: [8] u8;
-    _, bytes_read := stream_read(stream, ~~ buf);
-
-    return *(cast(^f64) buf);
+    return *(cast(^T) buf);
 }
 
 binary_read_slice :: (use br: ^BinaryReader,
@@ -119,19 +63,11 @@ binary_read_slice :: (use br: ^BinaryReader,
     size := 0, read_size := false,
     allocator := context.allocator) -> [] T {
     if size == 0 && read_size {
-        size = binary_read_i32(br);
+        size = binary_read(br, i32);
     }
 
-    sl := memory.make_slice(u8, size * sizeof T);
+    sl := memory.make_slice(u8, size * sizeof T, allocator = allocator);
     _, bytes_read := stream_read(stream, sl);
 
     return <[] T>.{ data = cast(^T) sl.data, count = size };
 }
-
-// This should be possible
-// binary_read :: (use br: ^BinaryReader, $T: type_expr) -> T {
-//     buf: [sizeof T] u8;
-//     _, bytes_read := stream_read(stream, ~~ buf);
-// 
-//     return *(cast(^T) buf);
-// }
\ No newline at end of file
index 7f19f85c53fe413203df212a6f57ebfe0b681ef6..78cce0947444fe066204c5a94ede5d232ea61ff5 100644 (file)
@@ -1040,6 +1040,7 @@ b32 convert_numlit_to_type(AstNumLit* num, Type* type);
 
 b32 type_check_or_auto_cast(AstTyped** pnode, Type* type);
 Type* resolve_expression_type(AstTyped* node);
+i64 get_expression_integer_value(AstTyped* node);
 
 b32 cast_is_legal(Type* from_, Type* to_, char** err_msg);
 char* get_function_name(AstFunction* func);
index be1ae8ce692a0b0de34ac514b737efd94f6cd74c..c8e01198df0113707e7855be0876f69dcb295346 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index 479b4456245e0564732d8e3917cb3f8281ea8bda..1518a750fb61c53acf4e07a73d726159de465c98 100644 (file)
@@ -429,7 +429,8 @@ b32 type_check_or_auto_cast(AstTyped** pnode, Type* type) {
     if (types_are_compatible(node->type, type)) return 1;
     if (node_is_auto_cast((AstNode *) node)) {
         char* dummy;
-        if (!cast_is_legal(((AstUnaryOp *) node)->expr->type, type, &dummy)) {
+        Type* from_type = ((AstUnaryOp *) node)->expr->type;
+        if (!from_type || !cast_is_legal(from_type, type, &dummy)) {
             return 0;
 
         } else {
@@ -493,6 +494,28 @@ Type* resolve_expression_type(AstTyped* node) {
     return node->type;
 }
 
+i64 get_expression_integer_value(AstTyped* node) {
+    resolve_expression_type(node);
+
+    if (node->kind == Ast_Kind_NumLit && type_is_integer(node->type)) {
+        return ((AstNumLit *) node)->value.l;
+    }
+
+    if (node->kind == Ast_Kind_Argument) {
+        return get_expression_integer_value(((AstArgument *) node)->value);
+    }
+
+    if (node->kind == Ast_Kind_Size_Of) {
+        return ((AstSizeOf *) node)->size;
+    }
+
+    if (node->kind == Ast_Kind_Align_Of) {
+        return ((AstAlignOf *) node)->alignment;
+    }
+
+    return 0;
+}
+
 static const b32 cast_legality[][11] = {
     /* I8  */ { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 },
     /* U8  */ { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 },
index 47f65823d7203f851b3b30450b49e09cd6b9d8d1..6b304ed07ee40434110864da40ef47e047d81860 100644 (file)
@@ -341,15 +341,14 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
                 resolve_expression_type((AstTyped *) a_node->count_expr);
 
                 // NOTE: Currently, the count_expr has to be an I32 literal
-                if (a_node->count_expr->kind != Ast_Kind_NumLit
-                    || a_node->count_expr->type->kind != Type_Kind_Basic
+                if (a_node->count_expr->type->kind != Type_Kind_Basic
                     || a_node->count_expr->type->Basic.kind != Basic_Kind_I32) {
                     onyx_report_error(type_node->token->pos, "Array type expects type 'i32' for size, got '%s'.",
                         type_get_name(a_node->count_expr->type));
                     return NULL;
                 }
 
-                count = ((AstNumLit *) a_node->count_expr)->value.i;
+                count = get_expression_integer_value((AstTyped *) a_node->count_expr);
             }
 
             a_type->Array.count = count;