aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/value.h
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-08-25 01:08:14 -0700
committerGravatar Tom Willemse2021-08-25 01:08:14 -0700
commit6508cfd8fe55e03a587b2f72d3ffc901d7ada049 (patch)
treeb5856f9802a5f1c60024cdc158ee2832179cfc5f /clox/src/value.h
parent3a02da216122c890c1218fe0c5488bc477e89031 (diff)
downloadcrafting-interpreters-6508cfd8fe55e03a587b2f72d3ffc901d7ada049.tar.gz
crafting-interpreters-6508cfd8fe55e03a587b2f72d3ffc901d7ada049.zip
Chapter 18.1 - 18.3
Diffstat (limited to 'clox/src/value.h')
-rw-r--r--clox/src/value.h33
1 files changed, 28 insertions, 5 deletions
diff --git a/clox/src/value.h b/clox/src/value.h
index 86d4b1c..7478aaf 100644
--- a/clox/src/value.h
+++ b/clox/src/value.h
@@ -3,17 +3,40 @@
#include "common.h"
-typedef double Value;
+typedef enum {
+ VAL_BOOL,
+ VAL_NIL,
+ VAL_NUMBER,
+} ValueType;
+
+typedef struct {
+ ValueType type;
+ union {
+ bool boolean;
+ double number;
+ } as;
+} Value;
+
+#define IS_BOOL(value) ((value).type == VAL_BOOL)
+#define IS_NIL(value) ((value).type == VAL_NIL)
+#define IS_NUMBER(value) ((value).type == VAL_NUMBER)
+
+#define AS_BOOL(value) ((value).as.boolean)
+#define AS_NUMBER(value) ((value).as.number)
+
+#define BOOL_VAL(value) ((Value){VAL_BOOL, {.boolean = value}})
+#define NIL_VAL ((Value){VAL_NIL, {.number = 0}})
+#define NUMBER_VAL(value) ((Value){VAL_NUMBER, {.number = value}})
typedef struct {
int capacity;
int count;
- Value* values;
+ Value *values;
} ValueArray;
-void initValueArray(ValueArray* array);
-void writeValueArray(ValueArray* array, Value value);
-void freeValueArray(ValueArray* array);
+void initValueArray(ValueArray *array);
+void writeValueArray(ValueArray *array, Value value);
+void freeValueArray(ValueArray *array);
void printValue(Value value);
#endif