From 2e62b41072738142dea9f0b5dd5d2d22455c7616 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Wed, 26 Jun 2024 19:27:42 +0100 Subject: add arrays, appending, getting length, indexing --- val.h | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'val.h') diff --git a/val.h b/val.h index 90b60f2..4f77828 100644 --- a/val.h +++ b/val.h @@ -41,6 +41,7 @@ bool val_equal(Val a, Val b); typedef enum { OTY_STRING, OTY_FUNC, + OTY_ARR, } ObjTy; typedef struct _obj { @@ -53,13 +54,8 @@ typedef struct { uint32_t hash; char *d; } ObjString; -#include "chunk.h" -typedef struct { - Obj obj; - Chunk ch; - uint8_t arity; -} ObjFunc; +#include "chunk.h" // Constructs a new objstring from the given C string, // creating its own fresh copy of the data. @@ -69,8 +65,25 @@ ObjString *objstring_copy_cstr(State *s, char *str); // taking ownership of the provided data. ObjString *objstring_take(State *S, char *src, size_t len); +typedef struct { + Obj obj; + Chunk ch; + uint8_t arity; +} ObjFunc; ObjFunc *objfunc_new(State *S, uint8_t arity); +typedef struct { + Obj obj; + size_t len; + size_t cap; + Val *d; +} ObjArr; + +ObjArr *objarr_new(State *S); +Val objarr_get(State *S, ObjArr *arr, size_t ix); +void objarr_append(State *S, ObjArr *arr, Val v); +void objarr_put(State *S, ObjArr *arr, size_t ix, Val v); + #define IS_NIL(x) (x.ty == TY_NIL) #define IS_NUM(x) (x.ty == TY_NUM) #define IS_BOOL(x) (x.ty == TY_BOOL) @@ -79,6 +92,7 @@ ObjFunc *objfunc_new(State *S, uint8_t arity); #define IS_STRING(x) (is_obj_ty((x), OTY_STRING)) #define IS_FUNC(x) (is_obj_ty((x), OTY_FUNC)) +#define IS_ARR(x) (is_obj_ty((x), OTY_ARR)) #define AS_NUM(x) (x.as.d) #define AS_BOOL(x) (x.as.b) @@ -88,6 +102,7 @@ ObjFunc *objfunc_new(State *S, uint8_t arity); #define AS_STRING(x) ((ObjString*)AS_OBJ(x)) #define AS_CSTRING(x) (AS_STRING(x)->d) #define AS_FUNC(x) ((ObjFunc*)AS_OBJ(x)) +#define AS_ARR(x) ((ObjArr*)AS_OBJ(x)) #define VAL_NIL ((Val){.ty=TY_NIL}) #define VAL_NUM(x) ((Val){.ty=TY_NUM, .as.d=(x) }) -- cgit v1.2.3