|
@@ -9,17 +9,17 @@ static int constantInstruction(const char *, Chunk *, int);
|
|
|
static int simpleInstruction(const char *name, int offset);
|
|
static int simpleInstruction(const char *name, int offset);
|
|
|
|
|
|
|
|
void disassembleChunk(Chunk *chunk, const char *name) {
|
|
void disassembleChunk(Chunk *chunk, const char *name) {
|
|
|
- printf("== %s ==\n", name);
|
|
|
|
|
|
|
+ printf("== %s STARTING ==\n", name);
|
|
|
|
|
|
|
|
for (int offset = 0; offset < chunk->count;) {
|
|
for (int offset = 0; offset < chunk->count;) {
|
|
|
offset = disassembleInstruction(chunk, offset);
|
|
offset = disassembleInstruction(chunk, offset);
|
|
|
}
|
|
}
|
|
|
|
|
+ printf("== %s END ==\n", name);
|
|
|
}
|
|
}
|
|
|
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]) {
|
|
|
|
|
|
|
+ if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
|
|
|
printf(" | ");
|
|
printf(" | ");
|
|
|
} else {
|
|
} else {
|
|
|
printf("%4d ", chunk->lines[offset]);
|
|
printf("%4d ", chunk->lines[offset]);
|
|
@@ -27,8 +27,13 @@ int disassembleInstruction(Chunk *chunk, int offset) {
|
|
|
|
|
|
|
|
uint8_t instruction = chunk->code[offset];
|
|
uint8_t instruction = chunk->code[offset];
|
|
|
switch (instruction) {
|
|
switch (instruction) {
|
|
|
- case OP_CONSTANT:return constantInstruction("OP_CONSTANT", chunk, offset);
|
|
|
|
|
- case OP_RETURN:return simpleInstruction("OP_RETURN", offset);
|
|
|
|
|
|
|
+ case OP_CONSTANT: return constantInstruction("OP_CONSTANT", chunk, offset);
|
|
|
|
|
+ case OP_ADD: return simpleInstruction("OP_ADD", offset);
|
|
|
|
|
+ case OP_SUBTRACT: return simpleInstruction("OP_SUBTRACT", offset);
|
|
|
|
|
+ case OP_MULTIPLY: return simpleInstruction("OP_MULTIPLY", offset);
|
|
|
|
|
+ case OP_DIVIDE: return simpleInstruction("OP_DIVIDE", offset);
|
|
|
|
|
+ case OP_NEGATE: return simpleInstruction("OP_NEGATE", offset);
|
|
|
|
|
+ case OP_RETURN: return simpleInstruction("OP_RETURN", offset);
|
|
|
default:printf("Unknown opcode %d\n", instruction);
|
|
default:printf("Unknown opcode %d\n", instruction);
|
|
|
return offset + 1;
|
|
return offset + 1;
|
|
|
}
|
|
}
|