summaryrefslogtreecommitdiff
path: root/val.h
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-06-26 14:44:19 +0100
committerubq323 <ubq323@ubq323.website>2024-06-26 14:44:19 +0100
commita8519434f058d0ab60bf7f90acc61997cb982cfa (patch)
treee1858d42805e61afbd5c6c0b9d2f3b47bc8411ee /val.h
parent20584e520dfd44393aa66350b442e8a23e46c5a3 (diff)
add cfunc type and rudimentary stdlib
Diffstat (limited to 'val.h')
-rw-r--r--val.h7
1 files changed, 7 insertions, 0 deletions
diff --git a/val.h b/val.h
index 4ee76d2..90b60f2 100644
--- a/val.h
+++ b/val.h
@@ -7,13 +7,16 @@
typedef struct _val Val;
typedef struct _obj Obj;
+typedef struct _state State;
+typedef Val (*CFunc)(State *S, int nargs, Val *args);
typedef enum {
TY_NIL,
TY_NUM,
TY_BOOL,
TY_OBJ,
+ TY_CFUNC,
} ValTy;
typedef struct _val {
@@ -22,6 +25,7 @@ typedef struct _val {
double d;
bool b;
Obj *o;
+ CFunc f;
} as;
} Val;
@@ -71,6 +75,7 @@ ObjFunc *objfunc_new(State *S, uint8_t arity);
#define IS_NUM(x) (x.ty == TY_NUM)
#define IS_BOOL(x) (x.ty == TY_BOOL)
#define IS_OBJ(x) (x.ty == TY_OBJ)
+#define IS_CFUNC(x) (x.ty == TY_CFUNC)
#define IS_STRING(x) (is_obj_ty((x), OTY_STRING))
#define IS_FUNC(x) (is_obj_ty((x), OTY_FUNC))
@@ -78,6 +83,7 @@ ObjFunc *objfunc_new(State *S, uint8_t arity);
#define AS_NUM(x) (x.as.d)
#define AS_BOOL(x) (x.as.b)
#define AS_OBJ(x) (x.as.o)
+#define AS_CFUNC(x) (x.as.f)
#define AS_STRING(x) ((ObjString*)AS_OBJ(x))
#define AS_CSTRING(x) (AS_STRING(x)->d)
@@ -89,6 +95,7 @@ ObjFunc *objfunc_new(State *S, uint8_t arity);
#define VAL_TRUE VAL_BOOL(1)
#define VAL_FALSE VAL_BOOL(0)
#define VAL_OBJ(x) ((Val){.ty=TY_OBJ, .as.o=(Obj*)(x) })
+#define VAL_CFUNC(x) ((Val){.ty=TY_CFUNC, .as.f=(CFunc)(x)})
static inline bool is_obj_ty(Val v, ObjTy t) {
return IS_OBJ(v) && (AS_OBJ(v)->oty == t);