-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.c
107 lines (89 loc) · 2.8 KB
/
chunk.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
#include <stdlib.h>
#include <stdio.h>
#include "chunk.h"
#include "memory.h"
#include "vm.h"
void initChunk(Chunk* chunk) {
chunk->count = 0;
chunk->capacity = 0;
chunk->code = NULL;
chunk->lines = NULL;
chunk->lineCount = 0;
chunk->lineCapacity = 0;
chunk->lines = NULL;
initValueArray(&chunk->constants);
}
void freeChunk(Chunk* chunk) {
FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
FREE_ARRAY(LineStart, chunk->lines, chunk->lineCapacity);
freeValueArray(&chunk->constants);
initChunk(chunk);
}
void writeChunk(Chunk* chunk, uint8_t byte, int line) {
if (chunk->capacity < chunk->count + 1) {
int oldCapacity = chunk->capacity;
chunk->capacity = GROW_CAPACITY(oldCapacity);
chunk->code = GROW_ARRAY(uint8_t, chunk->code, oldCapacity, chunk->capacity);
}
chunk->code[chunk->count] = byte;
chunk->count++;
// check if on the same line
if (chunk->lineCount > 0 && chunk->lines[chunk->lineCount - 1].line == line) {
return;
}
if (chunk->lineCapacity < chunk->lineCount + 1) {
int oldCapacity = chunk->lineCapacity;
chunk->lineCapacity = GROW_CAPACITY(oldCapacity);
chunk->lines = GROW_ARRAY(LineStart, chunk->lines, oldCapacity, chunk->lineCapacity);
}
LineStart* lineStart = &chunk->lines[chunk->lineCount++];
lineStart->offset = chunk->count - 1;
lineStart->line = line;
}
// returns the index where value was appended.
int addConstant(Chunk* chunk, Value value) {
push(value);
writeValueArray(&chunk->constants, value);
return chunk->constants.count - 1;
}
int getLine(Chunk* chunk, int instruction) {
if (chunk->lineCount == 1) {
return chunk->lines[chunk->lineCount - 1].line;
}
for (int i = 1; i < chunk->lineCount; i++) {
LineStart* line = &chunk->lines[i];
if (instruction < line->offset) {
return chunk->lines[i - 1].line;
}
}
return chunk->lines[chunk->lineCount - 1].line;
}
// his better get line function.
// int getLine(Chunk* chunk, int instruction) {
// int start = 0;
// int end = chunk->lineCount - 1;
// while (true) {
// int mid = (start + end) / 2;
// LineStart* line = &chunk->lines[mid];
// if (instruction < line->offset) {
// end = mid - 1;
// } else if (mid == chunk->lineCount - 1 || instruction < chunk->lines[mid+1].offset) {
// return line->line;
// } else {
// start = mid + 1;
// }
// }
// }
void writeConstant(Chunk* chunk, Value value, int line) {
int index = addConstant(chunk, value);
if (index < 256) {
writeChunk(chunk, OP_CONSTANT, line);
writeChunk(chunk, (uint8_t) index, line);
return;
}
writeChunk(chunk, OP_CONSTANT_LONG, line);
// it's 3 bytes for 24-bit addressing, thus 3 iterations.
for (int i = 0; i < 3; i++) {
writeChunk(chunk, (uint8_t) ((index >> (i * 8)) & 0xff), line);
}
}