From b2afd9e4a0389d824421b7dfe6b4db522b7a722b Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 23 Dec 2021 12:12:48 +0100 Subject: Add compilation of `or` builtin for the ir --- src/ir.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ir.h b/src/ir.h index bb15596..c1cdae4 100644 --- a/src/ir.h +++ b/src/ir.h @@ -29,6 +29,7 @@ typedef enum Op { // - Take a label as argument. // - For conditional jumps, the last value in the stack is used. OP_JUMP, + OP_JUMP_IF_TRUE, OP_JUMP_IF_FALSE, // Primitive complex commands. // - Prints the last object in the stack. @@ -53,6 +54,7 @@ static const char* ops_str[] = { [OP_DROP] = "OP_DROP", [OP_LABEL] = "OP_LABEL", [OP_JUMP] = "OP_JUMP", + [OP_JUMP_IF_TRUE] = "OP_JUMP_IF_TRUE", [OP_JUMP_IF_FALSE] = "OP_JUMP_IF_FALSE", [OP_PRINT] = "OP_PRINT", [OP_CALL] = "OP_CALL", @@ -117,6 +119,7 @@ print_instruction(Instruction *instruction) { OBJ_PRINT(instruction->argument); } break; case OP_JUMP: + case OP_JUMP_IF_TRUE: case OP_JUMP_IF_FALSE: case OP_LABEL: { printf("%-16s -> %zu\n", ops_str[op], instruction->label_id); @@ -180,7 +183,6 @@ compile_not(ProgramIr *program, Procedure *proc, void compile_and(ProgramIr *program, Procedure *proc, size_t line, size_t col, Object *args) { - // Generate labels. size_t label_false = program->labels++; size_t label_exit = program->labels++; while (args != NULL) { @@ -198,6 +200,18 @@ compile_and(ProgramIr *program, Procedure *proc, void compile_or(ProgramIr *program, Procedure *proc, size_t line, size_t col, Object *args) { + size_t label_true = program->labels++; + size_t label_exit = program->labels++; + while (args != NULL) { + compile_object(program, proc, args->head); + args = args->tail; + INST_ARG(proc, OP_JUMP_IF_TRUE, label_true, line, col); + } + INST_ARG(proc, OP_PUSH, &obj_false, line, col); + INST_ARG(proc, OP_JUMP, label_exit, line, col); + INST_ARG(proc, OP_LABEL, label_true, line, col); + INST_ARG(proc, OP_PUSH, &obj_true, line, col); + INST_ARG(proc, OP_LABEL, label_exit, line, col); } void -- cgit v1.2.1