From: Brendan Hansen Date: Tue, 29 Jun 2021 03:17:42 +0000 (-0500) Subject: casting from vararg to slice. made any a keyword X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ed60ee1b9b7a8052fcd3a802820c35ad7f56a901;p=onyx.git casting from vararg to slice. made any a keyword --- diff --git a/bin/onyx b/bin/onyx index 04e726d9..61def406 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/builtin.onyx b/core/builtin.onyx index 977d37c0..2b3aa3e1 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -183,7 +183,7 @@ CallSite :: struct { -Any :: struct { +any :: struct { data: rawptr; type: type_expr; } diff --git a/misc/onyx.sublime-syntax b/misc/onyx.sublime-syntax index 0e95db8a..6f7a4d7e 100644 --- a/misc/onyx.sublime-syntax +++ b/misc/onyx.sublime-syntax @@ -29,7 +29,7 @@ contexts: - match: '\b(package|struct|use|global|enum|if|elseif|else|for|while|do|break|continue|fallthrough|return|as|cast|sizeof|alignof|defer|switch|case)\b' scope: keyword.control.onyx - - match: '\b(bool|void|i8|u8|i16|u16|i32|u32|i64|u64|f32|f64|rawptr|str|cstr|range|type_expr)\b' + - match: '\b(bool|void|i8|u8|i16|u16|i32|u32|i64|u64|f32|f64|rawptr|str|cstr|range|type_expr|any)\b' scope: storage.type - match: '\b(i8x16|i16x8|i32x4|i64x2|f32x4|f64x2|v128)\b' diff --git a/misc/onyx.vim b/misc/onyx.vim index 09d6a1c9..08f08910 100644 --- a/misc/onyx.vim +++ b/misc/onyx.vim @@ -25,7 +25,7 @@ syn keyword onyxType f32 f64 syn keyword onyxType rawptr syn keyword onyxType str cstr syn keyword onyxType i8x16 i16x8 i32x4 i64x2 f32x4 f64x2 v128 -syn keyword onyxType type_expr +syn keyword onyxType type_expr any syn keyword onyxConstant true false null null_proc diff --git a/modules/json/encoder.onyx b/modules/json/encoder.onyx index 286a8d5b..84dceba8 100644 --- a/modules/json/encoder.onyx +++ b/modules/json/encoder.onyx @@ -21,6 +21,11 @@ encode_string :: (v: $T, allocator := context.allocator) -> (str, Encoding_Error return s, .None; } +// +// This could be changed to use the "any" type now, which would allow for any type to be +// represented as a json value. However, this eliminates the control that you get from +// this way. +// encode :: #match { (w: ^io.Writer, v: i32) -> Encoding_Error { io.write_i32(w, ~~v); diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index 15793918..d7d9831c 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -615,6 +615,15 @@ b32 cast_is_legal(Type* from_, Type* to_, char** err_msg) { } } + if (to->kind == Type_Kind_Slice && from->kind == Type_Kind_VarArgs) { + if (!types_are_compatible(to->Slice.ptr_to_data->Pointer.elem, from->VarArgs.ptr_to_data->Pointer.elem)) { + *err_msg = "Variadic argument to slice cast is not valid here because the types are different."; + return 0; + } else { + return 1; + } + } + if (from->kind == Type_Kind_Slice || to->kind == Type_Kind_Slice) { *err_msg = "Cannot cast to or from a slice."; return 0; @@ -881,4 +890,4 @@ b32 static_if_resolution(AstIf* static_if) { assert(condition_value->kind == Ast_Kind_NumLit); // This should be right, right? return condition_value->value.i != 0; -} \ No newline at end of file +} diff --git a/src/onyxbuiltins.c b/src/onyxbuiltins.c index 65fe57ad..49f5bbb4 100644 --- a/src/onyxbuiltins.c +++ b/src/onyxbuiltins.c @@ -392,9 +392,9 @@ void initialize_builtins(bh_allocator a) { return; } - builtin_any_type = (AstType *) symbol_raw_resolve(p->scope, "Any"); + builtin_any_type = (AstType *) symbol_raw_resolve(p->scope, "any"); if (builtin_any_type == NULL) { - onyx_report_error((OnyxFilePos) { 0 }, "'Any' struct not found in builtin package."); + onyx_report_error((OnyxFilePos) { 0 }, "'any' struct not found in builtin package."); return; } diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 6ea30958..47849049 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -2648,6 +2648,11 @@ EMIT_FUNC(cast, AstUnaryOp* cast) { return; } + if (to->kind == Type_Kind_Slice && from->kind == Type_Kind_VarArgs) { + // Nothing needs to be done because they are identical + return; + } + i32 fromidx = -1, toidx = -1; if (from->Basic.flags & Basic_Flag_Pointer || from->kind == Type_Kind_Array) { fromidx = 10;