2017-11-01 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Iniializes an array */
|
2017-11-28 23:27:55 +00:00
|
|
|
DstArray *dst_array_init(DstArray *array, int32_t capacity) {
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst *data = NULL;
|
2017-12-17 04:11:51 +00:00
|
|
|
if (capacity > 0) {
|
2018-01-06 16:09:15 +00:00
|
|
|
data = (Dst *) malloc(sizeof(Dst) * capacity);
|
2017-12-17 04:11:51 +00:00
|
|
|
if (NULL == data) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
array->count = 0;
|
|
|
|
array->capacity = capacity;
|
|
|
|
array->data = data;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
void dst_array_deinit(DstArray *array) {
|
|
|
|
free(array->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates a new array */
|
2017-11-28 23:27:55 +00:00
|
|
|
DstArray *dst_array(int32_t capacity) {
|
2017-12-21 04:03:34 +00:00
|
|
|
DstArray *array = dst_gcalloc(DST_MEMORY_ARRAY, sizeof(DstArray));
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_array_init(array, capacity);
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Ensure the array has enough capacity for elements */
|
2017-11-28 23:27:55 +00:00
|
|
|
void dst_array_ensure(DstArray *array, int32_t capacity) {
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst *newData;
|
|
|
|
Dst *old = array->data;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (capacity <= array->capacity) return;
|
2018-01-06 16:09:15 +00:00
|
|
|
newData = realloc(old, capacity * sizeof(Dst));
|
2017-11-01 21:53:43 +00:00
|
|
|
if (NULL == newData) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
array->data = newData;
|
|
|
|
array->capacity = capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the count of an array. Extend with nil if needed. */
|
2017-11-28 23:27:55 +00:00
|
|
|
void dst_array_setcount(DstArray *array, int32_t count) {
|
2017-11-29 20:17:56 +00:00
|
|
|
if (count < 0)
|
|
|
|
return;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (count > array->count) {
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t i;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_array_ensure(array, count + 1);
|
2018-01-05 21:17:55 +00:00
|
|
|
for (i = array->count; i < count; i++) {
|
|
|
|
array->data[i] = dst_wrap_nil();
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-11-29 20:17:56 +00:00
|
|
|
array->count = count;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a value to the top of the array */
|
2018-01-06 16:09:15 +00:00
|
|
|
void dst_array_push(DstArray *array, Dst x) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t newcount = array->count + 1;
|
2017-11-06 03:05:47 +00:00
|
|
|
if (newcount >= array->capacity) {
|
|
|
|
dst_array_ensure(array, newcount * 2);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
array->data[array->count] = x;
|
|
|
|
array->count = newcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop a value from the top of the array */
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst dst_array_pop(DstArray *array) {
|
2017-11-01 21:53:43 +00:00
|
|
|
if (array->count) {
|
|
|
|
return array->data[--array->count];
|
|
|
|
} else {
|
|
|
|
return dst_wrap_nil();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look at the last value in the array */
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst dst_array_peek(DstArray *array) {
|
2017-11-01 21:53:43 +00:00
|
|
|
if (array->count) {
|
|
|
|
return array->data[array->count - 1];
|
|
|
|
} else {
|
|
|
|
return dst_wrap_nil();
|
|
|
|
}
|
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
|
|
|
|
/* C Functions */
|
|
|
|
|
|
|
|
static int cfun_pop(DstArgs args) {
|
|
|
|
if (args.n != 1 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
|
|
|
return dst_return(args, dst_array_pop(dst_unwrap_array(args.v[0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_peek(DstArgs args) {
|
|
|
|
if (args.n != 1 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
|
|
|
return dst_return(args, dst_array_peek(dst_unwrap_array(args.v[0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_push(DstArgs args) {
|
|
|
|
DstArray *array;
|
|
|
|
int32_t newcount;
|
|
|
|
if (args.n < 1 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
|
|
|
array = dst_unwrap_array(args.v[0]);
|
|
|
|
newcount = array->count - 1 + args.n;
|
|
|
|
dst_array_ensure(array, newcount);
|
|
|
|
if (args.n > 1) memcpy(array->data + array->count, args.v + 1, (args.n - 1) * sizeof(Dst));
|
|
|
|
array->count = newcount;
|
|
|
|
return dst_return(args, args.v[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_setcount(DstArgs args) {
|
|
|
|
int32_t newcount;
|
|
|
|
if (args.n != 2 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
|
|
|
if (!dst_checktype(args.v[1], DST_INTEGER)) return dst_throw(args, "expected positive integer");
|
|
|
|
newcount = dst_unwrap_integer(args.v[1]);
|
|
|
|
if (newcount < 0) return dst_throw(args, "expected positive integer");
|
|
|
|
dst_array_setcount(dst_unwrap_array(args.v[0]), newcount);
|
|
|
|
return dst_return(args, args.v[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_ensure(DstArgs args) {
|
|
|
|
int32_t newcount;
|
|
|
|
if (args.n != 2 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
|
|
|
if (!dst_checktype(args.v[1], DST_INTEGER)) return dst_throw(args, "expected positive integer");
|
|
|
|
newcount = dst_unwrap_integer(args.v[1]);
|
|
|
|
if (newcount < 0) return dst_throw(args, "expected positive integer");
|
|
|
|
dst_array_ensure(dst_unwrap_array(args.v[0]), newcount);
|
|
|
|
return dst_return(args, args.v[0]);
|
|
|
|
}
|
|
|
|
|
2018-01-27 20:15:09 +00:00
|
|
|
static int cfun_slice(DstArgs args) {
|
|
|
|
const Dst *vals;
|
|
|
|
int32_t len;
|
|
|
|
DstArray *ret;
|
|
|
|
int32_t start, end;
|
|
|
|
if (args.n < 1 || !dst_seq_view(args.v[0], &vals, &len)) return dst_throw(args, "expected array/tuple");
|
|
|
|
/* Get start */
|
|
|
|
if (args.n < 2) {
|
|
|
|
start = 0;
|
|
|
|
} else if (dst_checktype(args.v[1], DST_INTEGER)) {
|
|
|
|
start = dst_unwrap_integer(args.v[1]);
|
|
|
|
} else {
|
|
|
|
return dst_throw(args, "expected integer");
|
|
|
|
}
|
|
|
|
/* Get end */
|
|
|
|
if (args.n < 3) {
|
|
|
|
end = -1;
|
|
|
|
} else if (dst_checktype(args.v[2], DST_INTEGER)) {
|
|
|
|
end = dst_unwrap_integer(args.v[2]);
|
|
|
|
} else {
|
|
|
|
return dst_throw(args, "expected integer");
|
|
|
|
}
|
|
|
|
if (start < 0) start = len + start;
|
|
|
|
if (end < 0) end = len + end + 1;
|
|
|
|
if (end >= start) {
|
|
|
|
int32_t i, j;
|
|
|
|
ret = dst_array(end - start);
|
|
|
|
for (j = 0, i = start; i < end; j++, i++) {
|
|
|
|
ret->data[j] = vals[i];
|
|
|
|
}
|
|
|
|
ret->count = j;
|
|
|
|
} else {
|
|
|
|
ret = dst_array(0);
|
|
|
|
}
|
|
|
|
return dst_return(args, dst_wrap_array(ret));
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Load the array module */
|
|
|
|
int dst_lib_array(DstArgs args) {
|
|
|
|
DstTable *env = dst_env_arg(args);
|
|
|
|
dst_env_def(env, "array-pop", dst_wrap_cfunction(cfun_pop));
|
|
|
|
dst_env_def(env, "array-peek", dst_wrap_cfunction(cfun_peek));
|
|
|
|
dst_env_def(env, "array-push", dst_wrap_cfunction(cfun_push));
|
|
|
|
dst_env_def(env, "array-setcount", dst_wrap_cfunction(cfun_setcount));
|
|
|
|
dst_env_def(env, "array-ensure", dst_wrap_cfunction(cfun_ensure));
|
2018-01-27 20:15:09 +00:00
|
|
|
dst_env_def(env, "array-slice", dst_wrap_cfunction(cfun_slice));
|
2018-01-18 22:25:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|