-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalue.h
219 lines (179 loc) · 6.56 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef clox_value_h
#define clox_value_h
#include <stdio.h> // for FILE struct
#include "common.h"
#include "vec.h"
#ifdef __cplusplus
extern "C" {
#endif
// fwd decls
struct Obj;
struct ObjString;
#ifdef NAN_TAGGING
typedef uint64_t Value;
// A mask that selects the sign bit.
#define SIGN_BIT ((uint64_t)1 << 63)
// The bits that must be set to indicate a quiet NaN.
#define QNAN ((uint64_t)0x7ffc000000000000)
// If the NaN bits are set, it's not a number.
#define IS_BOOL(value) ((value) == TRUE_VAL || (value) == FALSE_VAL)
#define IS_NIL(value) ((value) == NIL_VAL)
#define IS_NUMBER(value) (((value) & QNAN) != QNAN)
#define IS_OBJ(value) (((value) & (QNAN | SIGN_BIT)) == (QNAN | SIGN_BIT))
#define IS_UNDEF(value) ((value) == UNDEF_VAL)
// Masks out the tag bits used to identify the singleton value.
#define MASK_TAG (7)
// Tag values for the different singleton values.
#define TAG_NAN (0)
#define TAG_NIL (1)
#define TAG_FALSE (2)
#define TAG_TRUE (3)
#define TAG_UNDEFINED (4)
#define TAG_UNUSED2 (5)
#define TAG_UNUSED3 (6)
#define TAG_UNUSED4 (7)
// Value -> 0 or 1.
#define AS_BOOL(value) ((value) == TRUE_VAL)
// Value -> Obj*.
#define AS_OBJ(value) ((struct Obj*)(uintptr_t)((value) & ~(SIGN_BIT | QNAN)))
#define AS_NUMBER(value) (valueToNumber(value))
// Singleton values.
#define BOOL_VAL(b) ((b) ? TRUE_VAL : FALSE_VAL)
#define TRUE_VAL ((Value)(uint64_t)(QNAN | TAG_TRUE))
#define NIL_VAL ((Value)(uint64_t)(QNAN | TAG_NIL))
#define FALSE_VAL ((Value)(uint64_t)(QNAN | TAG_FALSE))
#define UNDEF_VAL ((Value)(uint64_t)(QNAN | TAG_UNDEFINED))
#define NUMBER_VAL(n) (numberToValue((double)(n)))
#define OBJ_VAL(obj) (objectToValue((struct Obj*)(obj)))
// Gets the singleton type tag for a Value (which must be a singleton).
#define GET_TAG(value) ((int)((value) & MASK_TAG))
#else
typedef enum {
VAL_T_TRUE = 1,
VAL_T_FALSE,
VAL_T_NIL,
VAL_T_NUMBER,
VAL_T_OBJ, // includes strings/arrays
VAL_T_UNDEF // used as an 'undefined' value type, for example as an undefined hash key
} ValueType;
typedef struct Value {
ValueType type;
union {
double number;
struct Obj *object;
} as;
} Value;
#define IS_BOOL(value) ((value).type == VAL_T_TRUE || (value).type == VAL_T_FALSE)
#define IS_NIL(value) ((value).type == VAL_T_NIL)
#define IS_NUMBER(value) ((value).type == VAL_T_NUMBER)
#define IS_OBJ(value) ((value).type == VAL_T_OBJ)
#define IS_UNDEF(value) ((value).type == VAL_T_UNDEF)
#define AS_OBJ(value) ((value).as.object)
#define AS_BOOL(value) ((value).as.number == 0 ? false : true)
#define AS_NUMBER(value) ((value).as.number)
#define BOOL_VAL(b) (b ? TRUE_VAL : FALSE_VAL)
#define TRUE_VAL ((Value){ VAL_T_TRUE, { .number = 1 } })
#define FALSE_VAL ((Value){ VAL_T_FALSE, { .number = 0 } })
#define NIL_VAL ((Value){ VAL_T_NIL, { .number = 0 } } )
#define UNDEF_VAL ((Value){ VAL_T_UNDEF, { .number = -1 } })
#define NUMBER_VAL(n) ((Value){ VAL_T_NUMBER, { .number = n } })
#define OBJ_VAL(obj) ((Value){ VAL_T_OBJ, { .object = (struct Obj*)obj } })
#endif
typedef vec_t(Value) vec_val_t;
typedef struct ValueArray {
int capacity;
int count;
Value *values;
} ValueArray;
#ifdef NAN_TAGGING
#define VALARRAY_FOREACH(ary, val, idx) \
for (idx = 0; idx < (ary)->count && ((val = (ary)->values[idx]) || 1) && !IS_UNDEF(val); idx++)
#define VALARRAY_FOREACH_START(ary, val, startIdx, idx) \
for (idx = startIdx; idx < (ary)->count && ((val = (ary)->values[idx]) || 1) && !IS_UNDEF(val); idx++)
#define VALARRAY_FOREACH_REVERSE(ary, val, idx) \
for (idx = ((ary)->count-1); idx >= 0 && ((val = (ary)->values[idx]) || 1) && !IS_UNDEF(val); idx--)
#else
#define VALARRAY_FOREACH(ary, val, idx) \
for (idx = 0; idx < (ary)->count && (val = (ary)->values[idx]).type != VAL_T_UNDEF; idx++)
#define VALARRAY_FOREACH_START(ary, val, startIdx, idx) \
for (idx = startIdx; idx < (ary)->count && (val = (ary)->values[idx]).type != VAL_T_UNDEF; idx++)
#define VALARRAY_FOREACH_REVERSE(ary, val, idx) \
for (idx = ((ary)->count-1); idx >= 0 && (val = (ary)->values[idx]).type != VAL_T_UNDEF; idx--)
#endif
#define IS_BOOL_FUNC (is_bool_p)
#define IS_NIL_FUNC (is_nil_p)
#define IS_NUMBER_FUNC (is_number_p)
#define IS_OBJ_FUNC (is_obj_p)
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
// A union to let us reinterpret a double as raw bits and back.
typedef union
{
uint64_t bits64;
uint32_t bits32[2];
double num;
} DoubleBits;
static inline double valueToNumber(Value val) {
#if NAN_TAGGING
DoubleBits data;
data.bits64 = val;
return data.num;
#else
return val.as.number;
#endif
}
static inline Value objectToValue(struct Obj *obj) {
#if NAN_TAGGING
// The triple casting is necessary here to satisfy some compilers:
// 1. (uintptr_t) Convert the pointer to a number of the right size.
// 2. (uint64_t) Pad it up to 64 bits in 32-bit builds.
// 3. Or in the bits to make a tagged Nan.
// 4. Cast to a typedef'd value.
return (Value)(SIGN_BIT | QNAN | (uint64_t)(uintptr_t)(obj));
#else
Value value;
value.type = VAL_T_OBJ;
value.as.object = obj;
return value;
#endif
}
static inline Value numberToValue(double num) {
#if NAN_TAGGING
DoubleBits data;
data.num = num;
return data.bits64;
#else
Value value;
value.type = VAL_T_NUMBER;
value.as.number = num;
return value;
#endif
}
void initValueArray(ValueArray *array);
void initValueArrayWithCapa(ValueArray *array, int capa);
void writeValueArrayEnd(ValueArray *array, Value value);
void writeValueArrayBeg(ValueArray *array, Value value);
void writeValueArrayBulk(ValueArray *array, size_t offset, size_t num, Value value);
void freeValueArray(ValueArray *array);
bool removeValueArray(ValueArray *array, int idx);
int printValue(FILE *file, Value value, bool canCallMethods, int maxLen);
void printInspectValue(FILE *file, Value value);
// value type predicate function
typedef bool (*value_type_p)(Value val);
bool is_bool_p(Value);
bool is_nil_p(Value);
bool is_number_p(Value);
bool is_obj_p(Value);
typedef struct ObjString *(*newStringFunc)(char *chars, size_t length, int flags);
struct ObjString *valueToString(Value value, newStringFunc fn, int flags);
struct ObjString *valueToInspectString(Value value, newStringFunc fn, int flags);
struct ObjString *inspectString(Value value);
const char *typeOfVal(Value val);
uint32_t valHash(Value val);
bool valEqual(Value a, Value b);
bool isTruthy(Value a);
void fillCallableName(Value callable, char buf[], size_t buflen);
struct ObjString *getCallableFunctionName(Value callable);
#ifdef __cplusplus
}
#endif
#endif