summaryrefslogtreecommitdiff
path: root/val.h
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-06-26 19:27:42 +0100
committerubq323 <ubq323@ubq323.website>2024-06-26 19:27:42 +0100
commit2e62b41072738142dea9f0b5dd5d2d22455c7616 (patch)
treef47ce547de39fb69b02eb70990c9485489f1e574 /val.h
parent6deeb9630d4b4e7d672ab851dcd4fe3d0d3d2865 (diff)
add arrays, appending, getting length, indexing
Diffstat (limited to 'val.h')
-rw-r--r--val.h27
1 files changed, 21 insertions, 6 deletions
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) })