1
0
Fork 0
forked from fun/fun
fun/src/value.h

51 lines
1.3 KiB
C
Raw Normal View History

2025-09-13 05:09:20 +02:00
#ifndef FUN_VALUE_H
#define FUN_VALUE_H
#include <inttypes.h>
struct Bytecode; /* forward */
2025-09-15 01:36:20 +02:00
struct Array; /* forward */
2025-09-13 05:09:20 +02:00
typedef enum {
VAL_INT,
VAL_STRING,
VAL_FUNCTION,
2025-09-15 01:36:20 +02:00
VAL_ARRAY,
2025-09-13 05:09:20 +02:00
VAL_NIL
} ValueType;
typedef struct {
ValueType type;
union {
int64_t i;
char *s;
struct Bytecode *fn;
2025-09-15 01:36:20 +02:00
struct Array *arr;
2025-09-13 05:09:20 +02:00
};
} Value;
/* constructors / helpers */
Value make_int(int64_t v);
Value make_string(const char *s);
Value make_function(struct Bytecode *fn);
Value make_nil(void);
2025-09-15 01:36:20 +02:00
/* arrays */
Value make_array_from_values(const Value *vals, int count); /* deep-copies vals */
int array_length(const Value *v); /* returns -1 if not array */
int array_get_copy(const Value *v, int index, Value *out); /* returns 0 on error; out = copy_value(item) */
int array_set(Value *v, int index, Value newElem); /* returns 0 on error; takes ownership of newElem */
/* copy/free */
Value copy_value(const Value *v); /* deep for strings, RC for arrays, shallow fn */
void free_value(Value v); /* frees owned resources */
2025-09-13 05:09:20 +02:00
/* utilities */
void print_value(const Value *v);
int value_is_truthy(const Value *v);
/* stringify into a newly-allocated C string; caller must free */
char *value_to_string_alloc(const Value *v);
2025-09-13 05:09:20 +02:00
#endif