-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.c
84 lines (76 loc) · 2.25 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
#include <stdlib.h>
#include "chunk.h"
#include "memory.h"
void initChunk(Chunk* chunk) {
chunk->count = 0;
chunk->capacity = 0;
chunk->code = 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(int, chunk->lines, chunk->capacity);
freeValueArray(&chunk->constants);
initChunk(chunk);
}
void encodeLine(Chunk* chunk, int line) {
if (!chunk->lineCount ||
chunk->lines[chunk->lineCount - 1] != line) {
if (chunk->lineCapacity < chunk->lineCount + 1) {
int oldLineCapacity = chunk->lineCapacity;
chunk->lineCapacity = GROW_CAPACITY(oldLineCapacity);
chunk->lines = GROW_ARRAY(int, chunk->lines,
oldLineCapacity, chunk->lineCapacity);
}
chunk->lines[chunk->lineCount] = line;
chunk->lineCount++;
} else {
if (chunk->lineCount > 1 &&
chunk->lines[chunk->lineCount - 2] < 0) {
chunk->lines[chunk->lineCount - 2]--;
} else {
if (chunk->lineCapacity < chunk->lineCount + 1) {
int oldLineCapacity = chunk->lineCapacity;
chunk->lineCapacity = GROW_CAPACITY(oldLineCapacity);
chunk->lines = GROW_ARRAY(int, chunk->lines,
oldLineCapacity, chunk->lineCapacity);
}
chunk->lines[chunk->lineCount] = line;
chunk->lines[chunk->lineCount - 1] = -2;
chunk->lineCount++;
}
}
}
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;
encodeLine(chunk, line);
chunk->count++;
}
int addConstant(Chunk* chunk, Value value) {
writeValueArray(&chunk->constants, value);
return chunk->constants.count - 1;
}
void writeConstant(Chunk* chunk, Value value, int line) {
int constant = addConstant(chunk, value);
if (constant < 256) {
writeChunk(chunk, OP_CONSTANT, line);
writeChunk(chunk, constant, line);
}
else {
writeChunk(chunk, OP_CONSTANT_LONG, line);
writeChunk(chunk, constant & 255, line);
constant >>= 8;
writeChunk(chunk, constant & 255, line);
constant >>= 8;
writeChunk(chunk, constant & 255, line);
}
}