casting from vararg to slice. made any a keyword
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 29 Jun 2021 03:17:42 +0000 (22:17 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 29 Jun 2021 03:17:42 +0000 (22:17 -0500)
bin/onyx
core/builtin.onyx
misc/onyx.sublime-syntax
misc/onyx.vim
modules/json/encoder.onyx
src/onyxastnodes.c
src/onyxbuiltins.c
src/onyxwasm.c

index 04e726d99cf7f3e066a353513b94853ac614c8f0..61def4067d2fce92f8f597256dd7c8137d717e57 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 977d37c0f1e1d03138b213fc59fe2a2964fdef54..2b3aa3e12373dd69812b960c957dc3fa64cd908a 100644 (file)
@@ -183,7 +183,7 @@ CallSite :: struct {
 
 
 
-Any :: struct {
+any :: struct {
     data: rawptr;
     type: type_expr;
 }
index 0e95db8ac376e1b1ffa9f81ce8d4583723815a22..6f7a4d7eebc9d379952d902f64a6da096df55de7 100644 (file)
@@ -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'
index 09d6a1c96c5e0d302580a616f5f85657ff6eeca5..08f089104fedb3caabe55288dcdaa91b58dd95b9 100644 (file)
@@ -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
 
index 286a8d5b36c56433768439bfecf0c87aed2df20b..84dceba851231c765fcb880d04c5665e12741184 100644 (file)
@@ -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);
index 15793918da613cad34345abec16ba62ad2eae2b2..d7d9831c006946349fe344fca53b4ced669df14e 100644 (file)
@@ -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
+}
index 65fe57addbb55431a33b778fa3cd773d76f51b44..49f5bbb4f54e5b4b170e5b01dd01bc8145f79992 100644 (file)
@@ -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;
     }
 
index 6ea30958c39efbf022fa1d373306720dec9d72f4..478490493bcb48bfe567140f51cefd148fb53370 100644 (file)
@@ -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;