-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.c
50 lines (44 loc) · 1.32 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
#include "value.h"
#include "memory.h"
#include "strings.h"
#include <stddef.h>
#include <stdio.h>
void init_value_array(ValueArray *array) {
array->values = NULL;
array->capacity = 0;
array->count = 0;
}
void write_value_array(ValueArray *array, Value value) {
if (array->count + 1 >= array->capacity) {
int old_capacity = array->capacity;
array->capacity = GROW_ARRAY_CAPACITY(array->capacity);
array->values = GROW_ARRAY(array->values, Value, old_capacity, array->capacity);
}
array->values[(array->count)++] = value;
}
void free_value_array(ValueArray *array) {
FREE_ARRAY(array->values, Value, array->capacity);
init_value_array(array);
}
void print_value(Value value) {
switch (value.type) {
case TYPE_BOOL:
printf(value.as.integer == 0 ? "false" : "true");
break;
case TYPE_DECIMAL:
printf("%.15g", value.as.decimal);
break;
case TYPE_INTEGER:
printf("%ld", value.as.integer);
break;
case TYPE_NIL:
printf("nil");
break;
case TYPE_OBJECT:
if (IS_STRING(value))
printf("%s", ((ObjString *) value.as.object)->chars);
else
printf("%s", type_to_string(value));
break;
}
}