Skip to content

Commit 09050a9

Browse files
authored
...
1 parent 53a5e3d commit 09050a9

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

value.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef VALUE_H
2+
#define VALUE_H
3+
4+
#include <stdbool.h>
5+
6+
typedef enum {
7+
TYPE_NIL,
8+
TYPE_BOOL,
9+
TYPE_INT,
10+
TYPE_FLOAT,
11+
TYPE_STRING
12+
} ValueType;
13+
14+
typedef struct {
15+
ValueType type;
16+
union {
17+
bool bool_val;
18+
int int_val;
19+
double float_val;
20+
const char* str_val;
21+
} data;
22+
} Value;
23+
24+
#define NIL_VAL ((Value){ TYPE_NIL, { .int_val = 0 } })
25+
#define BOOL_VAL(v) ((Value){ TYPE_BOOL, { .bool_val = v } })
26+
#define INT_VAL(v) ((Value){ TYPE_INT, { .int_val = v } })
27+
#define NUMBER_VAL(v) ((Value){ TYPE_FLOAT, { .float_val = v } })
28+
#define STRING_VAL(s) ((Value){ TYPE_STRING, { .str_val = s } })
29+
30+
#define IS_BOOL(v) ((v).type == TYPE_BOOL)
31+
#define IS_NIL(v) ((v).type == TYPE_NIL)
32+
#define IS_INT(v) ((v).type == TYPE_INT)
33+
#define IS_FLOAT(v) ((v).type == TYPE_FLOAT)
34+
#define IS_STRING(v) ((v).type == TYPE_STRING)
35+
#define IS_NUMBER(v) (IS_INT(v) || IS_FLOAT(v))
36+
37+
#define AS_BOOL(v) ((v).data.bool_val)
38+
#define AS_INT(v) ((v).data.int_val)
39+
#define AS_FLOAT(v) ((v).data.float_val)
40+
#define AS_STRING(v) ((v).data.str_val)
41+
42+
void print_value(Value value);
43+
44+
#endif

0 commit comments

Comments
 (0)