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-11-06 03:05:47 +00:00
|
|
|
#include <dst/dst.h>
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* Define a number of functions that can be used internally on ANY DstValue.
|
|
|
|
*/
|
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Boolean truth definition */
|
2017-11-01 21:53:43 +00:00
|
|
|
int dst_truthy(DstValue v) {
|
|
|
|
return v.type != DST_NIL && !(v.type == DST_BOOLEAN && !v.as.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-11-01 21:53:43 +00:00
|
|
|
int dst_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-11-01 21:53:43 +00:00
|
|
|
result = (x.as.boolean == y.as.boolean);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_REAL:
|
2017-11-01 21:53:43 +00:00
|
|
|
result = (x.as.real == y.as.real);
|
2017-04-24 20:02:54 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_INTEGER:
|
2017-11-01 21:53:43 +00:00
|
|
|
result = (x.as.integer == y.as.integer);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-11-27 19:03:34 +00:00
|
|
|
case DST_STRING:
|
|
|
|
result = dst_string_equal(x.as.string, y.as.string);
|
|
|
|
break;
|
|
|
|
case DST_STRUCT:
|
|
|
|
result = dst_struct_equal(x.as.st, y.as.st);
|
|
|
|
break;
|
2017-04-15 20:05:59 +00:00
|
|
|
default:
|
|
|
|
/* compare pointers */
|
2017-11-01 21:53:43 +00:00
|
|
|
result = (x.as.pointer == y.as.pointer);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Computes a hash value for a function */
|
2017-11-01 21:53:43 +00:00
|
|
|
uint32_t dst_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-11-01 21:53:43 +00:00
|
|
|
hash = x.as.boolean;
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRING:
|
|
|
|
case DST_SYMBOL:
|
2017-11-01 21:53:43 +00:00
|
|
|
hash = dst_string_hash(x.as.string);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TUPLE:
|
2017-11-27 19:03:34 +00:00
|
|
|
if (0 == dst_tuple_hash(x.as.tuple))
|
|
|
|
hash = dst_tuple_hash(x.as.tuple) =
|
|
|
|
dst_array_calchash(x.as.tuple, dst_tuple_length(x.as.tuple));
|
|
|
|
else
|
|
|
|
hash = dst_tuple_hash(x.as.tuple);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRUCT:
|
2017-11-27 19:03:34 +00:00
|
|
|
if (0 == dst_struct_hash(x.as.st))
|
|
|
|
hash = dst_struct_hash(x.as.st) =
|
|
|
|
dst_array_calchash(x.as.st, dst_struct_capacity(x.as.st));
|
|
|
|
else
|
|
|
|
hash = dst_struct_hash(x.as.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 */
|
2017-11-06 03:05:47 +00:00
|
|
|
uint64_t i = x.as.integer;
|
2017-11-27 19:03:34 +00:00
|
|
|
hash = (uint32_t)(i >> 32) ^ (uint32_t)(i & 0xFFFFFFFF);
|
2017-07-09 17:09:20 +00:00
|
|
|
} else {
|
|
|
|
/* Assuming 4 byte pointer (or smaller) */
|
2017-11-27 19:03:34 +00:00
|
|
|
hash = (uint32_t) (x.as.pointer - NULL);
|
2017-07-09 17:09:20 +00:00
|
|
|
}
|
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-11-01 21:53:43 +00:00
|
|
|
int dst_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-11-01 21:53:43 +00:00
|
|
|
if (x.as.boolean == y.as.boolean) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-11-01 21:53:43 +00:00
|
|
|
return x.as.boolean ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_REAL:
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Check for nans to ensure total order */
|
|
|
|
if (x.as.real != x.as.real)
|
|
|
|
return y.as.real != y.as.real
|
|
|
|
? 0
|
|
|
|
: -1;
|
|
|
|
if (y.as.real != y.as.real)
|
|
|
|
return 1;
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
if (x.as.real == y.as.real) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-11-01 21:53:43 +00:00
|
|
|
return x.as.real > y.as.real ? 1 : -1;
|
2017-04-24 20:02:54 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_INTEGER:
|
2017-11-01 21:53:43 +00:00
|
|
|
if (x.as.integer == y.as.integer) {
|
2017-04-24 20:02:54 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-11-01 21:53:43 +00:00
|
|
|
return x.as.integer > y.as.integer ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRING:
|
2017-11-01 21:53:43 +00:00
|
|
|
return dst_string_compare(x.as.string, y.as.string);
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_TUPLE:
|
2017-11-27 19:03:34 +00:00
|
|
|
return dst_tuple_compare(x.as.tuple, y.as.tuple);
|
|
|
|
case DST_STRUCT:
|
|
|
|
return dst_struct_compare(x.as.st, y.as.st);
|
2017-02-19 16:19:39 +00:00
|
|
|
default:
|
2017-11-01 21:53:43 +00:00
|
|
|
if (x.as.string == y.as.string) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-11-01 21:53:43 +00:00
|
|
|
return x.as.string > y.as.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-11-25 04:17:04 +00:00
|
|
|
|
2017-11-27 00:31:40 +00:00
|
|
|
/* Get a value out af an associated data structure. For invalid
|
|
|
|
* data structure or invalid key, returns nil. */
|
|
|
|
DstValue dst_get(DstValue ds, DstValue key) {
|
2017-11-25 04:17:04 +00:00
|
|
|
switch (ds.type) {
|
|
|
|
case DST_ARRAY:
|
2017-11-27 00:31:40 +00:00
|
|
|
if (key.type == DST_INTEGER &&
|
|
|
|
key.as.integer >= 0 &&
|
|
|
|
key.as.integer < ds.as.array->count)
|
|
|
|
return ds.as.array->data[key.as.integer];
|
2017-11-25 04:17:04 +00:00
|
|
|
break;
|
|
|
|
case DST_TUPLE:
|
2017-11-27 00:31:40 +00:00
|
|
|
if (key.type == DST_INTEGER &&
|
|
|
|
key.as.integer >= 0 &&
|
|
|
|
key.as.integer < dst_tuple_length(ds.as.tuple))
|
|
|
|
return ds.as.tuple[key.as.integer];
|
2017-11-25 04:17:04 +00:00
|
|
|
break;
|
|
|
|
case DST_BUFFER:
|
2017-11-27 00:31:40 +00:00
|
|
|
if (key.type == DST_INTEGER &&
|
|
|
|
key.as.integer >= 0 &&
|
|
|
|
key.as.integer < ds.as.buffer->count)
|
|
|
|
return dst_wrap_integer(ds.as.buffer->data[key.as.integer]);
|
2017-11-25 04:17:04 +00:00
|
|
|
break;
|
|
|
|
case DST_STRING:
|
|
|
|
case DST_SYMBOL:
|
2017-11-27 00:31:40 +00:00
|
|
|
if (key.type == DST_INTEGER &&
|
|
|
|
key.as.integer >= 0 &&
|
|
|
|
key.as.integer < dst_string_length(ds.as.string))
|
|
|
|
return dst_wrap_integer(ds.as.string[key.as.integer]);
|
2017-11-25 04:17:04 +00:00
|
|
|
break;
|
|
|
|
case DST_STRUCT:
|
2017-11-27 00:31:40 +00:00
|
|
|
return dst_struct_get(ds.as.st, key);
|
2017-11-25 04:17:04 +00:00
|
|
|
case DST_TABLE:
|
2017-11-27 00:31:40 +00:00
|
|
|
return dst_table_get(ds.as.table, key);
|
2017-11-25 04:17:04 +00:00
|
|
|
default:
|
2017-11-27 00:31:40 +00:00
|
|
|
break;
|
2017-11-25 04:17:04 +00:00
|
|
|
}
|
2017-11-27 00:31:40 +00:00
|
|
|
return dst_wrap_nil();
|
2017-11-25 04:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set a value in an associative data structure. Returns possible
|
|
|
|
* error message, and NULL if no error. */
|
2017-11-27 00:31:40 +00:00
|
|
|
void dst_put(DstValue ds, DstValue key, DstValue value) {
|
2017-11-25 04:17:04 +00:00
|
|
|
switch (ds.type) {
|
|
|
|
case DST_ARRAY:
|
2017-11-27 00:31:40 +00:00
|
|
|
if (key.type == DST_INTEGER &&
|
|
|
|
key.as.integer >= 0 &&
|
|
|
|
key.as.integer < ds.as.array->count)
|
|
|
|
ds.as.array->data[key.as.integer] = value;
|
|
|
|
return;
|
2017-11-25 04:17:04 +00:00
|
|
|
case DST_BUFFER:
|
2017-11-27 00:31:40 +00:00
|
|
|
if (key.type == DST_INTEGER &&
|
|
|
|
value.type == DST_INTEGER &&
|
|
|
|
key.as.integer >= 0 &&
|
|
|
|
key.as.integer < ds.as.buffer->count)
|
|
|
|
ds.as.buffer->data[key.as.integer] = value.as.integer;
|
|
|
|
return;
|
2017-11-25 04:17:04 +00:00
|
|
|
case DST_TABLE:
|
|
|
|
dst_table_put(ds.as.table, key, value);
|
2017-11-27 00:31:40 +00:00
|
|
|
return;
|
2017-11-25 04:17:04 +00:00
|
|
|
default:
|
2017-11-27 00:31:40 +00:00
|
|
|
return;
|
2017-11-25 04:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next key in an associative data structure. Used for iterating through an
|
|
|
|
* associative data structure. */
|
2017-11-27 00:31:40 +00:00
|
|
|
DstValue dst_next(DstValue ds, DstValue key) {
|
2017-11-25 04:17:04 +00:00
|
|
|
switch(ds.type) {
|
|
|
|
default:
|
2017-11-27 00:31:40 +00:00
|
|
|
return dst_wrap_nil();
|
2017-11-25 04:17:04 +00:00
|
|
|
case DST_TABLE:
|
2017-11-27 00:31:40 +00:00
|
|
|
return dst_table_next(ds.as.table, key);
|
2017-11-25 04:17:04 +00:00
|
|
|
case DST_STRUCT:
|
2017-11-27 00:31:40 +00:00
|
|
|
return dst_struct_next(ds.as.st, key);
|
2017-11-25 04:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the length of an object. Returns errors for invalid types */
|
|
|
|
uint32_t dst_length(DstValue x) {
|
|
|
|
switch (x.type) {
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
case DST_STRING:
|
|
|
|
return dst_string_length(x.as.string);
|
|
|
|
case DST_ARRAY:
|
|
|
|
return x.as.array->count;
|
|
|
|
case DST_BUFFER:
|
|
|
|
return x.as.buffer->count;
|
|
|
|
case DST_TUPLE:
|
|
|
|
return dst_tuple_length(x.as.tuple);
|
|
|
|
case DST_STRUCT:
|
|
|
|
return dst_struct_length(x.as.st);
|
|
|
|
case DST_TABLE:
|
|
|
|
return x.as.table->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the capacity of an object. Returns 0 for invalid types */
|
|
|
|
uint32_t dst_capacity(DstValue x) {
|
|
|
|
switch (x.type) {
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
case DST_STRING:
|
|
|
|
return dst_string_length(x.as.string);
|
|
|
|
case DST_ARRAY:
|
|
|
|
return x.as.array->capacity;
|
|
|
|
case DST_BUFFER:
|
|
|
|
return x.as.buffer->capacity;
|
|
|
|
case DST_TUPLE:
|
|
|
|
return dst_tuple_length(x.as.tuple);
|
|
|
|
case DST_STRUCT:
|
|
|
|
return dst_struct_length(x.as.st);
|
|
|
|
case DST_TABLE:
|
|
|
|
return x.as.table->capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Index into a data structure. Returns nil for out of bounds or invliad data structure */
|
|
|
|
DstValue dst_getindex(DstValue ds, uint32_t index) {
|
|
|
|
switch (ds.type) {
|
|
|
|
default:
|
|
|
|
return dst_wrap_nil();
|
|
|
|
case DST_STRING:
|
|
|
|
if (index >= dst_string_length(ds.as.string)) return dst_wrap_nil();
|
|
|
|
return dst_wrap_integer(ds.as.string[index]);
|
|
|
|
case DST_ARRAY:
|
|
|
|
if (index >= ds.as.array->count) return dst_wrap_nil();
|
|
|
|
return ds.as.array->data[index];
|
|
|
|
case DST_BUFFER:
|
|
|
|
if (index >= ds.as.buffer->count) return dst_wrap_nil();
|
|
|
|
return dst_wrap_integer(ds.as.buffer->data[index]);
|
|
|
|
case DST_TUPLE:
|
|
|
|
if (index >= dst_tuple_length(ds.as.tuple)) return dst_wrap_nil();
|
|
|
|
return ds.as.tuple[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set an index in a linear data structure. Does nothing if data structure
|
|
|
|
* is invalid */
|
|
|
|
void dst_setindex(DstValue ds, DstValue value, uint32_t index) {
|
|
|
|
switch (ds.type) {
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
case DST_ARRAY:
|
|
|
|
if (index >= ds.as.array->count) {
|
|
|
|
dst_array_ensure(ds.as.array, 2 * index);
|
|
|
|
ds.as.array->count = index + 1;
|
|
|
|
}
|
|
|
|
ds.as.array->data[index] = value;
|
|
|
|
return;
|
|
|
|
case DST_BUFFER:
|
|
|
|
if (value.type != DST_INTEGER) return;
|
|
|
|
if (index >= ds.as.buffer->count) {
|
|
|
|
dst_buffer_ensure(ds.as.buffer, 2 * index);
|
|
|
|
ds.as.buffer->count = index + 1;
|
|
|
|
}
|
|
|
|
ds.as.buffer->data[index] = value.as.integer;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|