debug.c 706 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // Created by 李晓明 on 2023/8/16.
  3. //
  4. #include "debug.h"
  5. #include <stdio.h>
  6. static int simpleInstruction(const char *name, int offset) {
  7. printf("%s\n", name);
  8. return offset + 1;
  9. }
  10. void disassembleChunk(Chunk *chunk, const char *name) {
  11. printf("== %s ==\n", name);
  12. for (int offset = 0; offset < chunk->count;) {
  13. offset = disassembleInstruction(chunk, offset);
  14. }
  15. }
  16. int disassembleInstruction(Chunk *chunk, int offset) {
  17. printf("%04d ", offset);
  18. uint8_t instruction = chunk->code[offset];
  19. switch (instruction) {
  20. case OP_RETURN:
  21. return simpleInstruction("OP_RETURN", offset);
  22. default:
  23. printf("Unknown opcode %d\n", instruction);
  24. return offset + 1;
  25. }
  26. }