aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-06-19 16:43:18 +0200
committerBad Diode <bd@badd10de.dev>2024-06-19 16:43:18 +0200
commitc0faac681a32ffc2e323917f8b54f33558b391a5 (patch)
tree7ccafe27d4233be6ffee460add131ed89d8a5341
parentc2c93cc4de5f7195d2c62af8984d6f41b96b0b1c (diff)
downloadbdl-c0faac681a32ffc2e323917f8b54f33558b391a5.tar.gz
bdl-c0faac681a32ffc2e323917f8b54f33558b391a5.zip
Add temp result printing after OP_HALT
-rw-r--r--src/main.c26
-rw-r--r--tests/expressions.bad8
2 files changed, 26 insertions, 8 deletions
diff --git a/src/main.c b/src/main.c
index d2bbe67..598a93f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -221,7 +221,7 @@ disassemble_chunk(Chunk chunk) {
221 if (array_size(chunk.constants) > 0) { 221 if (array_size(chunk.constants) > 0) {
222 println("%s: ======= constants ======", chunk.file_name); 222 println("%s: ======= constants ======", chunk.file_name);
223 for (sz i = 0; i < array_size(chunk.constants); i++) { 223 for (sz i = 0; i < array_size(chunk.constants); i++) {
224 println("%s: %x{2}: %x", chunk.file_name, i, chunk.constants[i]); 224 println("%s: %x{2}: %x{8}", chunk.file_name, i, chunk.constants[i]);
225 } 225 }
226 } 226 }
227} 227}
@@ -297,8 +297,10 @@ vm_run(VM *vm) {
297 fmod(vm->regs[src_a].f, vm->chunk->constants[src_b].f); 297 fmod(vm->regs[src_a].f, vm->chunk->constants[src_b].f);
298 } break; 298 } break;
299 case OP_HALT: { 299 case OP_HALT: {
300 println("VM HALT -> %d", vm->regs[instruction.dst]);
300 return; 301 return;
301 } 302 }
303
302 default: { 304 default: {
303 eprintln("unimplemented OP code: %d", instruction.op); 305 eprintln("unimplemented OP code: %d", instruction.op);
304 return; 306 return;
@@ -324,9 +326,9 @@ CompResult compile_expr(Chunk *chunk, Node *node);
324 326
325CompResult 327CompResult
326compile_binary(OpCode op, Chunk *chunk, Node *node) { 328compile_binary(OpCode op, Chunk *chunk, Node *node) {
329 sz reg_dst = chunk->reg_idx++;
327 CompResult comp_a = compile_expr(chunk, node->left); 330 CompResult comp_a = compile_expr(chunk, node->left);
328 CompResult comp_b = compile_expr(chunk, node->right); 331 CompResult comp_b = compile_expr(chunk, node->right);
329 sz reg_dst = chunk->reg_idx++;
330 sz reg_a; 332 sz reg_a;
331 sz reg_b; 333 sz reg_b;
332 switch (comp_a.type) { 334 switch (comp_a.type) {
@@ -454,12 +456,28 @@ process_file(Str path) {
454 array_zero(chunk.constants, 256, &bytecode_arena); 456 array_zero(chunk.constants, 256, &bytecode_arena);
455 array_zero(chunk.code, 0xffff, &bytecode_arena); 457 array_zero(chunk.code, 0xffff, &bytecode_arena);
456 sz n_roots = array_size(parser.nodes); 458 sz n_roots = array_size(parser.nodes);
459 CompResult res;
457 for (sz i = 0; i < n_roots; i++) { 460 for (sz i = 0; i < n_roots; i++) {
458 // The parser stores the root nodes as a stack. 461 // The parser stores the root nodes as a stack.
459 Node *root = parser.nodes[i]; 462 Node *root = parser.nodes[i];
460 compile_expr(&chunk, root); 463 res = compile_expr(&chunk, root);
464 }
465 sz res_reg = 0;
466 switch (res.type) {
467 case COMP_CONST: {
468 res_reg = chunk.reg_idx++;
469 Instruction inst =
470 (Instruction){.op = OP_LD64K, .dst = res_reg, .a = res.idx};
471 array_push(chunk.code, inst, chunk.storage);
472 } break;
473 case COMP_REG: {
474 res_reg = res.idx;
475 } break;
476 default: break;
461 } 477 }
462 array_push(chunk.code, (Instruction){.op = OP_HALT}, &bytecode_arena); 478 // After we are done move the last result to r0 for printing.
479 Instruction halt = (Instruction){.op = OP_HALT, .dst = res_reg};
480 array_push(chunk.code, halt, &bytecode_arena);
463 481
464 // Run bytecode on VM. 482 // Run bytecode on VM.
465 VM vm = {0}; 483 VM vm = {0};
diff --git a/tests/expressions.bad b/tests/expressions.bad
index 512da0a..3fc9431 100644
--- a/tests/expressions.bad
+++ b/tests/expressions.bad
@@ -1,10 +1,10 @@
1; 1 + -2 1; 1 + -2
269 + 1 269 + 1 + 2
3 3
4; 1 + 2 * 3 41 + 2 * 3
5 5
6; 1 + 2 * 3 - 4 61 + 2 * 3 - 4
7 7
8; 1 + 2 * (3 - 4) 81 + 2 * (3 - 4)
9 9
10; 1.0 - 1234.56e-3 10; 1.0 - 1234.56e-3