2021-07-08 11:24:24 +02:00
|
|
|
#include <stdio.h>
|
2021-09-08 07:54:12 +02:00
|
|
|
#include <string.h>
|
2021-07-08 11:24:24 +02:00
|
|
|
|
|
|
|
#include "memory.h"
|
2021-09-08 07:54:12 +02:00
|
|
|
#include "object.h"
|
2021-07-08 11:24:24 +02:00
|
|
|
#include "value.h"
|
|
|
|
|
2021-08-25 10:08:14 +02:00
|
|
|
void initValueArray(ValueArray *array) {
|
2021-07-08 11:24:24 +02:00
|
|
|
array->values = NULL;
|
|
|
|
array->capacity = 0;
|
|
|
|
array->count = 0;
|
|
|
|
}
|
|
|
|
|
2021-08-25 10:08:14 +02:00
|
|
|
void writeValueArray(ValueArray *array, Value value) {
|
2021-07-08 11:24:24 +02:00
|
|
|
if (array->capacity < array->count + 1) {
|
|
|
|
int oldCapacity = array->capacity;
|
|
|
|
array->capacity = GROW_CAPACITY(oldCapacity);
|
2021-08-25 10:08:14 +02:00
|
|
|
array->values =
|
|
|
|
GROW_ARRAY(Value, array->values, oldCapacity, array->capacity);
|
2021-07-08 11:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
array->values[array->count] = value;
|
|
|
|
array->count++;
|
|
|
|
}
|
|
|
|
|
2021-08-25 10:08:14 +02:00
|
|
|
void freeValueArray(ValueArray *array) {
|
2021-07-08 11:24:24 +02:00
|
|
|
FREE_ARRAY(Value, array->values, array->capacity);
|
|
|
|
initValueArray(array);
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:03:31 +02:00
|
|
|
void printValue(Value value) {
|
|
|
|
switch (value.type) {
|
|
|
|
case VAL_BOOL:
|
|
|
|
printf(AS_BOOL(value) ? "true" : "false");
|
|
|
|
break;
|
|
|
|
case VAL_NIL:
|
|
|
|
printf("nil");
|
|
|
|
break;
|
|
|
|
case VAL_NUMBER:
|
|
|
|
printf("%g", AS_NUMBER(value));
|
|
|
|
break;
|
2021-09-08 07:54:12 +02:00
|
|
|
case VAL_OBJ:
|
|
|
|
printObject(value);
|
|
|
|
break;
|
2021-09-06 11:03:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool valuesEqual(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);
|
2021-09-08 07:54:12 +02:00
|
|
|
case VAL_OBJ: {
|
2021-09-18 20:10:11 +02:00
|
|
|
return AS_OBJ(a) == AS_OBJ(b);
|
2021-09-08 07:54:12 +02:00
|
|
|
}
|
2021-09-06 11:03:31 +02:00
|
|
|
default:
|
|
|
|
return false; /* Unreachable */
|
|
|
|
}
|
|
|
|
}
|