#include "dis.h" #include #include void disasm_chunk(Chunk *ch) { for (size_t ip = 0; ip < ch->bc.len; ) { uint8_t instr = ch->bc.d[ip]; printf("%04zd\t",ip); ip ++; #define SIMPLE_INSTR(opcode, str) \ case opcode: puts(str); break; switch (instr) { SIMPLE_INSTR(OP_RET, "ret") case OP_LOADK: { uint8_t ix = ch->bc.d[ip++]; printf("loadk #%d\t; ",ix); Val k = ch->consts.d[ix]; printf("%-4s : ",typename_str(k)); println_val(k); break; } case OP_GETGLOBAL: { uint8_t ix = ch->bc.d[ip++]; printf("getglobal #%d\t; ",ix); Val k = ch->consts.d[ix]; printf("%-4s : ",typename_str(k)); println_val(k); break; } case OP_SETGLOBAL: { uint8_t ix = ch->bc.d[ip++]; printf("setglobal #%d\t; ",ix); Val k = ch->consts.d[ix]; printf("%-4s : ",typename_str(k)); println_val(k); break; } SIMPLE_INSTR(OP_PRINT, "print") SIMPLE_INSTR(OP_DROP, "drop") SIMPLE_INSTR(OP_ADD, "add") SIMPLE_INSTR(OP_SUB, "sub") SIMPLE_INSTR(OP_MUL, "mul") SIMPLE_INSTR(OP_DIV, "div") SIMPLE_INSTR(OP_NIL, "nil") SIMPLE_INSTR(OP_TRUE, "true") SIMPLE_INSTR(OP_FALSE, "false") } } #undef SIMPLE_INSTR }