2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +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:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
#include "internal.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Boolean truth definition */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_value_truthy(DstValue v) {
|
|
|
|
return v.type != DST_NIL && !(v.type == DST_BOOLEAN && !v.data.boolean);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Check if two values are equal. This is strict equality with no conversion. */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_value_equals(DstValue x, DstValue y) {
|
2017-02-09 23:50:47 +00:00
|
|
|
int result = 0;
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.type != y.type) {
|
2017-02-09 20:02:59 +00:00
|
|
|
result = 0;
|
|
|
|
} else {
|
2017-02-10 04:28:11 +00:00
|
|
|
switch (x.type) {
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_NIL:
|
2017-04-15 20:05:59 +00:00
|
|
|
result = 1;
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_BOOLEAN:
|
2017-04-15 20:05:59 +00:00
|
|
|
result = (x.data.boolean == y.data.boolean);
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_REAL:
|
2017-04-24 20:02:54 +00:00
|
|
|
result = (x.data.real == y.data.real);
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_INTEGER:
|
2017-04-24 20:02:54 +00:00
|
|
|
result = (x.data.integer == y.data.integer);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* compare pointers */
|
|
|
|
result = (x.data.pointer == y.data.pointer);
|
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Computes a hash value for a function */
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t dst_value_hash(DstValue x) {
|
2017-02-09 23:50:47 +00:00
|
|
|
uint32_t hash = 0;
|
2017-02-10 04:28:11 +00:00
|
|
|
switch (x.type) {
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_NIL:
|
2017-04-15 20:05:59 +00:00
|
|
|
hash = 0;
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_BOOLEAN:
|
2017-04-15 20:05:59 +00:00
|
|
|
hash = x.data.boolean;
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRING:
|
|
|
|
case DST_SYMBOL:
|
|
|
|
hash = dst_string_hash(x.data.string);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TUPLE:
|
|
|
|
hash = dst_tuple_hash(x.data.tuple);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRUCT:
|
|
|
|
hash = dst_struct_hash(x.data.st);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-07-09 17:09:20 +00:00
|
|
|
if (sizeof(double) == sizeof(void *)) {
|
|
|
|
/* Assuming 8 byte pointer */
|
|
|
|
hash = x.data.dwords[0] ^ x.data.dwords[1];
|
|
|
|
} else {
|
|
|
|
/* Assuming 4 byte pointer (or smaller) */
|
|
|
|
hash = (uint32_t) x.data.pointer;
|
|
|
|
}
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compares x to y. If they are equal retuns 0. If x is less, returns -1.
|
|
|
|
* If y is less, returns 1. All types are comparable
|
|
|
|
* and should have strict ordering. */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_value_compare(DstValue x, DstValue y) {
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.type == y.type) {
|
|
|
|
switch (x.type) {
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_NIL:
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_BOOLEAN:
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.boolean == y.data.boolean) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-02-10 04:28:11 +00:00
|
|
|
return x.data.boolean ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_REAL:
|
2017-04-24 20:02:54 +00:00
|
|
|
if (x.data.real == y.data.real) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-04-24 20:02:54 +00:00
|
|
|
return x.data.real > y.data.real ? 1 : -1;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_INTEGER:
|
2017-04-24 20:02:54 +00:00
|
|
|
if (x.data.integer == y.data.integer) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return x.data.integer > y.data.integer ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRING:
|
|
|
|
return dst_string_compare(x.data.string, y.data.string);
|
2017-03-07 20:29:40 +00:00
|
|
|
/* Lower indices are most significant */
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TUPLE:
|
2017-03-10 05:26:28 +00:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t xlen = dst_tuple_length(x.data.tuple);
|
|
|
|
uint32_t ylen = dst_tuple_length(y.data.tuple);
|
2017-03-10 05:26:28 +00:00
|
|
|
uint32_t count = xlen < ylen ? xlen : ylen;
|
|
|
|
for (i = 0; i < count; ++i) {
|
2017-09-09 18:39:51 +00:00
|
|
|
int comp = dst_value_compare(x.data.tuple[i], y.data.tuple[i]);
|
2017-03-10 05:26:28 +00:00
|
|
|
if (comp != 0) return comp;
|
|
|
|
}
|
|
|
|
if (xlen < ylen)
|
|
|
|
return -1;
|
|
|
|
else if (xlen > ylen)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-07 20:29:40 +00:00
|
|
|
break;
|
2017-02-19 16:19:39 +00:00
|
|
|
default:
|
2017-02-10 04:28:11 +00:00
|
|
|
if (x.data.string == y.data.string) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-02-10 04:28:11 +00:00
|
|
|
return x.data.string > y.data.string ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 04:28:11 +00:00
|
|
|
} else if (x.type < y.type) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2017-02-12 20:53:52 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_truthy(Dst *vm, uint32_t x) {
|
|
|
|
return dst_value_truthy(dst_arg(vm, x));
|
2017-02-12 20:53:52 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t dst_hash(Dst *vm, uint32_t x) {
|
|
|
|
return dst_value_hash(dst_arg(vm, x));
|
|
|
|
}
|
|
|
|
int dst_compare(Dst *vm, uint32_t x, uint32_t y) {
|
|
|
|
return dst_value_compare(dst_arg(vm, x), dst_arg(vm, y));
|
|
|
|
}
|
|
|
|
int dst_equals(Dst *vm, uint32_t x, uint32_t y) {
|
|
|
|
return dst_value_equals(dst_arg(vm, x), dst_arg(vm, y));
|
2017-03-01 01:20:29 +00:00
|
|
|
}
|
2017-03-09 18:49:46 +00:00
|
|
|
|
2017-03-21 03:06:38 +00:00
|
|
|
/* Get the length of an object. Returns errors for invalid types */
|
2017-09-09 18:39:51 +00:00
|
|
|
uint32_t dst_length(Dst *vm, uint32_t n) {
|
|
|
|
DstValue x = dst_arg(vm, n);
|
|
|
|
uint32_t length;
|
2017-03-21 03:06:38 +00:00
|
|
|
switch (x.type) {
|
|
|
|
default:
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->ret = dst_string_cv(vm, "cannot get length");
|
|
|
|
vm->flags = 1;
|
|
|
|
return 0;
|
|
|
|
case DST_STRING:
|
|
|
|
length = dst_string_length(x.data.string);
|
2017-03-21 03:06:38 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_ARRAY:
|
2017-03-21 03:06:38 +00:00
|
|
|
length = x.data.array->count;
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_BYTEBUFFER:
|
2017-03-21 03:06:38 +00:00
|
|
|
length = x.data.buffer->count;
|
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TUPLE:
|
|
|
|
length = dst_tuple_length(x.data.tuple);
|
2017-03-21 03:06:38 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRUCT:
|
|
|
|
length = dst_struct_length(x.data.st);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TABLE:
|
2017-04-25 01:00:56 +00:00
|
|
|
length = x.data.table->count;
|
2017-03-21 03:06:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-04-24 20:02:54 +00:00
|
|
|
return length;
|
2017-03-21 03:06:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Get the capacity of an object. Returns errors for invalid types */
|
|
|
|
uint32_t dst_capacity(Dst *vm, uint32_t n) {
|
|
|
|
DstValue x = dst_arg(vm, n);
|
|
|
|
uint32_t cap;
|
|
|
|
switch (x.type) {
|
|
|
|
default:
|
|
|
|
vm->ret = dst_string_cv(vm, "cannot get capacity");
|
|
|
|
vm->flags = 1;
|
|
|
|
return 0;
|
|
|
|
case DST_STRING:
|
|
|
|
cap = dst_string_length(x.data.string);
|
|
|
|
break;
|
|
|
|
case DST_ARRAY:
|
|
|
|
cap = x.data.array->capacity;
|
|
|
|
break;
|
|
|
|
case DST_BYTEBUFFER:
|
|
|
|
cap = x.data.buffer->capacity;
|
|
|
|
break;
|
|
|
|
case DST_TUPLE:
|
|
|
|
cap = dst_tuple_length(x.data.tuple);
|
|
|
|
break;
|
|
|
|
case DST_STRUCT:
|
|
|
|
cap = dst_struct_length(x.data.st);
|
|
|
|
break;
|
|
|
|
case DST_TABLE:
|
|
|
|
cap = x.data.table->capacity;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cap;
|
|
|
|
}
|