From 99a5f2d3082528f35cab2bde1e7b2dee4b45dba6 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 13 Jan 2021 17:26:19 -0600 Subject: [PATCH] arrays can cast to slices of the same type --- src/onyxastnodes.c | 12 ++++++++++-- src/onyxwasm.c | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index 8f8edcde..f0a90cca 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -263,9 +263,8 @@ AstTyped* ast_reduce(bh_allocator a, AstTyped* node) { switch (node->kind) { case Ast_Kind_Binary_Op: return (AstTyped *) ast_reduce_binop(a, (AstBinaryOp *) node); case Ast_Kind_Unary_Op: return (AstTyped *) ast_reduce_unaryop(a, (AstUnaryOp *) node); - case Ast_Kind_NumLit: return node; case Ast_Kind_Enum_Value: return (AstTyped *) ((AstEnumValue *) node)->value; - default: return NULL; + default: return node; } } @@ -510,6 +509,15 @@ b32 cast_is_legal(Type* from_, Type* to_, char** err_msg) { return 0; } + if (to->kind == Type_Kind_Slice && from->kind == Type_Kind_Array) { + if (!types_are_compatible(to->Slice.ptr_to_data->Pointer.elem, from->Array.elem)) { + *err_msg = "Array 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; diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 7ae92870..bd504314 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -2495,6 +2495,12 @@ EMIT_FUNC(cast, AstUnaryOp* cast) { return; } + if (to->kind == Type_Kind_Slice && from->kind == Type_Kind_Array) { + WID(WI_I32_CONST, from->Array.count); + *pcode = code; + return; + } + i32 fromidx = -1, toidx = -1; if (from->Basic.flags & Basic_Flag_Pointer || from->kind == Type_Kind_Array) { fromidx = 10; -- 2.25.1