mirror of
https://github.com/janet-lang/janet
synced 2025-10-24 20:27:41 +00:00
WIP
This commit is contained in:
164
core/value.c
164
core/value.c
@@ -22,13 +22,17 @@
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* Define a number of functions that can be used internally on ANY DstValue.
|
||||
*/
|
||||
|
||||
/* Boolean truth definition */
|
||||
int dst_value_truthy(DstValue v) {
|
||||
return v.type != DST_NIL && !(v.type == DST_BOOLEAN && !v.data.boolean);
|
||||
int dst_truthy(DstValue v) {
|
||||
return v.type != DST_NIL && !(v.type == DST_BOOLEAN && !v.as.boolean);
|
||||
}
|
||||
|
||||
/* Check if two values are equal. This is strict equality with no conversion. */
|
||||
int dst_value_equals(DstValue x, DstValue y) {
|
||||
int dst_equals(DstValue x, DstValue y) {
|
||||
int result = 0;
|
||||
if (x.type != y.type) {
|
||||
result = 0;
|
||||
@@ -38,17 +42,17 @@ int dst_value_equals(DstValue x, DstValue y) {
|
||||
result = 1;
|
||||
break;
|
||||
case DST_BOOLEAN:
|
||||
result = (x.data.boolean == y.data.boolean);
|
||||
result = (x.as.boolean == y.as.boolean);
|
||||
break;
|
||||
case DST_REAL:
|
||||
result = (x.data.real == y.data.real);
|
||||
result = (x.as.real == y.as.real);
|
||||
break;
|
||||
case DST_INTEGER:
|
||||
result = (x.data.integer == y.data.integer);
|
||||
result = (x.as.integer == y.as.integer);
|
||||
break;
|
||||
default:
|
||||
/* compare pointers */
|
||||
result = (x.data.pointer == y.data.pointer);
|
||||
result = (x.as.pointer == y.as.pointer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -56,75 +60,106 @@ int dst_value_equals(DstValue x, DstValue y) {
|
||||
}
|
||||
|
||||
/* Computes a hash value for a function */
|
||||
uint32_t dst_value_hash(DstValue x) {
|
||||
uint32_t dst_hash(DstValue x) {
|
||||
uint32_t hash = 0;
|
||||
switch (x.type) {
|
||||
case DST_NIL:
|
||||
hash = 0;
|
||||
break;
|
||||
case DST_BOOLEAN:
|
||||
hash = x.data.boolean;
|
||||
hash = x.as.boolean;
|
||||
break;
|
||||
case DST_STRING:
|
||||
case DST_SYMBOL:
|
||||
hash = dst_string_hash(x.data.string);
|
||||
hash = dst_string_hash(x.as.string);
|
||||
break;
|
||||
case DST_TUPLE:
|
||||
hash = dst_tuple_hash(x.data.tuple);
|
||||
hash = dst_tuple_hash(x.as.tuple);
|
||||
break;
|
||||
case DST_STRUCT:
|
||||
hash = dst_struct_hash(x.data.st);
|
||||
hash = dst_struct_hash(x.as.st);
|
||||
break;
|
||||
default:
|
||||
if (sizeof(double) == sizeof(void *)) {
|
||||
/* Assuming 8 byte pointer */
|
||||
hash = x.data.dwords[0] ^ x.data.dwords[1];
|
||||
hash = x.as.dwords[0] ^ x.as.dwords[1];
|
||||
} else {
|
||||
/* Assuming 4 byte pointer (or smaller) */
|
||||
hash = (uint32_t) x.data.pointer;
|
||||
hash = (uint32_t) x.as.pointer;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
/* Computes hash of an array of values */
|
||||
uint32_t dst_calchash_array(const DstValue *array, uint32_t len) {
|
||||
const DstValue *end = array + len;
|
||||
uint32_t hash = 5381;
|
||||
while (array < end)
|
||||
hash = (hash << 5) + hash + dst_value_hash(*array++);
|
||||
return hash;
|
||||
}
|
||||
|
||||
/* Compare two strings */
|
||||
int dst_string_compare(const uint8_t *lhs, const uint8_t *rhs) {
|
||||
uint32_t xlen = dst_string_length(lhs);
|
||||
uint32_t ylen = dst_string_length(rhs);
|
||||
uint32_t len = xlen > ylen ? ylen : xlen;
|
||||
uint32_t i;
|
||||
for (i = 0; i < len; ++i) {
|
||||
if (lhs[i] == rhs[i]) {
|
||||
continue;
|
||||
} else if (lhs[i] < rhs[i]) {
|
||||
return -1; /* x is less than y */
|
||||
} else {
|
||||
return 1; /* y is less than x */
|
||||
}
|
||||
}
|
||||
if (xlen == ylen) {
|
||||
return 0;
|
||||
} else {
|
||||
return xlen < ylen ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
int dst_value_compare(DstValue x, DstValue y) {
|
||||
int dst_compare(DstValue x, DstValue y) {
|
||||
if (x.type == y.type) {
|
||||
switch (x.type) {
|
||||
case DST_NIL:
|
||||
return 0;
|
||||
case DST_BOOLEAN:
|
||||
if (x.data.boolean == y.data.boolean) {
|
||||
if (x.as.boolean == y.as.boolean) {
|
||||
return 0;
|
||||
} else {
|
||||
return x.data.boolean ? 1 : -1;
|
||||
return x.as.boolean ? 1 : -1;
|
||||
}
|
||||
case DST_REAL:
|
||||
if (x.data.real == y.data.real) {
|
||||
if (x.as.real == y.as.real) {
|
||||
return 0;
|
||||
} else {
|
||||
return x.data.real > y.data.real ? 1 : -1;
|
||||
return x.as.real > y.as.real ? 1 : -1;
|
||||
}
|
||||
case DST_INTEGER:
|
||||
if (x.data.integer == y.data.integer) {
|
||||
if (x.as.integer == y.as.integer) {
|
||||
return 0;
|
||||
} else {
|
||||
return x.data.integer > y.data.integer ? 1 : -1;
|
||||
return x.as.integer > y.as.integer ? 1 : -1;
|
||||
}
|
||||
case DST_STRING:
|
||||
return dst_string_compare(x.data.string, y.data.string);
|
||||
return dst_string_compare(x.as.string, y.as.string);
|
||||
/* Lower indices are most significant */
|
||||
case DST_TUPLE:
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t xlen = dst_tuple_length(x.data.tuple);
|
||||
uint32_t ylen = dst_tuple_length(y.data.tuple);
|
||||
uint32_t xlen = dst_tuple_length(x.as.tuple);
|
||||
uint32_t ylen = dst_tuple_length(y.as.tuple);
|
||||
uint32_t count = xlen < ylen ? xlen : ylen;
|
||||
for (i = 0; i < count; ++i) {
|
||||
int comp = dst_value_compare(x.data.tuple[i], y.data.tuple[i]);
|
||||
int comp = dst_value_compare(x.as.tuple[i], y.as.tuple[i]);
|
||||
if (comp != 0) return comp;
|
||||
}
|
||||
if (xlen < ylen)
|
||||
@@ -135,10 +170,10 @@ int dst_value_compare(DstValue x, DstValue y) {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (x.data.string == y.data.string) {
|
||||
if (x.as.string == y.as.string) {
|
||||
return 0;
|
||||
} else {
|
||||
return x.data.string > y.data.string ? 1 : -1;
|
||||
return x.as.string > y.as.string ? 1 : -1;
|
||||
}
|
||||
}
|
||||
} else if (x.type < y.type) {
|
||||
@@ -146,78 +181,3 @@ int dst_value_compare(DstValue x, DstValue y) {
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dst_truthy(Dst *vm, uint32_t x) {
|
||||
return dst_value_truthy(dst_arg(vm, x));
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
/* Get the length of an object. Returns errors for invalid types */
|
||||
uint32_t dst_length(Dst *vm, uint32_t n) {
|
||||
DstValue x = dst_arg(vm, n);
|
||||
uint32_t length;
|
||||
switch (x.type) {
|
||||
default:
|
||||
vm->ret = dst_string_cv(vm, "cannot get length");
|
||||
vm->flags = 1;
|
||||
return 0;
|
||||
case DST_STRING:
|
||||
length = dst_string_length(x.data.string);
|
||||
break;
|
||||
case DST_ARRAY:
|
||||
length = x.data.array->count;
|
||||
break;
|
||||
case DST_BYTEBUFFER:
|
||||
length = x.data.buffer->count;
|
||||
break;
|
||||
case DST_TUPLE:
|
||||
length = dst_tuple_length(x.data.tuple);
|
||||
break;
|
||||
case DST_STRUCT:
|
||||
length = dst_struct_length(x.data.st);
|
||||
break;
|
||||
case DST_TABLE:
|
||||
length = x.data.table->count;
|
||||
break;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user