Implement comments

This commit is contained in:
David Gonzalez Martin 2025-02-28 14:19:30 -06:00
parent b3eee4ae99
commit 629a9bea96
3 changed files with 38 additions and 4 deletions

View File

@ -651,10 +651,33 @@ const Converter = struct {
}
fn skip_space(noalias converter: *Converter) void {
while (converter.offset < converter.content.len and is_space(converter.content[converter.offset])) {
converter.line_offset += @intFromBool(converter.content[converter.offset] == '\n');
converter.line_character_offset = if (converter.content[converter.offset] == '\n') converter.offset else converter.line_character_offset;
converter.offset += 1;
while (true) {
const offset = converter.offset;
while (converter.offset < converter.content.len and is_space(converter.content[converter.offset])) {
converter.line_offset += @intFromBool(converter.content[converter.offset] == '\n');
converter.line_character_offset = if (converter.content[converter.offset] == '\n') converter.offset else converter.line_character_offset;
converter.offset += 1;
}
if (converter.offset + 1 < converter.content.len) {
const i = converter.offset;
const is_comment = converter.content[i] == '/' and converter.content[i + 1] == '/';
if (is_comment) {
while (converter.offset < converter.content.len and converter.content[converter.offset] != '\n') {
converter.offset += 1;
}
if (converter.offset < converter.content.len) {
converter.line_offset += 1;
converter.line_character_offset = converter.offset;
converter.offset += 1;
}
}
}
if (converter.offset - offset == 0) {
break;
}
}
}

View File

@ -185,3 +185,7 @@ test "pointer" {
test "if_no_else" {
try invsrc(@src());
}
test "comments" {
try invsrc(@src());
}

7
tests/comments.bbb Normal file
View File

@ -0,0 +1,7 @@
[export] main = fn [cc(c)] () s32 // This is a comment
// This is a comment
{ // This is a comment
// This is a comment
return 0; // This is a comment
}// This is a comment
// This is a comment