-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.c
53 lines (44 loc) · 1.59 KB
/
value.c
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
#include <stdio.h>
#include <string.h>
#include "memory.h"
#include "value.h"
void initValueArray(ValueArray* array) {
array->values = NULL;
array->capacity = 0;
array->count = 0;
}
void appendValueArray(ValueArray* array, Value value) {
int oldCapacity;
if (array->capacity < array->count + 1) {
oldCapacity = array->capacity;
array->capacity = GROW_CAPACITY(oldCapacity);
array->values = GROW_ARRAY(Value, array->values, oldCapacity, array->capacity);
}
array->values[array->count++] = value;
}
void freeValueArray(ValueArray* array) {
FREE_ARRAY(Value, array->values, array->capacity);
initValueArray(array);
}
void freezeValueArray(ValueArray* array) {
array->values = GROW_ARRAY(Value, array->values, array->capacity, array->count);
array->capacity = array->count;
}
void printValue(Value value, int flags) {
if (IS_BOOL(value)) putstr(AS_BOOL(value) ? "true" : "false");
else if (IS_NIL(value)) putstr("nil");
else if (IS_EMPTY(value)) putstr("<empty>");
else if (IS_INT(value)) printf("%d", AS_INT(value));
else printObject(value, flags);
}
const char* valueType(Value value) {
if (IS_BOOL(value)) return "bool";
else if (IS_NIL(value)) return "nil";
else if (IS_EMPTY(value)) return "empty"; // internal
else if (IS_INT(value)) return "int";
else return typeName(OBJ_TYPE(value));
}
uint32_t hashValue(Value value) {
if (IS_STRING(value)) return AS_STRING(value)->hash;
else return value;
}