aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-07-05 08:52:44 +0200
committerBad Diode <bd@badd10de.dev>2024-07-05 08:52:44 +0200
commit4d1ceca9d6b905a45ff6748c154a28358a6e2d06 (patch)
tree5918d07178beefeb38220976b92ef5733c9ca935
parent998f649edd6c478af225ce3e885d596dd29c3a4f (diff)
downloadbdl-4d1ceca9d6b905a45ff6748c154a28358a6e2d06.tar.gz
bdl-4d1ceca9d6b905a45ff6748c154a28358a6e2d06.zip
Add printbool ops
-rw-r--r--src/compiler.c43
-rw-r--r--src/vm.c20
-rw-r--r--tests/compilation.bad24
3 files changed, 66 insertions, 21 deletions
diff --git a/src/compiler.c b/src/compiler.c
index 8bc71cb..b420ca5 100644
--- a/src/compiler.c
+++ b/src/compiler.c
@@ -107,12 +107,14 @@ typedef enum OpCode {
107 OP_PUTRET, // putret rx ; Put rx into the return value memory. 107 OP_PUTRET, // putret rx ; Put rx into the return value memory.
108 OP_PUTRETI, // putreti cx ; Put cx into the return value memory. 108 OP_PUTRETI, // putreti cx ; Put cx into the return value memory.
109 // Printing values with builtin print/println functions. 109 // Printing values with builtin print/println functions.
110 OP_PRINTSTR, // p rx 110 OP_PRINTSTR, // p rx
111 OP_PRINTSTRI, // p rx 111 OP_PRINTSTRI, // p cx
112 OP_PRINTS64, // p rx 112 OP_PRINTS64, // p rx
113 OP_PRINTF64, // p rx 113 OP_PRINTS64I, // p cx
114 OP_PRINTS64I, // p cx 114 OP_PRINTF64, // p rx
115 OP_PRINTF64I, // p cx 115 OP_PRINTF64I, // p cx
116 OP_PRINTBOOL, // p rx
117 OP_PRINTBOOLI, // p cx
116 // Load/Store instructions. 118 // Load/Store instructions.
117 OP_LD8K, // ld8k rx, ca -> u8 rx = ca 119 OP_LD8K, // ld8k rx, ca -> u8 rx = ca
118 OP_LD16K, // ld16k rx, ca -> u16 rx = ca 120 OP_LD16K, // ld16k rx, ca -> u16 rx = ca
@@ -213,11 +215,14 @@ Str op_str[] = {
213 [OP_LDLVAR] = cstr("LDLVAR "), 215 [OP_LDLVAR] = cstr("LDLVAR "),
214 [OP_LDLADDR] = cstr("LDLADDR "), 216 [OP_LDLADDR] = cstr("LDLADDR "),
215 [OP_LDSTR] = cstr("LDSTR "), 217 [OP_LDSTR] = cstr("LDSTR "),
216 [OP_PRINTSTR] = cstr("PRNTSTR "), 218 [OP_PRINTSTR] = cstr("PRNSTR "),
217 [OP_PRINTS64] = cstr("PRNTS64 "), 219 [OP_PRINTSTRI] = cstr("PRNSTRI "),
218 [OP_PRINTF64] = cstr("PRNTF64 "), 220 [OP_PRINTS64] = cstr("PRNS64 "),
219 [OP_PRINTS64I] = cstr("PRNTS64I"), 221 [OP_PRINTS64I] = cstr("PRNS64I "),
220 [OP_PRINTF64I] = cstr("PRNTF64I"), 222 [OP_PRINTF64] = cstr("PRNF64 "),
223 [OP_PRINTF64I] = cstr("PRNF64I "),
224 [OP_PRINTBOOL] = cstr("PRNBOOL "),
225 [OP_PRINTBOOLI] = cstr("PRNBOOLI"),
221 [OP_PUTRET] = cstr("PUTRET "), 226 [OP_PUTRET] = cstr("PUTRET "),
222 [OP_PUTRETI] = cstr("PUTRETI "), 227 [OP_PUTRETI] = cstr("PUTRETI "),
223 // Functions. 228 // Functions.
@@ -891,6 +896,18 @@ compile_funcall(Chunk *chunk, Node *node, sz lab_pre, sz lab_post) {
891 return (CompResult){.type = COMP_ERR}; 896 return (CompResult){.type = COMP_ERR};
892 } break; 897 } break;
893 } 898 }
899 } else if (str_eq(expr->type, cstr("bool"))) {
900 switch (result.type) {
901 case COMP_CONST: {
902 emit_op(OP_PRINTBOOLI, result.idx, 0, 0, expr, chunk);
903 } break;
904 case COMP_REG: {
905 emit_op(OP_PRINTBOOL, result.idx, 0, 0, expr, chunk);
906 } break;
907 default: {
908 return (CompResult){.type = COMP_ERR};
909 } break;
910 }
894 } 911 }
895 } 912 }
896 if (str_eq(name, cstr("println"))) { 913 if (str_eq(name, cstr("println"))) {
@@ -1580,15 +1597,17 @@ disassemble_instruction(Instruction instruction) {
1580 case OP_PRINTS64: 1597 case OP_PRINTS64:
1581 case OP_PRINTF64: 1598 case OP_PRINTF64:
1582 case OP_PRINTSTR: 1599 case OP_PRINTSTR:
1583 case OP_PRINTSTRI: 1600 case OP_PRINTBOOL:
1584 case OP_PUSH: 1601 case OP_PUSH:
1585 case OP_POP: 1602 case OP_POP:
1586 case OP_PUTRET: 1603 case OP_PUTRET:
1587 println("%s r%d", op_str[instruction.op], instruction.dst, 1604 println("%s r%d", op_str[instruction.op], instruction.dst,
1588 instruction.a, instruction.b); 1605 instruction.a, instruction.b);
1589 break; 1606 break;
1607 case OP_PRINTSTRI:
1590 case OP_PRINTS64I: 1608 case OP_PRINTS64I:
1591 case OP_PRINTF64I: 1609 case OP_PRINTF64I:
1610 case OP_PRINTBOOLI:
1592 case OP_RESERVE: 1611 case OP_RESERVE:
1593 case OP_PUSHI: 1612 case OP_PUSHI:
1594 case OP_PUTRETI: 1613 case OP_PUTRETI:
diff --git a/src/vm.c b/src/vm.c
index 404f410..0929fb6 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -289,6 +289,24 @@ vm_run(VM *vm) {
289 u8 idx = instruction.dst; 289 u8 idx = instruction.dst;
290 print("%d", vm->chunk->constants[idx].i); 290 print("%d", vm->chunk->constants[idx].i);
291 } break; 291 } break;
292 case OP_PRINTBOOL: {
293 u8 idx = instruction.dst;
294 bool val = vm->regs[idx].i;
295 if (val) {
296 print("true");
297 } else {
298 print("false");
299 }
300 } break;
301 case OP_PRINTBOOLI: {
302 u8 idx = instruction.dst;
303 bool val = vm->chunk->constants[idx].i;
304 if (val) {
305 print("true");
306 } else {
307 print("false");
308 }
309 } break;
292 case OP_PRINTF64: { 310 case OP_PRINTF64: {
293 u8 idx = instruction.dst; 311 u8 idx = instruction.dst;
294 printf("%f", vm->regs[idx].f); 312 printf("%f", vm->regs[idx].f);
@@ -299,7 +317,7 @@ vm_run(VM *vm) {
299 } break; 317 } break;
300 case OP_PRINTSTR: { 318 case OP_PRINTSTR: {
301 u8 idx = instruction.dst; 319 u8 idx = instruction.dst;
302 Str *string = (Str*)vm->regs[idx].ptr; 320 Str *string = (Str *)vm->regs[idx].ptr;
303 print("%s", *string); 321 print("%s", *string);
304 } break; 322 } break;
305 case OP_PRINTSTRI: { 323 case OP_PRINTSTRI: {
diff --git a/tests/compilation.bad b/tests/compilation.bad
index 746d2b9..257a6e7 100644
--- a/tests/compilation.bad
+++ b/tests/compilation.bad
@@ -1,13 +1,21 @@
1let name = "alex" 1; let name = "alex"
2let copy = "susan" 2; let copy = "susan"
3println(name) 3; println(name)
4println(copy) 4; println(copy)
5set copy = name 5; set copy = name
6println(copy) 6; println(copy)
7 7
8fun greet(name: str greeting: str): nil println("hello " name " " greeting "!") 8; fun greet(name: str greeting: str): nil println("hello " name " " greeting "!")
9 9
10greet(name "WOOOW") 10; greet(name "WOOOW")
11
12fun tester(condition: bool): nil {
13 if condition println("ding") else println("dong")
14}
15
16tester(false)
17println(true)
18println(1 == 2)
11 19
12; let y = 4 20; let y = 4
13; fun nested(): nil { 21; fun nested(): nil {