-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.c
172 lines (155 loc) · 3.72 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
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
#include "value.h"
#include "chunk.h"
#include "memory.h"
#include "table.h"
#include "vm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void initValueArray(ValueArray *arr) {
arr->count = 0;
arr->capacity = 0;
arr->values = NULL;
}
void writeValueArray(ValueArray *arr, Value val) {
if (arr->capacity < arr->count + 1) {
uint32_t oldCapacity = arr->capacity;
arr->capacity = GROW_CAPACITY(arr->capacity);
arr->values = GROW_ARRAY(Value, arr->values, oldCapacity, arr->capacity);
}
arr->values[arr->count] = val;
arr->count++;
}
void freeObject(Obj *obj) {
ObjType type = obj->type;
switch (type) {
case OBJ_STRING: {
ObjString *ptr = (ObjString *)obj;
free((void *)ptr->string);
free((void *)ptr);
break;
}
case OBJ_FUNCTION: {
ObjFunc *ptr = (ObjFunc *)obj;
freeChunk(ptr->chunk);
free((void *)ptr);
break;
}
case OBJ_STRUCT: {
ObjStruct *ptr = (ObjStruct *)obj;
freeTable(&(ptr->table));
freeObject((Obj *)ptr->type);
free((void *)ptr);
}
default:
break;
}
}
void freeValueArray(ValueArray *arr) {
FREE_ARRAY(Value, arr->values, arr->count);
initValueArray(arr);
}
void printValue(Value val) {
switch (val.type) {
case VALUE_BOOL: {
printf("%s", val.as.boolean ? "true" : "false");
break;
}
case VALUE_NIL: {
printf("nil");
break;
}
case VALUE_NUM: {
printf("%d", val.as.number);
break;
}
case VALUE_OBJ:
switch (val.as.obj->type) {
case OBJ_STRING:
printf("%s", ((ObjString *)val.as.obj)->string);
break;
case OBJ_STRUCT:
printf("`%s`", ((ObjStruct *)val.as.obj)->type->string);
break;
case OBJ_FUNCTION:
printf("function: %d params", ((ObjFunc *)val.as.obj)->numParams);
break;
default:
break;
}
default:
break;
}
}
bool isObjectOfType(Value val, ObjType type) {
return IS_OBJ(val) && val.as.obj->type == type;
}
uint32_t hash(const char *string, int length) {
uint32_t hash = 2166136261u;
for (int i = 0; i < length; i++) {
hash ^= (uint32_t)string[i];
hash *= 16777619;
}
return hash;
}
// Checks if string exists in intern table, otherwise creates string in heap,
// creates objstring, and adds it to table
ObjString *copyString(const char *string, int length) {
uint32_t hashVal = hash(string, length);
ObjString *intern = findStringInTable(&vm.strings, string, length, hashVal);
if (intern != NULL) {
return intern;
}
char *heapPtr = (char *)malloc(sizeof(char) * (length + 1));
if (heapPtr == NULL) {
exit(1);
}
memcpy(heapPtr, string, length);
heapPtr[length] = '\0';
ObjString *heapObj = (ObjString *)malloc(sizeof(ObjString));
if (heapObj == NULL) {
exit(1);
}
((Obj *)heapObj)->type = OBJ_STRING;
((Obj *)heapObj)->next = vm.objects;
vm.objects = &heapObj->obj;
heapObj->length = length;
heapObj->string = heapPtr;
heapObj->hash = hashVal;
set(&vm.strings, heapObj, MAKE_NIL());
return heapObj;
}
// Creates a ObjFunc on the heap
ObjFunc *createFunc(Chunk *chunk, int numParams) {
ObjFunc *output = (ObjFunc *)malloc(sizeof(ObjFunc));
((Obj *)output)->type = OBJ_FUNCTION;
((Obj *)output)->next = vm.objects;
vm.objects = &output->obj;
output->chunk = chunk;
output->numParams = numParams;
return output;
}
char *typeName(Value val) {
switch (val.type) {
case VALUE_BOOL:
return "Boolean";
case VALUE_NIL:
return "Nil";
case VALUE_NUM:
return "Number";
case VALUE_OBJ: {
switch (val.as.obj->type) {
case OBJ_STRING:
return "String";
case OBJ_FUNCTION:
return "Function Object";
case OBJ_STRUCT:
return "Struct";
default:
return "Unknown Object";
}
}
default:
return "Unknown";
}
}