Skip to content

Commit 1976c05

Browse files
authored
Update value.h
1 parent 320b7e4 commit 1976c05

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

value.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
#include <stdbool.h>
55
#include <stddef.h>
6+
#include <stdarg.h>
7+
#include <string.h>
68

7-
#ifndef VALUE_TYPE_DEFINED
8-
#define VALUE_TYPE_DEFINED
99
typedef enum {
1010
TYPE_NIL,
1111
TYPE_BOOL,
@@ -14,7 +14,6 @@ typedef enum {
1414
TYPE_STRING,
1515
TYPE_OBJ
1616
} ValueType;
17-
#endif
1817

1918
typedef struct Object Object;
2019
typedef struct Value Value;
@@ -39,8 +38,10 @@ struct Value {
3938
#define NIL_VAL ((Value){ TYPE_NIL, { .int_val = 0 } })
4039
#define BOOL_VAL(v) ((Value){ TYPE_BOOL, { .bool_val = (v) } })
4140
#define INT_VAL(v) ((Value){ TYPE_INT, { .int_val = (v) } })
41+
#define FLOAT_VAL(v) ((Value){ TYPE_FLOAT, { .float_val = (v) } })
4242
#define NUMBER_VAL(v) ((Value){ TYPE_FLOAT, { .float_val = (v) } })
4343
#define STRING_VAL(s) ((Value){ TYPE_STRING, { .str_val = (s) } })
44+
#define OBJ_VAL(o) ((Value){ TYPE_OBJ, { .obj = (o) } })
4445

4546
#define IS_BOOL(v) ((v).type == TYPE_BOOL)
4647
#define IS_NIL(v) ((v).type == TYPE_NIL)
@@ -53,10 +54,13 @@ struct Value {
5354
#define AS_BOOL(v) ((v).data.bool_val)
5455
#define AS_INT(v) ((v).data.int_val)
5556
#define AS_FLOAT(v) ((v).data.float_val)
57+
#define AS_NUMBER(v) (IS_INT(v) ? (double)AS_INT(v) : AS_FLOAT(v))
5658
#define AS_STRING(v) ((v).data.str_val)
5759
#define AS_OBJ(v) ((v).data.obj)
60+
#define AS_OBJECT(v) ((v).data.obj)
5861

5962
void print_value(Value value);
6063
void* reallocate(void* pointer, size_t oldSize, size_t newSize);
64+
bool values_equal(Value a, Value b);
6165

6266
#endif

0 commit comments

Comments
 (0)