2017-12-21 04:03:34 +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-30 21:46:59 +00:00
|
|
|
#include <dst/dststl.h>
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_push(DstArgs args) {
|
2018-01-16 01:14:21 +00:00
|
|
|
if (args.n != 2) return dst_throw(args, "expected 2 arguments");
|
|
|
|
if (!dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
2018-01-14 17:10:45 +00:00
|
|
|
dst_array_push(dst_unwrap_array(args.v[0]), args.v[1]);
|
2018-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, args.v[0]);
|
2018-01-14 15:23:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_exit(DstArgs args) {
|
2018-01-12 21:25:24 +00:00
|
|
|
int32_t exitcode = 0;
|
2018-01-14 17:10:45 +00:00
|
|
|
if (args.n > 0) {
|
|
|
|
exitcode = dst_hash(args.v[0]);
|
2018-01-12 21:25:24 +00:00
|
|
|
}
|
|
|
|
exit(exitcode);
|
2018-01-13 19:08:42 +00:00
|
|
|
return 0;
|
2018-01-12 21:25:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_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);
|
|
|
|
return 0;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_describe(DstArgs args) {
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t i;
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; ++i) {
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t j, len;
|
2018-01-14 17:10:45 +00:00
|
|
|
const uint8_t *vstr = dst_description(args.v[i]);
|
2018-01-04 02:36:10 +00:00
|
|
|
len = dst_string_length(vstr);
|
|
|
|
for (j = 0; j < len; ++j) {
|
|
|
|
putc(vstr[j], stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc('\n', stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_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-16 01:14:21 +00:00
|
|
|
int dst_stl_buffer_to_string(DstArgs args) {
|
|
|
|
DstBuffer *b;
|
|
|
|
if (args.n != 1) return dst_throw(args, "expected 1 argument");
|
|
|
|
if (!dst_checktype(args.v[0], DST_BUFFER)) return dst_throw(args, "expected buffer");
|
|
|
|
b = dst_unwrap_buffer(args.v[0]);
|
|
|
|
return dst_return(args, dst_wrap_string(dst_string(b->data, b->count)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int dst_cfun_tuple(DstArgs args) {
|
|
|
|
return dst_return(args, dst_wrap_tuple(dst_tuple_n(args.v, args.n)));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
int dst_cfun_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-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, dst_wrap_array(array));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
int dst_cfun_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-01-16 01:14:21 +00:00
|
|
|
if (args.n & 1) return 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-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, dst_wrap_table(table));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
int dst_cfun_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-01-16 01:14:21 +00:00
|
|
|
if (args.n & 1) return 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-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, dst_wrap_struct(dst_struct_end(st)));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_fiber(DstArgs args) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstFiber *fiber;
|
2018-01-16 01:14:21 +00:00
|
|
|
if (args.n < 1) return dst_throw(args, "expected at least one argument");
|
|
|
|
if (!dst_checktype(args.v[0], DST_FUNCTION)) return dst_throw(args, "expected a function");
|
2018-01-05 21:17:55 +00:00
|
|
|
fiber = dst_fiber(64);
|
2018-01-14 17:10:45 +00:00
|
|
|
dst_fiber_funcframe(fiber, dst_unwrap_function(args.v[0]));
|
2018-01-05 21:17:55 +00:00
|
|
|
fiber->parent = dst_vm_fiber;
|
2018-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, dst_wrap_fiber(fiber));
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_buffer(DstArgs args) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstBuffer *buffer = dst_buffer(10);
|
|
|
|
int32_t i;
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; ++i) {
|
|
|
|
const uint8_t *bytes = dst_to_string(args.v[i]);
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t len = dst_string_length(bytes);
|
|
|
|
dst_buffer_push_bytes(buffer, bytes, len);
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, dst_wrap_buffer(buffer));
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_gensym(DstArgs args) {
|
2018-01-16 01:14:21 +00:00
|
|
|
if (args.n > 1) return dst_throw(args, "expected one argument");
|
2018-01-14 17:10:45 +00:00
|
|
|
if (args.n == 0) {
|
2018-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, dst_wrap_symbol(dst_symbol_gen(NULL, 0)));
|
2018-01-05 21:17:55 +00:00
|
|
|
} else {
|
2018-01-14 17:10:45 +00:00
|
|
|
const uint8_t *s = dst_to_string(args.v[0]);
|
2018-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, dst_wrap_symbol(dst_symbol_gen(s, dst_string_length(s))));
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_length(DstArgs args) {
|
2018-01-16 01:14:21 +00:00
|
|
|
if (args.n != 1) return dst_throw(args, "expected at least 1 argument");
|
|
|
|
return dst_return(args, dst_wrap_integer(dst_length(args.v[0])));
|
2018-01-12 18:54:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_get(DstArgs args) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst ds;
|
2018-01-16 01:14:21 +00:00
|
|
|
if (args.n < 1) return dst_throw(args, "expected at least 1 argument");
|
2018-01-14 17:10:45 +00:00
|
|
|
ds = args.v[0];
|
|
|
|
for (i = 1; i < args.n; i++) {
|
|
|
|
ds = dst_get(ds, args.v[i]);
|
2017-12-30 21:46:59 +00:00
|
|
|
if (dst_checktype(ds, DST_NIL))
|
2017-12-21 04:03:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, ds);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_status(DstArgs args) {
|
2018-01-12 21:25:24 +00:00
|
|
|
const char *status;
|
2018-01-16 01:14:21 +00:00
|
|
|
if (args.n != 1) return dst_throw(args, "expected 1 argument");
|
|
|
|
if (!dst_checktype(args.v[0], DST_FIBER)) return dst_throw(args, "expected fiber");
|
2018-01-14 17:10:45 +00:00
|
|
|
switch(dst_unwrap_fiber(args.v[0])->status) {
|
2018-01-12 21:25:24 +00:00
|
|
|
case DST_FIBER_PENDING:
|
|
|
|
status = "pending";
|
|
|
|
break;
|
|
|
|
case DST_FIBER_NEW:
|
|
|
|
status = "new";
|
|
|
|
break;
|
|
|
|
case DST_FIBER_ALIVE:
|
|
|
|
status = "alive";
|
|
|
|
break;
|
|
|
|
case DST_FIBER_DEAD:
|
|
|
|
status = "dead";
|
|
|
|
break;
|
|
|
|
case DST_FIBER_ERROR:
|
|
|
|
status = "error";
|
|
|
|
break;
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
return dst_return(args, dst_cstringv(status));
|
2018-01-12 21:25:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_stl_put(DstArgs args) {
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst ds, key, value;
|
2018-01-14 17:10:45 +00:00
|
|
|
DstArgs subargs = args;
|
2018-01-16 01:14:21 +00:00
|
|
|
if (args.n < 3) return dst_throw(args, "expected at least 3 arguments");
|
2018-01-14 17:10:45 +00:00
|
|
|
subargs.n -= 2;
|
2018-01-16 01:14:21 +00:00
|
|
|
if (dst_stl_get(subargs)) return 1;
|
2018-01-14 17:10:45 +00:00
|
|
|
ds = *args.ret;
|
|
|
|
key = args.v[args.n - 2];
|
|
|
|
value = args.v[args.n - 1];
|
2017-12-30 21:46:59 +00:00
|
|
|
dst_put(ds, key, value);
|
|
|
|
return 0;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
int dst_stl_gccollect(DstArgs args) {
|
|
|
|
(void) args;
|
|
|
|
dst_collect();
|
2017-12-30 21:46:59 +00:00
|
|
|
return 0;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
int dst_stl_type(DstArgs args) {
|
|
|
|
if (args.n != 1) return dst_throw(args, "expected 1 argument");
|
|
|
|
if (dst_checktype(args.v[0], DST_ABSTRACT)) {
|
|
|
|
return dst_return(args, dst_cstringv(dst_abstract_type(dst_unwrap_abstract(args.v[0]))->name));
|
|
|
|
} else {
|
|
|
|
return dst_return(args, dst_cstringv(dst_type_names[dst_type(args.v[0])]));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
Dst dst_stl_env() {
|
|
|
|
Dst ret;
|
|
|
|
DstArgs args;
|
|
|
|
DstTable *module = dst_table(0);
|
|
|
|
ret = dst_wrap_table(module);
|
|
|
|
args.n = 1;
|
|
|
|
args.v = &ret;
|
|
|
|
args.ret = &ret;
|
2018-01-04 02:36:10 +00:00
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
dst_module_def(module, "native", dst_wrap_cfunction(dst_load_native));
|
|
|
|
dst_module_def(module, "push", dst_wrap_cfunction(dst_stl_push));
|
|
|
|
dst_module_def(module, "print", dst_wrap_cfunction(dst_stl_print));
|
|
|
|
dst_module_def(module, "describe", dst_wrap_cfunction(dst_stl_describe));
|
|
|
|
dst_module_def(module, "string", dst_wrap_cfunction(dst_stl_string));
|
|
|
|
dst_module_def(module, "buffer-to-string", dst_wrap_cfunction(dst_stl_buffer_to_string));
|
|
|
|
dst_module_def(module, "table", dst_wrap_cfunction(dst_cfun_table));
|
|
|
|
dst_module_def(module, "array", dst_wrap_cfunction(dst_cfun_array));
|
|
|
|
dst_module_def(module, "tuple", dst_wrap_cfunction(dst_cfun_tuple));
|
|
|
|
dst_module_def(module, "struct", dst_wrap_cfunction(dst_cfun_struct));
|
|
|
|
dst_module_def(module, "fiber", dst_wrap_cfunction(dst_stl_fiber));
|
|
|
|
dst_module_def(module, "status", dst_wrap_cfunction(dst_stl_status));
|
|
|
|
dst_module_def(module, "buffer", dst_wrap_cfunction(dst_stl_buffer));
|
|
|
|
dst_module_def(module, "gensym", dst_wrap_cfunction(dst_stl_gensym));
|
|
|
|
dst_module_def(module, "get", dst_wrap_cfunction(dst_stl_get));
|
|
|
|
dst_module_def(module, "put", dst_wrap_cfunction(dst_stl_put));
|
|
|
|
dst_module_def(module, "length", dst_wrap_cfunction(dst_stl_length));
|
|
|
|
dst_module_def(module, "gccollect", dst_wrap_cfunction(dst_stl_gccollect));
|
|
|
|
dst_module_def(module, "type", dst_wrap_cfunction(dst_stl_type));
|
2018-01-16 04:31:39 +00:00
|
|
|
dst_module_def(module, "exit", dst_wrap_cfunction(dst_stl_exit));
|
|
|
|
|
|
|
|
dst_module_def(module, "parse", dst_wrap_cfunction(dst_parse_cfun));
|
|
|
|
dst_module_def(module, "compile", dst_wrap_cfunction(dst_compile_cfun));
|
|
|
|
dst_module_def(module, "asm", dst_wrap_cfunction(dst_asm_cfun));
|
|
|
|
dst_module_def(module, "disasm", dst_wrap_cfunction(dst_disasm_cfun));
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
/* Allow references to the environment */
|
|
|
|
dst_module_def(module, "_env", ret);
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
/*Load auxiliary modules */
|
|
|
|
dst_io_init(args);
|
|
|
|
dst_math_init(args);
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
return ret;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|