1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-19 15:14:48 +00:00
janet/core/util.c

105 lines
3.4 KiB
C
Raw Normal View History

/*
* Copyright (c) 2017 Calvin Rose
2017-07-02 01:51:16 +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
*
* 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
*
* 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-07-03 00:51:52 +00:00
/* The DST value types in order. These types can be used as
* mnemonics instead of a bit pattern for type checking */
const char *dst_type_names[15] = {
"nil",
"real",
"integer",
"boolean",
"string",
"symbol",
"array",
"tuple",
"table",
"struct",
"fiber",
"buffer",
"function",
"cfunction",
"userdata"
};
2017-07-03 00:51:52 +00:00
2017-04-24 17:12:55 +00:00
/* Read both tuples and arrays as c pointers + uint32_t length. Return 1 if the
* view can be constructed, 0 if an invalid type. */
int dst_seq_view(DstValue seq, const DstValue **data, uint32_t *len) {
if (seq.type == DST_ARRAY) {
2017-11-01 21:53:43 +00:00
*data = seq.as.array->data;
*len = seq.as.array->count;
2017-04-24 17:12:55 +00:00
return 1;
} else if (seq.type == DST_TUPLE) {
2017-11-01 21:53:43 +00:00
*data = seq.as.st;
*len = dst_tuple_length(seq.as.st);
2017-04-24 17:12:55 +00:00
return 1;
2017-07-02 01:51:16 +00:00
}
2017-04-24 17:12:55 +00:00
return 0;
}
/* Read both strings and buffer as unsigned character array + uint32_t len.
* Returns 1 if the view can be constructed and 0 if the type is invalid. */
int dst_chararray_view(DstValue str, const uint8_t **data, uint32_t *len) {
if (str.type == DST_STRING || str.type == DST_SYMBOL) {
2017-11-01 21:53:43 +00:00
*data = str.as.string;
*len = dst_string_length(str.as.string);
2017-04-24 17:12:55 +00:00
return 1;
2017-11-01 21:53:43 +00:00
} else if (str.type == DST_BUFFER) {
*data = str.as.buffer->data;
*len = str.as.buffer->count;
2017-04-24 17:12:55 +00:00
return 1;
}
return 0;
}
/* Read both structs and tables as the entries of a hashtable with
2017-04-24 17:12:55 +00:00
* identical structure. Returns 1 if the view can be constructed and
* 0 if the type is invalid. */
int dst_hashtable_view(DstValue tab, const DstValue **data, uint32_t *len, uint32_t *cap) {
if (tab.type == DST_TABLE) {
2017-11-01 21:53:43 +00:00
*data = tab.as.table->data;
*cap = tab.as.table->capacity;
*len = tab.as.table->count;
2017-04-24 17:12:55 +00:00
return 1;
} else if (tab.type == DST_STRUCT) {
2017-11-01 21:53:43 +00:00
*data = tab.as.st;
*cap = dst_struct_capacity(tab.as.st);
*len = dst_struct_length(tab.as.st);
2017-04-24 17:12:55 +00:00
return 1;
}
return 0;
}
/* Convert a real to int */
int64_t dst_real_to_integer(double real) {
/* TODO - consider c undefined behavior */
return (int64_t) real;
}
/* Convert an integer to a real */
double dst_integer_to_real(int64_t integer) {
/* TODO - consider c undefined behavior */
return (double) integer;
}