Add array:reversed()
This commit is contained in:
parent
135e23094c
commit
d94053ca77
@ -223,6 +223,14 @@ public array_t Array__slice(array_t *array, int64_t first, int64_t length, int64
|
||||
};
|
||||
}
|
||||
|
||||
public array_t Array__reversed(array_t array)
|
||||
{
|
||||
array_t reversed = array;
|
||||
reversed.stride = -array.stride;
|
||||
reversed.data = array.data + (array.length-1)*array.stride;
|
||||
return reversed;
|
||||
}
|
||||
|
||||
public array_t Array__concat(array_t x, array_t y, const TypeInfo *type)
|
||||
{
|
||||
int64_t item_size = get_item_size(type);
|
||||
|
@ -63,6 +63,7 @@ void Array__clear(array_t *array);
|
||||
void Array__compact(array_t *arr, const TypeInfo *type);
|
||||
bool Array__contains(array_t array, void *item, const TypeInfo *type);
|
||||
array_t Array__slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type);
|
||||
array_t Array__reversed(array_t array);
|
||||
array_t Array__concat(array_t x, array_t y, const TypeInfo *type);
|
||||
uint32_t Array__hash(const array_t *arr, const TypeInfo *type);
|
||||
int32_t Array__compare(const array_t *x, const array_t *y, const TypeInfo *type);
|
||||
|
@ -1493,6 +1493,9 @@ CORD compile(env_t *env, ast_t *ast)
|
||||
.next=new(arg_t, .name="stride", .type=Type(IntType, .bits=64), .default_val=FakeAST(Int, .i=1, .bits=64))));
|
||||
return CORD_all("Array__slice(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
|
||||
compile_type_info(env, self_value_t), ")");
|
||||
} else if (streq(call->name, "reversed")) {
|
||||
CORD self = compile_to_pointer_depth(env, call->self, 0, false);
|
||||
return CORD_all("Array__reversed(", self, ")");
|
||||
} else code_err(ast, "There is no '%s' method for arrays", call->name);
|
||||
}
|
||||
case TableType: {
|
||||
|
@ -68,3 +68,8 @@ if yes
|
||||
= @[10, 20, 30]
|
||||
>> copy
|
||||
= [10, 20]
|
||||
|
||||
if yes
|
||||
>> arr := [10, 20, 30]
|
||||
>> arr:reversed()
|
||||
= [30, 20, 10]
|
||||
|
@ -510,6 +510,7 @@ type_t *get_type(env_t *env, ast_t *ast)
|
||||
return Type(PointerType, .pointed=Match(self_value_t, ArrayType)->item_type, .is_optional=true, .is_readonly=true);
|
||||
else if (streq(call->name, "clear")) return Type(VoidType);
|
||||
else if (streq(call->name, "slice")) return self_value_t;
|
||||
else if (streq(call->name, "reversed")) return self_value_t;
|
||||
else code_err(ast, "There is no '%s' method for arrays", call->name);
|
||||
}
|
||||
case TableType: {
|
||||
|
Loading…
Reference in New Issue
Block a user