ADD: continue keyword.

main
bog 2023-08-27 00:14:02 +02:00
parent b9eb183319
commit 84b0af8090
4 changed files with 40 additions and 3 deletions

View File

@ -5,6 +5,7 @@
enum BlockInfoHints {
HINT_END,
HINT_START
};
enum BlockInfoKind {

View File

@ -111,7 +111,6 @@ void compile_node(compiler* self, node* root, program* prog)
for (size_t i=0; i<info->size; i++)
{
size_t addr = info->data[i]->addr;
int hint = info->data[i]->hint;
program_set_param(prog, addr, prog->instrs.size);
}
@ -136,7 +135,12 @@ void compile_node(compiler* self, node* root, program* prog)
for (size_t i=0; i<bi->size; i++)
{
if (bi->data[i]->hint == HINT_END)
if (bi->data[i]->hint == HINT_START)
{
program_set_param(prog, bi->data[i]->addr,
start_addr);
}
else if (bi->data[i]->hint == HINT_END)
{
program_set_param(prog, bi->data[i]->addr,
prog->instrs.size);
@ -151,6 +155,12 @@ void compile_node(compiler* self, node* root, program* prog)
block_info_push(bi, prog->instrs.size, HINT_END);
program_add_instr(prog, OP_BR, NO_PARAM);
}
else if (root->type == NODE_CONTINUE)
{
block_info* bi = compiler_find_info(self, INFO_KIND_LOOP);
block_info_push(bi, prog->instrs.size, HINT_START);
program_add_instr(prog, OP_BR, NO_PARAM);
}
else if (root->type == NODE_INDEX)
{
compile_children(self, root, prog);

View File

@ -85,7 +85,14 @@ exprs:
expr:
BREAK {
CONTINUE {
node* n = malloc(sizeof(node));
node_init(n, NODE_CONTINUE, "", line);
stack_push(n);
$$ = 1;
}
| BREAK {
node* n = malloc(sizeof(node));
node_init(n, NODE_BREAK, "", line);
stack_push(n);

View File

@ -34,3 +34,22 @@ end
assert 17 == e
let! f = 0
let! g = 0
while f < 100
f = f + 1
if f % 2 == 0
continue
end
if f >= 50
break
end
g = g + 1
end
assert 51 == f
assert 25 == g