Added simple arrays.
This commit is contained in:
parent
0b6cab8672
commit
af16311ed5
8 changed files with 370 additions and 6 deletions
15
src/value.h
15
src/value.h
|
|
@ -4,11 +4,13 @@
|
|||
#include <inttypes.h>
|
||||
|
||||
struct Bytecode; /* forward */
|
||||
struct Array; /* forward */
|
||||
|
||||
typedef enum {
|
||||
VAL_INT,
|
||||
VAL_STRING,
|
||||
VAL_FUNCTION,
|
||||
VAL_ARRAY,
|
||||
VAL_NIL
|
||||
} ValueType;
|
||||
|
||||
|
|
@ -18,6 +20,7 @@ typedef struct {
|
|||
int64_t i;
|
||||
char *s;
|
||||
struct Bytecode *fn;
|
||||
struct Array *arr;
|
||||
};
|
||||
} Value;
|
||||
|
||||
|
|
@ -27,9 +30,15 @@ Value make_string(const char *s);
|
|||
Value make_function(struct Bytecode *fn);
|
||||
Value make_nil(void);
|
||||
|
||||
/* copy (deep for strings), free (free string only) */
|
||||
Value copy_value(const Value *v);
|
||||
void free_value(Value v);
|
||||
/* 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 */
|
||||
|
||||
/* utilities */
|
||||
void print_value(const Value *v);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue