1
0
Fork 0
forked from fun/fun

Built-ins: Maps and more... ;)

This commit is contained in:
Johannes Findeisen 2025-09-15 04:41:11 +02:00
commit d1bbd2e501
9 changed files with 663 additions and 36 deletions

View file

@ -5,12 +5,14 @@
struct Bytecode; /* forward */
struct Array; /* forward */
struct Map; /* forward */
typedef enum {
VAL_INT,
VAL_STRING,
VAL_FUNCTION,
VAL_ARRAY,
VAL_MAP,
VAL_NIL
} ValueType;
@ -21,6 +23,7 @@ typedef struct {
char *s;
struct Bytecode *fn;
struct Array *arr;
struct Map *map;
};
} Value;
@ -42,9 +45,17 @@ int array_remove(Value *v, int index, Value *out); /* returns 1 on suc
Value array_slice(const Value *v, int start, int end); /* negative end means till end */
Value array_concat(const Value *a, const Value *b); /* returns new array */
/* maps (string keys) */
Value make_map_empty(void); /* new empty map */
int map_set(Value *m, const char *key, Value v); /* 1 on ok (takes ownership of v) */
int map_get_copy(const Value *m, const char *key, Value *out);/* 1 on found, out=copy */
int map_has(const Value *m, const char *key); /* 1/0 */
Value map_keys_array(const Value *m); /* array of strings */
Value map_values_array(const Value *m); /* array of values (copies) */
/* copy/free */
Value copy_value(const Value *v); /* deep for strings, RC for arrays, shallow fn */
Value deep_copy_value(const Value *v); /* deep copy including arrays */
Value copy_value(const Value *v); /* deep for strings, RC for arrays/maps, shallow fn */
Value deep_copy_value(const Value *v); /* deep copy including arrays/maps */
void free_value(Value v); /* frees owned resources */
/* utilities */