-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.h
56 lines (44 loc) · 1.29 KB
/
value.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Created by Fabian Simon on 29.09.23.
//
#ifndef FAVE_CUH_VALUE_H
#define FAVE_CUH_VALUE_H
#include "common.h"
typedef struct Obj Obj;
typedef struct ObjString ObjString;
typedef enum {
VAL_BOOL,
VAL_NIL,
VAL_NUMBER,
VAL_OBJ,
} ValueType;
typedef struct {
ValueType type;
union {
bool boolean;
double number;
Obj* obj;
} as;
} Value;
#define IS_BOOL(val) ((val).type == VAL_BOOL)
#define IS_NIL(val) ((val).type == VAL_NIL)
#define IS_NUMBER(val) ((val).type == VAL_NUMBER)
#define IS_OBJ(val) ((val).type == VAL_OBJ)
#define AS_BOOL(val) ((val).as.boolean)
#define AS_NUMBER(val) ((val).as.number)
#define AS_OBJ(val) ((val).as.obj)
#define BOOL_VAL(val) ((Value){VAL_BOOL, {.boolean = val}})
#define NIL_VAL ((Value){VAL_NIL, {.number = 0}})
#define NUMBER_VAL(val) ((Value){VAL_NUMBER, {.number = val}})
#define OBJ_VAL(object) ((Value){VAL_OBJ, {.obj = (Obj*)object}})
typedef struct {
int capacity;
int count;
Value* values;
} ValueArray;
bool values_equal(Value a, Value b);
void init_value_array(ValueArray* arr);
void write_value_array(ValueArray* arr, Value val);
void free_value_array(ValueArray* arr);
void print_value(Value value);
#endif //FAVE_CUH_VALUE_H