From 4d1ceca9d6b905a45ff6748c154a28358a6e2d06 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 5 Jul 2024 08:52:44 +0200 Subject: Add printbool ops --- src/compiler.c | 43 +++++++++++++++++++++++++++++++------------ src/vm.c | 20 +++++++++++++++++++- tests/compilation.bad | 24 ++++++++++++++++-------- 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 { OP_PUTRET, // putret rx ; Put rx into the return value memory. OP_PUTRETI, // putreti cx ; Put cx into the return value memory. // Printing values with builtin print/println functions. - OP_PRINTSTR, // p rx - OP_PRINTSTRI, // p rx - OP_PRINTS64, // p rx - OP_PRINTF64, // p rx - OP_PRINTS64I, // p cx - OP_PRINTF64I, // p cx + OP_PRINTSTR, // p rx + OP_PRINTSTRI, // p cx + OP_PRINTS64, // p rx + OP_PRINTS64I, // p cx + OP_PRINTF64, // p rx + OP_PRINTF64I, // p cx + OP_PRINTBOOL, // p rx + OP_PRINTBOOLI, // p cx // Load/Store instructions. OP_LD8K, // ld8k rx, ca -> u8 rx = ca OP_LD16K, // ld16k rx, ca -> u16 rx = ca @@ -213,11 +215,14 @@ Str op_str[] = { [OP_LDLVAR] = cstr("LDLVAR "), [OP_LDLADDR] = cstr("LDLADDR "), [OP_LDSTR] = cstr("LDSTR "), - [OP_PRINTSTR] = cstr("PRNTSTR "), - [OP_PRINTS64] = cstr("PRNTS64 "), - [OP_PRINTF64] = cstr("PRNTF64 "), - [OP_PRINTS64I] = cstr("PRNTS64I"), - [OP_PRINTF64I] = cstr("PRNTF64I"), + [OP_PRINTSTR] = cstr("PRNSTR "), + [OP_PRINTSTRI] = cstr("PRNSTRI "), + [OP_PRINTS64] = cstr("PRNS64 "), + [OP_PRINTS64I] = cstr("PRNS64I "), + [OP_PRINTF64] = cstr("PRNF64 "), + [OP_PRINTF64I] = cstr("PRNF64I "), + [OP_PRINTBOOL] = cstr("PRNBOOL "), + [OP_PRINTBOOLI] = cstr("PRNBOOLI"), [OP_PUTRET] = cstr("PUTRET "), [OP_PUTRETI] = cstr("PUTRETI "), // Functions. @@ -891,6 +896,18 @@ compile_funcall(Chunk *chunk, Node *node, sz lab_pre, sz lab_post) { return (CompResult){.type = COMP_ERR}; } break; } + } else if (str_eq(expr->type, cstr("bool"))) { + switch (result.type) { + case COMP_CONST: { + emit_op(OP_PRINTBOOLI, result.idx, 0, 0, expr, chunk); + } break; + case COMP_REG: { + emit_op(OP_PRINTBOOL, result.idx, 0, 0, expr, chunk); + } break; + default: { + return (CompResult){.type = COMP_ERR}; + } break; + } } } if (str_eq(name, cstr("println"))) { @@ -1580,15 +1597,17 @@ disassemble_instruction(Instruction instruction) { case OP_PRINTS64: case OP_PRINTF64: case OP_PRINTSTR: - case OP_PRINTSTRI: + case OP_PRINTBOOL: case OP_PUSH: case OP_POP: case OP_PUTRET: println("%s r%d", op_str[instruction.op], instruction.dst, instruction.a, instruction.b); break; + case OP_PRINTSTRI: case OP_PRINTS64I: case OP_PRINTF64I: + case OP_PRINTBOOLI: case OP_RESERVE: case OP_PUSHI: 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) { u8 idx = instruction.dst; print("%d", vm->chunk->constants[idx].i); } break; + case OP_PRINTBOOL: { + u8 idx = instruction.dst; + bool val = vm->regs[idx].i; + if (val) { + print("true"); + } else { + print("false"); + } + } break; + case OP_PRINTBOOLI: { + u8 idx = instruction.dst; + bool val = vm->chunk->constants[idx].i; + if (val) { + print("true"); + } else { + print("false"); + } + } break; case OP_PRINTF64: { u8 idx = instruction.dst; printf("%f", vm->regs[idx].f); @@ -299,7 +317,7 @@ vm_run(VM *vm) { } break; case OP_PRINTSTR: { u8 idx = instruction.dst; - Str *string = (Str*)vm->regs[idx].ptr; + Str *string = (Str *)vm->regs[idx].ptr; print("%s", *string); } break; 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 @@ -let name = "alex" -let copy = "susan" -println(name) -println(copy) -set copy = name -println(copy) +; let name = "alex" +; let copy = "susan" +; println(name) +; println(copy) +; set copy = name +; println(copy) -fun greet(name: str greeting: str): nil println("hello " name " " greeting "!") +; fun greet(name: str greeting: str): nil println("hello " name " " greeting "!") -greet(name "WOOOW") +; greet(name "WOOOW") + +fun tester(condition: bool): nil { + if condition println("ding") else println("dong") +} + +tester(false) +println(true) +println(1 == 2) ; let y = 4 ; fun nested(): nil { -- cgit v1.2.1