2017-12-21 04:03:34 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2017-12-21 04:03:34 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2018-01-30 04:38:49 +00:00
|
|
|
#include <dst/dstcorelib.h>
|
2018-03-31 20:40:36 +00:00
|
|
|
#include "state.h"
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_print(DstArgs args) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; ++i) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t j, len;
|
2018-01-14 17:10:45 +00:00
|
|
|
const uint8_t *vstr = dst_to_string(args.v[i]);
|
2017-12-30 21:46:59 +00:00
|
|
|
len = dst_string_length(vstr);
|
|
|
|
for (j = 0; j < len; ++j) {
|
|
|
|
putc(vstr[j], stdout);
|
|
|
|
}
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
putc('\n', stdout);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_NIL();
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_describe(DstArgs args) {
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t i;
|
2018-03-14 22:57:26 +00:00
|
|
|
DstBuffer b;
|
|
|
|
dst_buffer_init(&b, 0);
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; ++i) {
|
2018-03-14 22:57:26 +00:00
|
|
|
int32_t len;
|
|
|
|
const uint8_t *str = dst_description(args.v[i]);
|
|
|
|
len = dst_string_length(str);
|
|
|
|
dst_buffer_push_bytes(&b, str, len);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
2018-03-14 22:57:26 +00:00
|
|
|
*args.ret = dst_stringv(b.data, b.count);
|
|
|
|
dst_buffer_deinit(&b);
|
2018-01-04 02:36:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_string(DstArgs args) {
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t i;
|
|
|
|
DstBuffer b;
|
|
|
|
dst_buffer_init(&b, 0);
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; ++i) {
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t len;
|
2018-01-14 17:10:45 +00:00
|
|
|
const uint8_t *str = dst_to_string(args.v[i]);
|
2018-01-12 15:41:27 +00:00
|
|
|
len = dst_string_length(str);
|
|
|
|
dst_buffer_push_bytes(&b, str, len);
|
|
|
|
}
|
2018-01-14 17:10:45 +00:00
|
|
|
*args.ret = dst_stringv(b.data, b.count);
|
2018-01-12 15:41:27 +00:00
|
|
|
dst_buffer_deinit(&b);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_symbol(DstArgs args) {
|
2018-01-21 22:08:11 +00:00
|
|
|
int32_t i;
|
|
|
|
DstBuffer b;
|
|
|
|
dst_buffer_init(&b, 0);
|
|
|
|
for (i = 0; i < args.n; ++i) {
|
|
|
|
int32_t len;
|
|
|
|
const uint8_t *str = dst_to_string(args.v[i]);
|
|
|
|
len = dst_string_length(str);
|
|
|
|
dst_buffer_push_bytes(&b, str, len);
|
|
|
|
}
|
|
|
|
*args.ret = dst_symbolv(b.data, b.count);
|
|
|
|
dst_buffer_deinit(&b);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-31 22:39:18 +00:00
|
|
|
int dst_core_buffer(DstArgs args) {
|
|
|
|
int32_t i;
|
|
|
|
DstBuffer *b = dst_buffer(0);
|
|
|
|
for (i = 0; i < args.n; ++i) {
|
|
|
|
int32_t len;
|
|
|
|
const uint8_t *str = dst_to_string(args.v[i]);
|
|
|
|
len = dst_string_length(str);
|
|
|
|
dst_buffer_push_bytes(b, str, len);
|
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_BUFFER(args, b);
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 15:06:31 +00:00
|
|
|
int dst_core_scannumber(DstArgs args) {
|
|
|
|
const uint8_t *data;
|
|
|
|
Dst x;
|
|
|
|
int32_t len;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_BYTES(data, len, args, 0);
|
2018-05-01 15:06:31 +00:00
|
|
|
x = dst_scan_number(data, len);
|
2018-05-13 00:31:28 +00:00
|
|
|
if (dst_checktype(x, DST_NIL)) {
|
|
|
|
DST_THROW(args, "error parsing number");
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, x);
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dst_core_scaninteger(DstArgs args) {
|
|
|
|
const uint8_t *data;
|
|
|
|
int32_t len, ret;
|
|
|
|
int err = 0;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_BYTES(data, len, args, 0);
|
2018-05-01 15:06:31 +00:00
|
|
|
ret = dst_scan_integer(data, len, &err);
|
|
|
|
if (err) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "error parsing integer");
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_INTEGER(args, ret);
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dst_core_scanreal(DstArgs args) {
|
|
|
|
const uint8_t *data;
|
|
|
|
int32_t len;
|
|
|
|
double ret;
|
|
|
|
int err = 0;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_BYTES(data, len, args, 0);
|
2018-05-01 15:06:31 +00:00
|
|
|
ret = dst_scan_real(data, len, &err);
|
|
|
|
if (err) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "error parsing real");
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_REAL(args, ret);
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_tuple(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_TUPLE(args, dst_tuple_n(args.v, args.n));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_array(DstArgs args) {
|
2018-01-14 17:10:45 +00:00
|
|
|
DstArray *array = dst_array(args.n);
|
|
|
|
array->count = args.n;
|
|
|
|
memcpy(array->data, args.v, args.n * sizeof(Dst));
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_ARRAY(args, array);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_table(DstArgs args) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2018-01-14 17:10:45 +00:00
|
|
|
DstTable *table = dst_table(args.n >> 1);
|
2018-05-20 01:16:00 +00:00
|
|
|
if (args.n & 1)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected even number of arguments");
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; i += 2) {
|
|
|
|
dst_table_put(table, args.v[i], args.v[i + 1]);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_TABLE(args, table);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_struct(DstArgs args) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2018-01-14 17:10:45 +00:00
|
|
|
DstKV *st = dst_struct_begin(args.n >> 1);
|
2018-05-13 00:31:28 +00:00
|
|
|
if (args.n & 1)
|
|
|
|
DST_THROW(args, "expected even number of arguments");
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; i += 2) {
|
|
|
|
dst_struct_put(st, args.v[i], args.v[i + 1]);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_STRUCT(args, dst_struct_end(st));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_gensym(DstArgs args) {
|
2018-07-01 19:49:33 +00:00
|
|
|
DST_FIXARITY(args, 0);
|
|
|
|
DST_RETURN_SYMBOL(args, dst_symbol_gen());
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_gccollect(DstArgs args) {
|
2018-01-16 01:14:21 +00:00
|
|
|
(void) args;
|
|
|
|
dst_collect();
|
2017-12-30 21:46:59 +00:00
|
|
|
return 0;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 05:44:51 +00:00
|
|
|
int dst_core_gcsetinterval(DstArgs args) {
|
2018-04-28 22:10:57 +00:00
|
|
|
int32_t val;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_INTEGER(val, args, 0);
|
2018-04-28 22:10:57 +00:00
|
|
|
if (val < 0)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected non-negative integer");
|
2018-04-28 22:10:57 +00:00
|
|
|
dst_vm_gc_interval = val;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_NIL(args);
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dst_core_gcinterval(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 0);
|
|
|
|
DST_RETURN_INTEGER(args, dst_vm_gc_interval);
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_type(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
2018-01-16 01:14:21 +00:00
|
|
|
if (dst_checktype(args.v[0], DST_ABSTRACT)) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, dst_csymbolv(dst_abstract_type(dst_unwrap_abstract(args.v[0]))->name));
|
2018-01-16 01:14:21 +00:00
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, dst_csymbolv(dst_type_names[dst_type(args.v[0])]));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_next(DstArgs args) {
|
2018-01-17 14:58:32 +00:00
|
|
|
Dst ds;
|
|
|
|
const DstKV *kv;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 2);
|
|
|
|
DST_CHECKMANY(args, 0, DST_TFLAG_DICTIONARY);
|
2018-01-17 14:58:32 +00:00
|
|
|
ds = args.v[0];
|
|
|
|
if (dst_checktype(ds, DST_TABLE)) {
|
|
|
|
DstTable *t = dst_unwrap_table(ds);
|
|
|
|
kv = dst_checktype(args.v[1], DST_NIL)
|
2018-02-03 23:12:07 +00:00
|
|
|
? NULL
|
2018-01-17 14:58:32 +00:00
|
|
|
: dst_table_find(t, args.v[1]);
|
2018-06-17 17:55:02 +00:00
|
|
|
kv = dst_table_next(t, kv);
|
2018-04-28 22:10:57 +00:00
|
|
|
} else {
|
2018-01-17 14:58:32 +00:00
|
|
|
const DstKV *st = dst_unwrap_struct(ds);
|
|
|
|
kv = dst_checktype(args.v[1], DST_NIL)
|
2018-02-03 23:12:07 +00:00
|
|
|
? NULL
|
2018-01-17 14:58:32 +00:00
|
|
|
: dst_struct_find(st, args.v[1]);
|
2018-06-17 17:55:02 +00:00
|
|
|
kv = dst_struct_next(st, kv);
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
|
|
|
if (kv) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, kv->key);
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_NIL(args);
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 04:38:49 +00:00
|
|
|
int dst_core_hash(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_RETURN_INTEGER(args, dst_hash(args.v[0]));
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|