diff options
Diffstat (limited to 'src/bytecode/vm.h')
-rwxr-xr-x | src/bytecode/vm.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h index 33582b1..3f59aff 100755 --- a/src/bytecode/vm.h +++ b/src/bytecode/vm.h | |||
@@ -60,6 +60,22 @@ vm_reset(VM *vm) { | |||
60 | } | 60 | } |
61 | 61 | ||
62 | // Helper macros for a more clear VM switch. | 62 | // Helper macros for a more clear VM switch. |
63 | #ifdef DEBUG | ||
64 | #define STACK_TRACE() \ | ||
65 | fprintf(stderr, "stack trace:\n"); \ | ||
66 | for (ssize_t i = array_size(vm->frames) - 1; i >= 0 ; i--) { \ | ||
67 | CallFrame frame = vm->frames[i]; \ | ||
68 | Chunk *chunk = frame.chunk; \ | ||
69 | size_t instruction = vm->pc - chunk->code - 1; \ | ||
70 | fprintf(stderr, "\t%-4ld -> ", i); \ | ||
71 | fprintf(stderr, "%.*s",(int)array_size(chunk->name), chunk->name); \ | ||
72 | fprintf(stderr, ":%ld:%ld\n", chunk->lines[instruction].line, chunk->lines[instruction].col); \ | ||
73 | vm->pc = frame.rp; \ | ||
74 | } | ||
75 | #else | ||
76 | #define STACK_TRACE() | ||
77 | #endif | ||
78 | |||
63 | #define RUNTIME_ERROR(ERR) \ | 79 | #define RUNTIME_ERROR(ERR) \ |
64 | error_push((Error){ \ | 80 | error_push((Error){ \ |
65 | .type = ERR_TYPE_RUNTIME, \ | 81 | .type = ERR_TYPE_RUNTIME, \ |
@@ -67,6 +83,7 @@ vm_reset(VM *vm) { | |||
67 | .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, \ | 83 | .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, \ |
68 | .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, \ | 84 | .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, \ |
69 | }); \ | 85 | }); \ |
86 | STACK_TRACE() \ | ||
70 | return | 87 | return |
71 | 88 | ||
72 | #define FIXNUM_ARITHMETIC_OP(OP) \ | 89 | #define FIXNUM_ARITHMETIC_OP(OP) \ |