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