1#ifndef REDUCT_STDLIB_IMPL_H
2#define REDUCT_STDLIB_IMPL_H 1
114 return filteredHandle;
142 accumulator = result;
180 argv[_iter.index - 1] = entry;
185 if (argv != stackBuffer)
205#define REDUCT_ANY_ALL_IMPL(_name, _predicate, _default) \
206 REDUCT_API reduct_handle_t _name(reduct_t* reduct, reduct_handle_t* list, reduct_handle_t* callable) \
208 REDUCT_ASSERT(reduct != REDUCT_NULL); \
209 REDUCT_ERROR_CHECK_LIST(reduct, list, #_name); \
210 reduct_item_t* listItem = REDUCT_HANDLE_TO_ITEM(list); \
211 reduct_handle_t fn = (callable != REDUCT_NULL) ? *callable : REDUCT_HANDLE_NONE; \
212 reduct_handle_t entry; \
213 REDUCT_LIST_FOR_EACH(&entry, &listItem->list) \
215 reduct_handle_t result = reduct_eval_maybe_call(reduct, fn, &entry); \
216 REDUCT_GC_RETAIN(reduct, result); \
219 REDUCT_GC_RELEASE(reduct, result); \
220 return REDUCT_HANDLE_FROM_INT(!(_default)); \
222 REDUCT_GC_RELEASE(reduct, result); \
224 return REDUCT_HANDLE_FROM_INT(_default); \
315 a[_iter.index - 1] = entry;
463 if (endVal > startVal)
465 count = (
reduct_size_t)((endVal - startVal + stepVal - 1) / stepVal);
470 if (startVal > endVal)
472 count = (
reduct_size_t)((startVal - endVal - stepVal - 1) / -stepVal);
651 return (len > 0) ? str[0] : defaultChar;
678 if (targetIndex < item->length)
711 buffer[targetIndex] = charToPut;
712 if (targetIndex + 1 < item->
length)
789 return reduct_assoc(reduct, handle, index, &newVal, fillVal);
823 if (targetLen <= item->length)
1056 currentPos += newLen;
1061 buffer[currentPos++] = item->
atom.
string[i++];
1109 return resultHandle;
1142 return resultHandle;
1219 if (entryH == targetEntry)
1242 if (pathItem->
length == 0)
1250 if (pathItem->
length == 1)
1252 restPath = firstKey;
1287 if (entryH == targetEntry)
1297 if (pathItem->
length == 0)
1303 if (pathItem->
length == 1)
1353 if (childItem->
length > index)
1361 return resultHandle;
1519 char *srcStr, *tgtStr;
1524 if (tgtLen > srcLen)
1528 const char* offset = start ? srcStr : srcStr + srcLen - tgtLen;
1572 totalLen += sepLen * (list->
length - 1);
1586 if (_iter.index < list->
length)
1589 currentPos += sepLen;
1603 char *srcStr, *sepStr;
1642 return resultHandle;
1699 if (start == srcLen)
1714#define REDUCT_INTROSPECTION_LOOP(_predicate) \
1717 REDUCT_ASSERT(reduct != REDUCT_NULL); \
1718 REDUCT_ASSERT(argv != REDUCT_NULL || argc == 0); \
1719 for (reduct_size_t i = 0; i < argc; i++) \
1721 if (!(_predicate)) \
1723 return REDUCT_HANDLE_FALSE(); \
1726 return REDUCT_HANDLE_TRUE(); \
1729#define REDUCT_INTROSPECTION_IMPL(_name, _predicate_macro) \
1730 REDUCT_API reduct_handle_t _name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
1732 REDUCT_UNUSED(reduct); \
1733 REDUCT_INTROSPECTION_LOOP(_predicate_macro(&argv[i])); \
1745#define REDUCT_PREDICATE_IS_STRING(_h) (REDUCT_HANDLE_IS_ATOM(_h) && !REDUCT_HANDLE_IS_NUMBER_SHAPED(_h))
1748#define REDUCT_PREDICATE_IS_EMPTY(_h) (reduct_handle_item(reduct, _h)->length == 0)
1751#define REDUCT_PREDICATE_IS_NIL(_h) (*(_h) == reduct_handle_nil(reduct))
1754#define REDUCT_GET_NUMERIC_IMPL(_name, _res, _type, _other, _targetUnion, _otherUnion) \
1755 REDUCT_API reduct_handle_t reduct_get_##_name(reduct_t* reduct, reduct_handle_t* handle) \
1757 REDUCT_ASSERT(reduct != REDUCT_NULL); \
1758 if (REDUCT_HANDLE_IS_##_type(handle)) \
1762 if (REDUCT_HANDLE_IS_##_other(handle)) \
1764 return REDUCT_HANDLE_FROM_##_res((reduct_##_name##_t)REDUCT_HANDLE_TO_##_other(handle)); \
1766 reduct_item_t* item = reduct_handle_item(reduct, handle); \
1767 if (item->flags & REDUCT_ITEM_FLAG_##_type##_SHAPED) \
1769 return REDUCT_HANDLE_FROM_##_res(item->atom._targetUnion); \
1771 if (item->flags & REDUCT_ITEM_FLAG_##_other##_SHAPED) \
1773 return REDUCT_HANDLE_FROM_##_res((reduct_##_name##_t)item->atom._otherUnion); \
1775 REDUCT_ERROR_RUNTIME(reduct, "expected " #_name ", got %s", reduct_item_type_str(item->type)); \
1821 if (pathLen > 0 && (path[0] ==
'/' || path[0] ==
'\\'))
1825 if (pathLen > 1 && ((path[0] >=
'a' && path[0] <=
'z') || (path[0] >=
'A' && path[0] <=
'Z')) && path[1] ==
':')
1843 input = funcItem->
input;
1854 const char* lastSlash = NULL;
1855 const char* p = input->
path;
1858 if (*p ==
'/' || *p ==
'\\')
1872 if (dirLen + pathLen >= maxLen)
1879 outPath[dirLen + pathLen] =
'\0';
1926 fseek(file, 0, SEEK_END);
1927 long size = ftell(file);
1928 fseek(file, 0, SEEK_SET);
1968 if (
REDUCT_FWRITE(contentStr, 1, contentLen, file) != contentLen)
1998 char* buffer = stackBuffer;
2004 if (c == EOF || c ==
'\n')
2006 if (c == EOF && length == 0)
2008 if (buffer != stackBuffer)
2017 if (length + 1 >= capacity)
2023 if (buffer != stackBuffer)
2030 if (buffer != stackBuffer)
2035 capacity = newCapacity;
2037 buffer[length++] = (char)c;
2042 if (buffer != stackBuffer)
2100 if (val < 0 || val > 255)
2102 REDUCT_ERROR_RUNTIME(reduct,
"chr expects an integer in range 0-255, got %lld", (
long long)val);
2127 if (fmtStr[i] ==
'{')
2129 if (i + 1 < fmtLen && fmtStr[i + 1] ==
'{')
2138 if (j < fmtLen && fmtStr[j] >=
'0' && fmtStr[j] <=
'9')
2141 while (j < fmtLen && fmtStr[j] >=
'0' && fmtStr[j] <=
'9')
2143 explicitIndex = explicitIndex * 10 + (fmtStr[j] -
'0');
2148 if (j < fmtLen && fmtStr[j] ==
'}')
2162 else if (fmtStr[i] ==
'}')
2164 if (i + 1 < fmtLen && fmtStr[i + 1] ==
'}')
2183 if (fmtStr[i] ==
'{')
2185 if (i + 1 < fmtLen && fmtStr[i + 1] ==
'{')
2187 buffer[currentPos++] =
'{';
2194 if (j < fmtLen && fmtStr[j] >=
'0' && fmtStr[j] <=
'9')
2197 while (j < fmtLen && fmtStr[j] >=
'0' && fmtStr[j] <=
'9')
2199 explicitIndex = explicitIndex * 10 + (fmtStr[j] -
'0');
2204 if (j < fmtLen && fmtStr[j] ==
'}')
2207 currentPos +=
reduct_stringify(reduct, &argv[idx], buffer + currentPos, totalLen - currentPos + 1);
2212 else if (fmtStr[i] ==
'}')
2214 if (i + 1 < fmtLen && fmtStr[i + 1] ==
'}')
2216 buffer[currentPos++] =
'}';
2221 buffer[currentPos++] = fmtStr[i];
2252 extern char** environ;
2262 char* env = environ[i];
2263 char* eq = strchr(env,
'=');
2285 if (reduct->argc == 0)
2291 for (
int i = 0; i < reduct->argc; i++)
2301#define REDUCT_MATH_MIN_MAX_IMPL(_name, _op) \
2302 REDUCT_API reduct_handle_t _name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2304 REDUCT_ASSERT(reduct != REDUCT_NULL); \
2307 return reduct_handle_nil(reduct); \
2309 reduct_handle_t current = argv[0]; \
2310 for (reduct_size_t i = 1; i < argc; i++) \
2312 reduct_promotion_t prom; \
2313 reduct_handle_promote(reduct, ¤t, &argv[i], &prom); \
2314 if (prom.type == REDUCT_PROMOTION_TYPE_INT) \
2316 current = REDUCT_HANDLE_FROM_INT(prom.a.intVal _op prom.b.intVal ? prom.a.intVal : prom.b.intVal); \
2321 REDUCT_HANDLE_FROM_FLOAT(prom.a.floatVal _op prom.b.floatVal ? prom.a.floatVal : prom.b.floatVal); \
2361#define REDUCT_MATH_UNARY_IMPL(_name, _int_func, _float_func) \
2362 REDUCT_API reduct_handle_t _name(reduct_t* reduct, reduct_handle_t* val) \
2364 REDUCT_ASSERT(reduct != REDUCT_NULL); \
2365 if (REDUCT_HANDLE_IS_INT_SHAPED(val)) \
2367 reduct_handle_t iVal = reduct_get_int(reduct, val); \
2368 reduct_int64_t i = REDUCT_HANDLE_TO_INT(&iVal); \
2369 return REDUCT_HANDLE_FROM_INT((reduct_int64_t)_int_func(i)); \
2371 reduct_handle_t floatVal = reduct_get_float(reduct, val); \
2372 reduct_float_t f = REDUCT_HANDLE_TO_FLOAT(&floatVal); \
2373 return REDUCT_HANDLE_FROM_FLOAT((reduct_float_t)_float_func(f)); \
2376#define REDUCT_INT_ABS(_x) ((_x) < 0 ? -(_x) : (_x))
2381#define REDUCT_MATH_UNARY_TO_INT_IMPL(_name, _float_func) \
2382 REDUCT_API reduct_handle_t _name(struct reduct* reduct, reduct_handle_t* val) \
2384 REDUCT_ASSERT(reduct != REDUCT_NULL); \
2385 if (REDUCT_HANDLE_IS_INT_SHAPED(val)) \
2387 return reduct_get_int(reduct, val); \
2389 reduct_handle_t floatVal = reduct_get_float(reduct, val); \
2390 reduct_float_t f = REDUCT_HANDLE_TO_FLOAT(&floatVal); \
2391 return REDUCT_HANDLE_FROM_INT((reduct_int64_t)_float_func(f)); \
2443#define REDUCT_MATH_UNARY_FLOAT_IMPL(_name, _func) \
2444 REDUCT_API reduct_handle_t _name(struct reduct* reduct, reduct_handle_t* val) \
2446 REDUCT_ASSERT(reduct != REDUCT_NULL); \
2447 reduct_handle_t fv = reduct_get_float(reduct, val); \
2448 return REDUCT_HANDLE_FROM_FLOAT((reduct_float_t)_func(REDUCT_HANDLE_TO_FLOAT(&fv))); \
2501#define REDUCT_STDLIB_WRAPPER_0(_name, _impl) \
2502 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2504 reduct_error_check_arity(reduct, argc, 0, #_name); \
2506 return _impl(reduct); \
2509#define REDUCT_STDLIB_WRAPPER_1(_name, _impl) \
2510 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2512 reduct_error_check_arity(reduct, argc, 1, #_name); \
2513 return _impl(reduct, &argv[0]); \
2516#define REDUCT_STDLIB_WRAPPER_2(_name, _impl) \
2517 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2519 reduct_error_check_arity(reduct, argc, 2, #_name); \
2520 return _impl(reduct, &argv[0], &argv[1]); \
2523#define REDUCT_STDLIB_WRAPPER_3(_name, _impl) \
2524 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2526 reduct_error_check_arity(reduct, argc, 3, #_name); \
2527 return _impl(reduct, &argv[0], &argv[1], &argv[2]); \
2530#define REDUCT_STDLIB_WRAPPER_R12(_name, _impl) \
2531 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2533 reduct_error_check_arity_range(reduct, argc, 1, 2, #_name); \
2534 return _impl(reduct, &argv[0], argc == 2 ? &argv[1] : REDUCT_NULL); \
2537#define REDUCT_STDLIB_WRAPPER_R23(_name, _impl) \
2538 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2540 reduct_error_check_arity_range(reduct, argc, 2, 3, #_name); \
2541 return _impl(reduct, &argv[0], &argv[1], argc == 3 ? &argv[2] : REDUCT_NULL); \
2544#define REDUCT_STDLIB_WRAPPER_R34(_name, _impl) \
2545 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2547 reduct_error_check_arity_range(reduct, argc, 3, 4, #_name); \
2548 return _impl(reduct, &argv[0], &argv[1], &argv[2], argc == 4 ? &argv[3] : REDUCT_NULL); \
2551#define REDUCT_STDLIB_WRAPPER_ARG2(_name, _impl) \
2552 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2554 reduct_error_check_arity(reduct, argc, 2, #_name); \
2555 return _impl(reduct, &argv[0], &argv[1]); \
2558#define REDUCT_STDLIB_WRAPPER_V1(_name, _impl) \
2559 static reduct_handle_t reduct_stdlib_##_name(reduct_t* reduct, reduct_size_t argc, reduct_handle_t* argv) \
2561 reduct_error_check_min_arity(reduct, argc, 1, #_name); \
2562 return _impl(reduct, argc, argv); \
2740 {
"assert!", reduct_stdlib_assert},
2741 {
"throw!", reduct_stdlib_throw},
2742 {
"try", reduct_stdlib_try},
2749 {
"map", reduct_stdlib_map},
2750 {
"filter", reduct_stdlib_filter},
2752 {
"apply", reduct_stdlib_apply},
2753 {
"any?", reduct_stdlib_any},
2754 {
"all?", reduct_stdlib_all},
2755 {
"sort", reduct_stdlib_sort},
2762 {
"len", reduct_stdlib_len},
2765 {
"first", reduct_stdlib_first},
2766 {
"last", reduct_stdlib_last},
2767 {
"rest", reduct_stdlib_rest},
2768 {
"init", reduct_stdlib_init},
2769 {
"nth", reduct_stdlib_nth},
2770 {
"assoc", reduct_stdlib_assoc},
2771 {
"dissoc", reduct_stdlib_dissoc},
2772 {
"update", reduct_stdlib_update},
2773 {
"index-of", reduct_stdlib_index_of},
2774 {
"reverse", reduct_stdlib_reverse},
2775 {
"slice", reduct_stdlib_slice},
2776 {
"flatten", reduct_stdlib_flatten},
2777 {
"contains?", reduct_stdlib_contains},
2778 {
"replace", reduct_stdlib_replace},
2779 {
"unique", reduct_stdlib_unique},
2780 {
"chunk", reduct_stdlib_chunk},
2781 {
"find", reduct_stdlib_find},
2782 {
"get-in", reduct_stdlib_get_in},
2783 {
"assoc-in", reduct_stdlib_assoc_in},
2784 {
"dissoc-in", reduct_stdlib_dissoc_in},
2785 {
"update-in", reduct_stdlib_update_in},
2786 {
"keys", reduct_stdlib_keys},
2787 {
"values", reduct_stdlib_values},
2788 {
"merge", reduct_stdlib_merge},
2789 {
"explode", reduct_stdlib_explode},
2790 {
"implode", reduct_stdlib_implode},
2798 {
"starts-with?", reduct_stdlib_starts_with},
2799 {
"ends-with?", reduct_stdlib_ends_with},
2800 {
"contains?", reduct_stdlib_contains},
2801 {
"replace", reduct_stdlib_replace},
2802 {
"join", reduct_stdlib_join},
2803 {
"split", reduct_stdlib_split},
2804 {
"upper", reduct_stdlib_upper},
2805 {
"lower", reduct_stdlib_lower},
2806 {
"trim", reduct_stdlib_trim},
2813 {
"atom?", reduct_stdlib_is_atom},
2814 {
"int?", reduct_stdlib_is_int},
2815 {
"float?", reduct_stdlib_is_float},
2816 {
"number?", reduct_stdlib_is_number},
2817 {
"string?", reduct_stdlib_is_string},
2818 {
"lambda?", reduct_stdlib_is_lambda},
2819 {
"native?", reduct_stdlib_is_native},
2820 {
"callable?", reduct_stdlib_is_callable},
2821 {
"list?", reduct_stdlib_is_list},
2822 {
"empty?", reduct_stdlib_is_empty},
2823 {
"nil?", reduct_stdlib_is_nil},
2830 {
"int", reduct_stdlib_int},
2831 {
"float", reduct_stdlib_float},
2832 {
"string", reduct_stdlib_string},
2839 {
"eval", reduct_stdlib_eval},
2840 {
"parse", reduct_stdlib_parse},
2841 {
"run", reduct_stdlib_run},
2842 {
"load!", reduct_stdlib_load},
2843 {
"read-file!", reduct_stdlib_read_file},
2844 {
"write-file!", reduct_stdlib_write_file},
2845 {
"read-char!", reduct_stdlib_read_char},
2846 {
"read-line!", reduct_stdlib_read_line},
2849 {
"ord", reduct_stdlib_ord},
2850 {
"chr", reduct_stdlib_chr},
2852 {
"now!", reduct_stdlib_now},
2853 {
"uptime!", reduct_stdlib_uptime},
2854 {
"env!", reduct_stdlib_env},
2855 {
"args!", reduct_stdlib_args},
2862 {
"min", reduct_stdlib_min},
2863 {
"max", reduct_stdlib_max},
2864 {
"clamp", reduct_stdlib_clamp},
2865 {
"abs", reduct_stdlib_abs},
2866 {
"floor", reduct_stdlib_floor},
2867 {
"ceil", reduct_stdlib_ceil},
2868 {
"round", reduct_stdlib_round},
2869 {
"pow", reduct_stdlib_pow},
2870 {
"exp", reduct_stdlib_exp},
2871 {
"log", reduct_stdlib_log},
2872 {
"sqrt", reduct_stdlib_sqrt},
2873 {
"sin", reduct_stdlib_sin},
2874 {
"cos", reduct_stdlib_cos},
2875 {
"tan", reduct_stdlib_tan},
2876 {
"asin", reduct_stdlib_asin},
2877 {
"acos", reduct_stdlib_acos},
2878 {
"atan", reduct_stdlib_atan},
2879 {
"atan2", reduct_stdlib_atan2},
2880 {
"sinh", reduct_stdlib_sinh},
2881 {
"cosh", reduct_stdlib_cosh},
2882 {
"tanh", reduct_stdlib_tanh},
2883 {
"asinh", reduct_stdlib_asinh},
2884 {
"acosh", reduct_stdlib_acosh},
2885 {
"atanh", reduct_stdlib_atanh},
2886 {
"rand", reduct_stdlib_rand},
2887 {
"seed!", reduct_stdlib_seed},
Core definitions and structures.
#define REDUCT_MIN(_a, _b)
#define REDUCT_MALLOC(_size)
#define REDUCT_SCRATCH_BUFFER_FREE(_name)
Scratch buffer free macro.
#define REDUCT_ATAN2(_y, _x)
reduct_bool_t
Boolean type.
#define REDUCT_MEMCMP(_s1, _s2, _size)
#define REDUCT_FCLOSE(_file)
#define REDUCT_FGETC(_file)
#define REDUCT_FWRITE(_ptr, _size, _nmemb, _file)
#define REDUCT_SCRATCH_BUFFER(_name, _size)
Scratch buffer macro.
#define REDUCT_CONTAINER_OF(_ptr, _type, _member)
Container of macro.
#define REDUCT_FOPEN(_path, _mode)
#define REDUCT_MEMCPY(_dest, _src, _size)
reduct_uint64_t reduct_handle_t
Handle type.
#define REDUCT_SRAND(_seed)
#define REDUCT_FREAD(_ptr, _size, _nmemb, _file)
#define REDUCT_ASSERT(_cond)
#define REDUCT_POW(_x, _y)
#define REDUCT_PATH_MAX
Maximum path length for Reduct.
#define REDUCT_FREE(_ptr)
#define REDUCT_MAX(_a, _b)
#define REDUCT_UNUSED(_x)
#define REDUCT_STACK_BUFFER_SIZE
The size of temporary stack allocated buffers.
Virtual machine evaluation.
REDUCT_API reduct_atom_t * reduct_atom_lookup(struct reduct *reduct, const char *str, reduct_size_t len, reduct_atom_lookup_flags_t flags)
Lookup an atom in the Reduct structure.
@ REDUCT_ATOM_LOOKUP_NONE
No flags.
@ REDUCT_ATOM_LOOKUP_QUOTED
Atom should be explicitly quoted.
#define REDUCT_CHAR_TO_LOWER(_c)
Get the lowercase equivalent of a character.
#define REDUCT_CHAR_TO_UPPER(_c)
Get the uppercase equivalent of a character.
#define REDUCT_CHAR_IS_WHITESPACE(_c)
Check if a character is whitespace.
REDUCT_API reduct_function_t * reduct_compile(reduct_t *reduct, reduct_handle_t *ast)
Compiles a Reduct AST into a callable bytecode function.
#define REDUCT_ERROR_RUNTIME(_reduct,...)
Throw a runtime error using the jump buffer in the error structure.
#define REDUCT_ERROR()
Create a Reduct error structure.
REDUCT_API void reduct_error_check_arity(struct reduct *reduct, reduct_size_t argc, reduct_size_t expected, const char *name)
Check the arity of a native function call.
#define REDUCT_ERROR_INTERNAL(_reduct,...)
Throw an internal error using the jump buffer in the error structure.
#define REDUCT_ERROR_CATCH(_error)
Catch an error using the jump buffer in the error structure.
#define REDUCT_ERROR_CHECK_LIST(_reduct, _handle, _name)
Assert that a handle is a list.
REDUCT_API void reduct_error_check_arity_range(struct reduct *reduct, reduct_size_t argc, reduct_size_t min, reduct_size_t max, const char *name)
#define REDUCT_ERROR_CHECK_CALLABLE(_reduct, _handle, _name)
Assert that a handle is a callable.
#define REDUCT_ERROR_CHECK_SEQUENCE(_reduct, _handle, _name)
Assert that a handle is a sequence (list or atom).
REDUCT_API reduct_handle_t reduct_eval_call(reduct_t *reduct, reduct_handle_t callable, reduct_size_t argc, reduct_handle_t *argv)
Calls a Reduct callable (closure or native) with arguments.
REDUCT_API reduct_handle_t reduct_eval(reduct_t *reduct, reduct_function_t *function)
Evaluates a compiled Reduct function.
#define REDUCT_GC_RETAIN(_reduct, _handle)
Retain an item, preventing it from being collected by the GC.
#define REDUCT_GC_RELEASE(_reduct, _handle)
Release a previously retained item, allowing it to be collected by the GC.
#define REDUCT_HANDLE_FALSE()
Constant false handle.
#define REDUCT_HANDLE_IS_LAMBDA(_handle)
Check if a handle is a lambda.
#define REDUCT_HANDLE_IS_NUMBER_SHAPED(_handle)
Check if a handle is a number or references a number shaped item.
#define REDUCT_HANDLE_FROM_LIST(_list)
Create a handle from a list pointer.
#define REDUCT_HANDLE_FROM_BOOL(_cond)
Create a boolean handle from a C condition.
#define REDUCT_HANDLE_TO_INT(_handle)
Get the integer value of a handle.
#define REDUCT_HANDLE_TO_ITEM(_handle)
Get the item pointer of a handle.
REDUCT_API void reduct_handle_promote(struct reduct *reduct, reduct_handle_t *a, reduct_handle_t *b, reduct_promotion_t *out)
Promote two handles to a common numeric type.
REDUCT_API reduct_handle_t reduct_handle_nil(struct reduct *reduct)
Get the constant nil handle.
#define REDUCT_HANDLE_FROM_FLOAT(_val)
Create a handle from a float.
#define REDUCT_HANDLE_IS_CALLABLE(_handle)
Check if a handle is callable.
#define REDUCT_HANDLE_GET_TYPE(_handle)
Get the type of the item referenced by the handle, or REDUCT_ITEM_TYPE_ATOM if not an item.
#define REDUCT_HANDLE_IS_LIST(_handle)
Check if a handle is a list.
#define REDUCT_HANDLE_TO_FLOAT(_handle)
Get the float value of a handle.
#define REDUCT_HANDLE_FROM_ATOM(_atom)
Create a handle from an atom pointer.
#define REDUCT_HANDLE_FROM_INT(_val)
Create a handle from an integer.
REDUCT_API struct reduct_item * reduct_handle_item(struct reduct *reduct, reduct_handle_t *handle)
Ensure that a handle is an item and return the pointer.
#define REDUCT_HANDLE_NONE
Invalid handle constant.
REDUCT_API void reduct_handle_get_string_params(struct reduct *reduct, reduct_handle_t *handle, char **outStr, reduct_size_t *outLen)
Get the string pointer and length from an atom handle.
#define REDUCT_HANDLE_IS_TRUTHY(_handle)
Check if a handle is truthy.
REDUCT_API reduct_int64_t reduct_handle_compare(struct reduct *reduct, reduct_handle_t *a, reduct_handle_t *b)
Compare two items for ordering (less than, equal, or greater than).
#define REDUCT_HANDLE_IS_FLOAT_SHAPED(_handle)
Check if a handle is a float or references a float shaped item.
#define REDUCT_HANDLE_IS_INT_SHAPED(_handle)
Check if a handle is an integer of references a integer shaped item.
#define REDUCT_HANDLE_TRUE()
Constant true handle.
#define REDUCT_HANDLE_IS_NATIVE(_handle)
Check if a handle is a native function.
#define REDUCT_HANDLE_IS_ATOM(_handle)
Check if a handle is an atom.
@ REDUCT_PROMOTION_TYPE_INT
#define REDUCT_ITEM_TYPE_LIST
A list.
REDUCT_API const char * reduct_item_type_str(reduct_item_type_t type)
Get the string representation of an Reduct item type.
#define REDUCT_ITEM_TYPE_ATOM
An atom.
REDUCT_API void reduct_native_register(struct reduct *reduct, reduct_native_t *array, reduct_size_t count)
Register native functions.
REDUCT_API reduct_handle_t reduct_parse_file(reduct_t *reduct, const char *path)
Parse a Reduct file.
REDUCT_API reduct_handle_t reduct_parse(reduct_t *reduct, const char *str, reduct_size_t len, const char *path)
Parse a Reduct string.
REDUCT_API reduct_handle_t reduct_sqrt(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_round(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_is_empty(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_cosh(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_sin(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_any(struct reduct *reduct, reduct_handle_t *list, reduct_handle_t *callable)
REDUCT_API reduct_handle_t reduct_write_file(struct reduct *reduct, reduct_handle_t *path, reduct_handle_t *content)
REDUCT_API reduct_handle_t reduct_atan2(struct reduct *reduct, reduct_handle_t *y, reduct_handle_t *x)
REDUCT_API reduct_handle_t reduct_read_line(struct reduct *reduct)
REDUCT_API reduct_handle_t reduct_exp(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_acosh(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_acos(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_unique(struct reduct *reduct, reduct_handle_t *handle)
REDUCT_API reduct_handle_t reduct_max(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_chunk(struct reduct *reduct, reduct_handle_t *handle, reduct_handle_t *sizeH)
REDUCT_API reduct_handle_t reduct_asin(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_is_atom(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_read_char(struct reduct *reduct)
REDUCT_API reduct_handle_t reduct_sinh(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_env(struct reduct *reduct)
REDUCT_API reduct_handle_t reduct_chr(struct reduct *reduct, reduct_handle_t *handle)
REDUCT_API reduct_handle_t reduct_is_float(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_find(struct reduct *reduct, reduct_handle_t *handle, reduct_handle_t *callable)
REDUCT_API reduct_handle_t reduct_range(struct reduct *reduct, reduct_handle_t *start, reduct_handle_t *end, reduct_handle_t *step)
REDUCT_API reduct_handle_t reduct_is_lambda(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_contains(struct reduct *reduct, reduct_handle_t *handle, reduct_handle_t *target)
REDUCT_API reduct_handle_t reduct_cos(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_seed(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_get_float(struct reduct *reduct, reduct_handle_t *handle)
REDUCT_API reduct_handle_t reduct_is_int(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_is_list(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_dissoc(struct reduct *reduct, reduct_handle_t *handle, reduct_handle_t *index)
REDUCT_API reduct_handle_t reduct_ord(struct reduct *reduct, reduct_handle_t *handle)
REDUCT_API reduct_handle_t reduct_floor(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_flatten(struct reduct *reduct, reduct_handle_t *handle, reduct_handle_t *depthH)
REDUCT_API reduct_handle_t reduct_is_number(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_args(struct reduct *reduct)
REDUCT_API reduct_handle_t reduct_atan(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_is_nil(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_abs(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_all(struct reduct *reduct, reduct_handle_t *list, reduct_handle_t *callable)
REDUCT_API reduct_handle_t reduct_ceil(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_log(struct reduct *reduct, reduct_handle_t *val, reduct_handle_t *base)
REDUCT_API reduct_handle_t reduct_is_callable(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
reduct_stdlib_sets_t
Built-in library sets.
REDUCT_API reduct_handle_t reduct_run(struct reduct *reduct, reduct_handle_t *handle)
REDUCT_API reduct_handle_t reduct_atanh(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_rand(struct reduct *reduct, reduct_handle_t *minVal, reduct_handle_t *maxVal)
REDUCT_API reduct_handle_t reduct_get_int(struct reduct *reduct, reduct_handle_t *handle)
REDUCT_API reduct_handle_t reduct_is_string(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_is_native(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_asinh(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_min(struct reduct *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_tanh(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_read_file(struct reduct *reduct, reduct_handle_t *path)
REDUCT_API reduct_handle_t reduct_replace(struct reduct *reduct, reduct_handle_t *handle, reduct_handle_t *oldVal, reduct_handle_t *newVal)
REDUCT_API reduct_handle_t reduct_tan(struct reduct *reduct, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_load(struct reduct *reduct, reduct_handle_t *path)
@ REDUCT_STDLIB_SEQUENCES
@ REDUCT_STDLIB_HIGHER_ORDER
@ REDUCT_STDLIB_INTROSPECTION
@ REDUCT_STDLIB_TYPE_CASTING
REDUCT_API reduct_size_t reduct_stringify(reduct_t *reduct, reduct_handle_t *handle, char *buffer, reduct_size_t size)
Converts a Reduct handle to its string representation.
REDUCT_API reduct_handle_t reduct_list_nth(struct reduct *reduct, reduct_list_t *list, reduct_size_t index)
Get the nth element of the list.
REDUCT_API reduct_list_t * reduct_list_dissoc(struct reduct *reduct, reduct_list_t *list, reduct_size_t index)
Create a new list with the element at the specified index removed.
REDUCT_API reduct_list_t * reduct_list_assoc(struct reduct *reduct, reduct_list_t *list, reduct_size_t index, reduct_handle_t val)
Create a new list with an updated value at the specified index.
REDUCT_API reduct_list_t * reduct_list_slice(struct reduct *reduct, reduct_list_t *list, reduct_size_t start, reduct_size_t end)
Create a new list by slicing an existing list.
REDUCT_API void reduct_list_append(struct reduct *reduct, reduct_list_t *list, reduct_handle_t val)
Append an element to the list.
#define REDUCT_LIST_FOR_EACH(_handle, _list)
Macro for iterating over all elements in a list.
REDUCT_API void reduct_list_append_list(struct reduct *reduct, reduct_list_t *list, reduct_list_t *other)
Append all elements from one list to another.
REDUCT_API reduct_list_t * reduct_list_new(struct reduct *reduct)
Create a new editable list.
Native function registration.
Built-in library registration and operations.
static reduct_handle_t reduct_list_find_entry(reduct_t *reduct, reduct_item_t *listItem, reduct_handle_t *key)
REDUCT_API reduct_handle_t reduct_sort(reduct_t *reduct, reduct_handle_t *listHandle, reduct_handle_t *callableHandle)
static reduct_handle_t reduct_eval_maybe_call(reduct_t *reduct, reduct_handle_t fn, reduct_handle_t *arg)
REDUCT_API reduct_handle_t reduct_ends_with(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *suffix)
REDUCT_API reduct_handle_t reduct_get_string(reduct_t *reduct, reduct_handle_t *handle)
REDUCT_API reduct_handle_t reduct_reduce(reduct_t *reduct, reduct_handle_t *list, reduct_handle_t *initial, reduct_handle_t *callable)
#define REDUCT_INT_ABS(_x)
REDUCT_API reduct_handle_t reduct_split(reduct_t *reduct, reduct_handle_t *srcHandle, reduct_handle_t *sepHandle)
REDUCT_API void reduct_stdlib_register(reduct_t *reduct, reduct_stdlib_sets_t sets)
#define REDUCT_STDLIB_WRAPPER_R34(_name, _impl)
REDUCT_API reduct_handle_t reduct_starts_with(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *prefix)
#define REDUCT_STDLIB_WRAPPER_V1(_name, _impl)
#define REDUCT_MATH_UNARY_TO_INT_IMPL(_name, _float_func)
#define REDUCT_STDLIB_WRAPPER_0(_name, _impl)
#define REDUCT_PREDICATE_IS_EMPTY(_h)
REDUCT_API reduct_handle_t reduct_slice(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *startH, reduct_handle_t *endH)
REDUCT_API reduct_handle_t reduct_trim(reduct_t *reduct, reduct_handle_t *srcHandle)
REDUCT_API reduct_handle_t reduct_concat(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_rest(reduct_t *reduct, reduct_handle_t *handle)
static reduct_handle_t reduct_stdlib_eval_impl(reduct_t *reduct, reduct_handle_t *arg)
#define REDUCT_STDLIB_WRAPPER_R23(_name, _impl)
REDUCT_API reduct_handle_t reduct_assoc(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *index, reduct_handle_t *value, reduct_handle_t *fillVal)
REDUCT_API reduct_handle_t reduct_implode(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
static char reduct_handle_get_char(reduct_t *reduct, reduct_handle_t *handle, char defaultChar)
REDUCT_API reduct_handle_t reduct_try(reduct_t *reduct, reduct_handle_t *callable, reduct_handle_t *catchFn)
REDUCT_API reduct_handle_t reduct_apply(reduct_t *reduct, reduct_handle_t *list, reduct_handle_t *callable)
REDUCT_API reduct_handle_t reduct_repeat(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *count)
static reduct_handle_t reduct_stdlib_parse_impl(reduct_t *reduct, reduct_handle_t *arg)
#define REDUCT_GET_NUMERIC_IMPL(_name, _res, _type, _other, _targetUnion, _otherUnion)
static reduct_handle_t reduct_stdlib_println(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
static void reduct_path_copy(reduct_t *reduct, char *dest, const char *src, reduct_size_t len, reduct_size_t max)
REDUCT_API reduct_handle_t reduct_index_of(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *target)
#define REDUCT_INTROSPECTION_IMPL(_name, _predicate_macro)
static reduct_handle_t reduct_stdlib_repeat(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_now(reduct_t *reduct)
REDUCT_API reduct_handle_t reduct_throw(reduct_t *reduct, reduct_handle_t *msg)
REDUCT_API reduct_handle_t reduct_len(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
#define REDUCT_STDLIB_WRAPPER_R12(_name, _impl)
REDUCT_API reduct_handle_t reduct_values(reduct_t *reduct, reduct_handle_t *listHandle)
REDUCT_API reduct_handle_t reduct_update(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *index, reduct_handle_t *callable, reduct_handle_t *fillVal)
#define REDUCT_STDLIB_WRAPPER_3(_name, _impl)
REDUCT_API reduct_handle_t reduct_update_in(reduct_t *reduct, reduct_handle_t *list, reduct_handle_t *path, reduct_handle_t *callable)
REDUCT_API reduct_handle_t reduct_format(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
static void reduct_resolve_path(reduct_t *reduct, const char *path, reduct_size_t pathLen, char *outPath, reduct_size_t maxLen)
REDUCT_API reduct_handle_t reduct_merge(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_assoc_in(reduct_t *reduct, reduct_handle_t *list, reduct_handle_t *path, reduct_handle_t *val)
REDUCT_API reduct_handle_t reduct_get_in(reduct_t *reduct, reduct_handle_t *list, reduct_handle_t *path, reduct_handle_t *defaultVal)
REDUCT_API reduct_handle_t reduct_keys(reduct_t *reduct, reduct_handle_t *listHandle)
REDUCT_API reduct_handle_t reduct_lower(reduct_t *reduct, reduct_handle_t *srcHandle)
static void reduct_sort_merge(reduct_t *reduct, reduct_handle_t callable, reduct_handle_t *a, reduct_size_t left, reduct_size_t right, reduct_size_t end, reduct_handle_t *b)
REDUCT_API reduct_handle_t reduct_filter(reduct_t *reduct, reduct_handle_t *list, reduct_handle_t *callable)
#define REDUCT_STDLIB_WRAPPER_2(_name, _impl)
static reduct_handle_t reduct_stdlib_range(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_first(reduct_t *reduct, reduct_handle_t *handle)
REDUCT_API reduct_handle_t reduct_assert(reduct_t *reduct, reduct_handle_t *cond, reduct_handle_t *msg)
REDUCT_API reduct_handle_t reduct_pow(reduct_t *reduct, reduct_handle_t *base, reduct_handle_t *exp)
REDUCT_API reduct_handle_t reduct_clamp(reduct_t *reduct, reduct_handle_t *val, reduct_handle_t *minVal, reduct_handle_t *maxVal)
REDUCT_API reduct_handle_t reduct_nth(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *index, reduct_handle_t *defaultVal)
#define REDUCT_PREDICATE_IS_STRING(_h)
REDUCT_API reduct_handle_t reduct_print(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
static reduct_int64_t reduct_handle_normalize_index(reduct_t *reduct, reduct_handle_t *index, reduct_size_t length)
REDUCT_API reduct_handle_t reduct_println(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
static reduct_handle_t reduct_stdlib_reduce(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
static reduct_handle_t reduct_sequence_trim(reduct_t *reduct, reduct_handle_t *handle, reduct_bool_t rest)
REDUCT_API reduct_handle_t reduct_dissoc_in(reduct_t *reduct, reduct_handle_t *list, reduct_handle_t *path)
REDUCT_API reduct_handle_t reduct_init(reduct_t *reduct, reduct_handle_t *handle)
static reduct_handle_t reduct_sequence_check_edge(reduct_t *reduct, reduct_handle_t *handle, reduct_handle_t *target, reduct_bool_t start, const char *name)
#define REDUCT_MATH_UNARY_FLOAT_IMPL(_name, _func)
REDUCT_API reduct_handle_t reduct_uptime(reduct_t *reduct)
REDUCT_API reduct_handle_t reduct_join(reduct_t *reduct, reduct_handle_t *listHandle, reduct_handle_t *sepHandle)
#define REDUCT_PREDICATE_IS_NIL(_h)
#define REDUCT_MATH_UNARY_IMPL(_name, _int_func, _float_func)
static reduct_handle_t reduct_list_project(reduct_t *reduct, reduct_handle_t *listHandle, reduct_size_t index, const char *name)
static reduct_handle_t reduct_stdlib_print(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
REDUCT_API reduct_handle_t reduct_explode(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
static reduct_handle_t reduct_sequence_edge(reduct_t *reduct, reduct_handle_t *handle, reduct_bool_t first)
static reduct_handle_t reduct_stdlib_format(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
#define REDUCT_MATH_MIN_MAX_IMPL(_name, _op)
REDUCT_API reduct_handle_t reduct_upper(reduct_t *reduct, reduct_handle_t *srcHandle)
#define REDUCT_STDLIB_WRAPPER_1(_name, _impl)
REDUCT_API reduct_handle_t reduct_reverse(reduct_t *reduct, reduct_handle_t *handle)
static reduct_handle_t reduct_stdlib_concat(reduct_t *reduct, reduct_size_t argc, reduct_handle_t *argv)
static reduct_bool_t reduct_list_get_entry(reduct_t *reduct, reduct_handle_t *entryH, reduct_handle_t *outKey, reduct_handle_t *outVal)
static reduct_handle_t reduct_string_transform(reduct_t *reduct, reduct_handle_t *srcHandle, reduct_bool_t upper)
#define REDUCT_ANY_ALL_IMPL(_name, _predicate, _default)
#define REDUCT_STDLIB_WRAPPER_ARG2(_name, _impl)
REDUCT_API reduct_handle_t reduct_last(reduct_t *reduct, reduct_handle_t *handle)
static void reduct_sequence_normalize_range(reduct_t *reduct, reduct_handle_t *startH, reduct_handle_t *endH, reduct_size_t length, reduct_size_t *outStart, reduct_size_t *outEnd)
static void reduct_get_resolved_path(reduct_t *reduct, reduct_handle_t *pathHandle, char *outBuf)
REDUCT_API reduct_handle_t reduct_map(reduct_t *reduct, reduct_handle_t *list, reduct_handle_t *callable)
char * string
Pointer to the string.
char message[REDUCT_ERROR_MAX_LEN]
Evaluation frame structure.
struct reduct_closure * closure
The closure being evaluated.
Compiled function structure.
reduct_list_t list
A list.
reduct_uint32_t length
Common length for the item. (Stored in the union to save space due to padding rules....
struct reduct_input * input
The parsed input that created this item.
reduct_atom_t atom
An atom.
reduct_item_type_t type
The type of the item.
Native function definition structure.
struct reduct_eval_state * evalState