From: Brendan Hansen Date: Sat, 23 Jan 2021 16:32:48 +0000 (-0600) Subject: allow for 'sizeof' in array size; simplified binary reader X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=44f991c22bd5b2e04af26723550ca02fe097df65;p=onyx.git allow for 'sizeof' in array size; simplified binary reader --- diff --git a/bin/onyx b/bin/onyx index cbc21488..f4d0eb56 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/io/binary.onyx b/core/io/binary.onyx index dbdd9925..63f5199d 100644 --- a/core/io/binary.onyx +++ b/core/io/binary.onyx @@ -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 diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 7f19f85c..78cce094 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -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); diff --git a/onyx.exe b/onyx.exe index be1ae8ce..c8e01198 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index 479b4456..1518a750 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -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 }, diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 47f65823..6b304ed0 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -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;