-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.c
54 lines (46 loc) · 1.38 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
53
54
//
// Created by Fabian Simon on 29.09.23.
//
#include <stdio.h>
#include <string.h>
#include "value.h"
#include "object.h"
#include "memory.h"
void init_value_array(ValueArray* arr) {
arr->values = NULL;
arr->capacity = 0;
arr->count = 0;
}
void write_value_array(ValueArray* arr, Value val) {
if (arr->capacity < arr->count+1) {
int old_capacity = arr->capacity;
arr->capacity = GROW_CAPACITY(old_capacity);
arr->values = GROW_ARRAY(Value, arr->values, old_capacity, arr->capacity);
}
arr->values[arr->count] = val;
arr->count++;
}
void free_value_array(ValueArray* arr) {
FREE_ARRAY(Value, arr->values, arr->capacity);
init_value_array(arr);
}
void print_value(Value val) {
switch (val.type) {
case VAL_BOOL:
printf(AS_BOOL(val) ? "true" : "false");
break;
case VAL_NIL: printf("NIL"); break;
case VAL_NUMBER: printf("%g", AS_NUMBER(val)); break;
case VAL_OBJ: print_object(val); break;
}
}
bool values_equal(Value a, Value b) {
if (a.type != b.type) return false;
switch (a.type) {
case VAL_BOOL: return AS_BOOL(a) == AS_BOOL(b);
case VAL_NIL: return true;
case VAL_NUMBER: return AS_NUMBER(a) == AS_NUMBER(b);
case VAL_OBJ: return AS_OBJ(a) == AS_OBJ(b);
default: return false; // unreachable.
}
}