add line numbers
This commit is contained in:
parent
a2e5d43e1e
commit
72c5830637
|
@ -6,23 +6,28 @@ void newChunk(Chunk* chunk) {
|
||||||
chunk->count = 0;
|
chunk->count = 0;
|
||||||
chunk->capacity = 0;
|
chunk->capacity = 0;
|
||||||
chunk->code = NULL;
|
chunk->code = NULL;
|
||||||
|
chunk->lines = NULL;
|
||||||
newValueArray(&chunk->constants);
|
newValueArray(&chunk->constants);
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeChunk(Chunk *chunk, uint8_t byte) {
|
void writeChunk(Chunk *chunk, uint8_t byte, int line) {
|
||||||
if (chunk->capacity < chunk->count + 1) {
|
if (chunk->capacity < chunk->count + 1) {
|
||||||
int oldCapacity = chunk->capacity;
|
int oldCapacity = chunk->capacity;
|
||||||
chunk->capacity = GROW_CAPACITY(oldCapacity);
|
chunk->capacity = GROW_CAPACITY(oldCapacity);
|
||||||
chunk->code = GROW_ARRAY(uint8_t, chunk->code,
|
chunk->code = GROW_ARRAY(uint8_t, chunk->code,
|
||||||
oldCapacity, chunk->capacity);
|
oldCapacity, chunk->capacity);
|
||||||
|
chunk->lines = GROW_ARRAY(int, chunk->lines,
|
||||||
|
oldCapacity, chunk->capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
chunk->code[chunk->count] = byte;
|
chunk->code[chunk->count] = byte;
|
||||||
|
chunk->lines[chunk->count] = line;
|
||||||
chunk->count++;
|
chunk->count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void freeChunk(Chunk *chunk) {
|
void freeChunk(Chunk *chunk) {
|
||||||
FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
|
FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
|
||||||
|
FREE_ARRAY(int, chunk->lines, chunk->capacity);
|
||||||
freeValueArray(&chunk->constants);
|
freeValueArray(&chunk->constants);
|
||||||
newChunk(chunk);
|
newChunk(chunk);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,12 +15,13 @@ typedef struct Chunk {
|
||||||
int count;
|
int count;
|
||||||
int capacity;
|
int capacity;
|
||||||
uint8_t *code;
|
uint8_t *code;
|
||||||
|
int *lines;
|
||||||
ValueArray constants;
|
ValueArray constants;
|
||||||
} Chunk;
|
} Chunk;
|
||||||
|
|
||||||
void newChunk(Chunk *chunk);
|
void newChunk(Chunk *chunk);
|
||||||
void freeChunk(Chunk *chunk);
|
void freeChunk(Chunk *chunk);
|
||||||
void writeChunk(Chunk *chunk, uint8_t byte);
|
void writeChunk(Chunk *chunk, uint8_t byte, int line);
|
||||||
int addConstant(Chunk *chunk, Value value);
|
int addConstant(Chunk *chunk, Value value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -27,6 +27,12 @@ static int simpleInstruction(const char* name, int offset) {
|
||||||
|
|
||||||
int disassembleInstruction(Chunk* chunk, int offset) {
|
int disassembleInstruction(Chunk* chunk, int offset) {
|
||||||
printf("%04d ", offset);
|
printf("%04d ", offset);
|
||||||
|
if (offset > 0 &&
|
||||||
|
chunk->lines[offset] == chunk->lines[offset - 1]) {
|
||||||
|
printf(" | ");
|
||||||
|
} else {
|
||||||
|
printf("%4d ", chunk->lines[offset]);
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t instruction = chunk->code[offset];
|
uint8_t instruction = chunk->code[offset];
|
||||||
switch (instruction) {
|
switch (instruction) {
|
||||||
|
|
|
@ -7,10 +7,10 @@ int main(int argc, const char** argv) {
|
||||||
newChunk(&chunk);
|
newChunk(&chunk);
|
||||||
|
|
||||||
int constant = addConstant(&chunk, 1.2);
|
int constant = addConstant(&chunk, 1.2);
|
||||||
writeChunk(&chunk, OP_CONSTANT);
|
writeChunk(&chunk, OP_CONSTANT, 1);
|
||||||
writeChunk(&chunk, constant);
|
writeChunk(&chunk, constant, 1);
|
||||||
|
|
||||||
writeChunk(&chunk, OP_RETURN);
|
writeChunk(&chunk, OP_RETURN, 1);
|
||||||
|
|
||||||
disassembleChunk(&chunk, "test chunk");
|
disassembleChunk(&chunk, "test chunk");
|
||||||
freeChunk(&chunk);
|
freeChunk(&chunk);
|
||||||
|
|
Loading…
Reference in New Issue