Rename 'while' to 'loop' and imp no condition loop

This commit is contained in:
David Gonzalez Martin 2024-06-06 15:57:43 -06:00
parent d998376bc0
commit 3a19ee4151
6 changed files with 83 additions and 46 deletions

14
TODOLIST Normal file
View File

@ -0,0 +1,14 @@
globals
arrays
assert
c abi
orelse
size
trailing zeroes
leading zeroes
while true
returns inside loops (if conditional)
returns inside loops (else conditional)
returns inside loops (non-conditional)
function pointers
for loops

View File

@ -1403,6 +1403,7 @@ const Keyword = enum{
@"else",
@"for",
@"if",
@"loop",
@"break",
};
@ -3601,6 +3602,53 @@ pub fn analyze_local_block(thread: *Thread, analyzer: *Analyzer, parser: *Parser
terminated = terminated or if_block.terminated;
}
},
'l' => {
const identifier = parser.parse_raw_identifier(src);
const loop_text = "loop";
if (byte_equal(identifier, loop_text)) {
parser.skip_space(src);
const loop_header_block = create_basic_block(thread);
const loop_body_block = create_basic_block(thread);
const loop_exit_block = create_basic_block(thread);
_ = emit_jump(analyzer, thread, loop_header_block);
analyzer.current_basic_block = loop_header_block;
if (src[parser.i] == '(') {
const condition = parser.parse_condition(analyzer, thread, file);
_ = emit_branch(analyzer, thread, condition, loop_body_block, loop_exit_block);
} else {
_ = emit_jump(analyzer, thread, loop_body_block);
}
parser.skip_space(src);
analyzer.current_basic_block = loop_body_block;
_ = analyzer.loops.append(.{
.continue_block = loop_header_block,
.break_block = loop_exit_block,
});
switch (src[parser.i]) {
brace_open => {
const loop_block = analyze_local_block(thread, analyzer, parser, file);
if (!loop_block.terminated) {
_ = emit_jump(analyzer, thread, loop_header_block);
} else {
unreachable;
}
},
else => unreachable,
}
analyzer.current_basic_block = loop_exit_block;
analyzer.loops.length -= 1;
} else {
parser.i = statement_start_ch_index;
}
},
'r' => {
const identifier = parser.parse_raw_identifier(src);
@ -3655,49 +3703,6 @@ pub fn analyze_local_block(thread: *Thread, analyzer: *Analyzer, parser: *Parser
parser.i = statement_start_ch_index;
}
},
'w' => {
const identifier = parser.parse_raw_identifier(src);
const while_text = "while";
if (byte_equal(identifier, while_text)) {
parser.skip_space(src);
const loop_header = create_basic_block(thread);
_ = emit_jump(analyzer, thread, loop_header);
analyzer.current_basic_block = loop_header;
const condition = parser.parse_condition(analyzer, thread, file);
parser.skip_space(src);
const loop_body_block = create_basic_block(thread);
const loop_exit_block = create_basic_block(thread);
_ = emit_branch(analyzer, thread, condition, loop_body_block, loop_exit_block);
analyzer.current_basic_block = loop_body_block;
_ = analyzer.loops.append(.{
.continue_block = loop_header,
.break_block = loop_exit_block,
});
switch (src[parser.i]) {
'{' => {
const loop_block = analyze_local_block(thread, analyzer, parser, file);
if (!loop_block.terminated) {
_ = emit_jump(analyzer, thread, loop_header);
} else {
unreachable;
}
},
else => unreachable,
}
analyzer.current_basic_block = loop_exit_block;
analyzer.loops.length -= 1;
} else {
parser.i = statement_start_ch_index;
}
},
else => {},
}

View File

@ -1,7 +1,7 @@
fn[cc(.c)] main[export]() s32 {
>i: s32 = 0;
>top: s32 = 10;
while (i < top) {
loop (i < top) {
i += 1;
}

View File

@ -1,7 +1,7 @@
fn[cc(.c)] main[export]() s32 {
>i: s32 = 0;
>n: s32 = 5;
while (i < 10) {
loop (i < 10) {
i += 1;
if (i == n) {
break;

View File

@ -1,7 +1,7 @@
fn[cc(.c)] main[export]() s32 {
>i: s32 = 0;
>n: s32 = 0;
while (i < 10) {
loop (i < 10) {
i += 1;
if (n == 0) {
continue;

View File

@ -0,0 +1,18 @@
fn foo() s32 {
>n: s32 = 6;
>a: s32 = 0;
loop {
if (a == n) {
return a;
}
a += 1;
}
}
fn[cc(.c)] main[export]() s32 {
>n: s32 = 6;
>result = foo();
return result - n;
}