Skip to content

Commit

Permalink
Make while worthwhile
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Aug 16, 2024
1 parent 02a99ee commit b47d729
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
13 changes: 11 additions & 2 deletions Sources/backends/cstyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,17 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
*offset += sprintf(&code[*offset], "\telse\n");
break;
}
case OPCODE_WHILE: {
*offset += sprintf(&code[*offset], "\twhile (_%" PRIu64 ")\n", o->op_while.condition.index);
case OPCODE_WHILE_START: {
*offset += sprintf(&code[*offset], "\twhile (true)\n");
*offset += sprintf(&code[*offset], "\t{\n");
break;
}
case OPCODE_WHILE_CONDITION: {
*offset += sprintf(&code[*offset], "\tif (!_%" PRIu64 ") break;\n", o->op_while.condition.index);
break;
}
case OPCODE_WHILE_END: {
*offset += sprintf(&code[*offset], "\t}\n");
break;
}
case OPCODE_BLOCK_START: {
Expand Down
16 changes: 15 additions & 1 deletion Sources/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,14 @@ void emit_statement(opcodes *code, block *parent, statement *statement) {
case STATEMENT_WHILE: {
{
opcode o;
o.type = OPCODE_WHILE;
o.type = OPCODE_WHILE_START;
o.size = OP_SIZE(o, op_nothing);
emit_op(code, &o);
}

{
opcode o;
o.type = OPCODE_WHILE_CONDITION;
o.size = OP_SIZE(o, op_while);

variable v = emit_expression(code, parent, statement->willy.test);
Expand All @@ -564,6 +571,13 @@ void emit_statement(opcodes *code, block *parent, statement *statement) {

emit_statement(code, parent, statement->willy.while_block);

{
opcode o;
o.type = OPCODE_WHILE_END;
o.size = OP_SIZE(o, op_nothing);
emit_op(code, &o);
}

break;
}
case STATEMENT_BLOCK: {
Expand Down
5 changes: 4 additions & 1 deletion Sources/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ typedef struct opcode {
OPCODE_OR,
OPCODE_IF,
OPCODE_ELSE,
OPCODE_WHILE,
OPCODE_WHILE_START,
OPCODE_WHILE_CONDITION,
OPCODE_WHILE_END,
OPCODE_WHILE_BODY,
OPCODE_BLOCK_START,
OPCODE_BLOCK_END
} type;
Expand Down

0 comments on commit b47d729

Please sign in to comment.