diff options
| author | hachem <im@hachem.wtf> | 2026-08-30 21:13:34 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-08-30 21:13:34 +0200 |
| commit | 13704a1df5c15af7163cc7764b0aeebc81ad2bdd (patch) | |
| tree | 4b9f10e1a686e8a4a87534d974d0bc3c64bd3d61 | |
| parent | 5d13b5dc014b2e81325c627ce36c44633655b2a4 (diff) | |
feat: add comment support
| -rw-r--r-- | src/lexer/lexer.c | 21 | ||||
| -rw-r--r-- | tests/lexer_test.c | 12 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 6018fab..97a91b1 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -75,6 +75,27 @@ static void skip_whitespace(struct Lexer* lexer) lexer->line += 1; advance(lexer); } + else if (c == '/' && lexer->current[1] == '/') + { + while (peek(lexer) != '\n' && peek(lexer) != '\0') + advance(lexer); + } + else if (c == '/' && lexer->current[1] == '*') + { + advance(lexer); + advance(lexer); + while (!(peek(lexer) == '*' && lexer->current[1] == '/') && peek(lexer) != '\0') + { + if (peek(lexer) == '\n') + lexer->line += 1; + advance(lexer); + } + if (peek(lexer) != '\0') + { + advance(lexer); + advance(lexer); + } + } else return; } diff --git a/tests/lexer_test.c b/tests/lexer_test.c index c5c9f14..11b15b9 100644 --- a/tests/lexer_test.c +++ b/tests/lexer_test.c @@ -53,6 +53,17 @@ static void test_line_counting(struct TestContext* context) check(context, scan_token(&lexer).line == 3); } +static void test_comments(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("rax // line comment\n/* block\ncomment */ rbx"); + check(context, token_matches(scan_token(&lexer), TOKEN_IDENTIFIER, "rax")); + + struct Token second = scan_token(&lexer); + check(context, token_matches(second, TOKEN_IDENTIFIER, "rbx")); + check(context, second.line == 3); + check(context, scan_token(&lexer).type == TOKEN_EOF); +} + void run_lexer_tests(struct TestContext* context) { test_identifiers_and_integers(context); @@ -60,4 +71,5 @@ void run_lexer_tests(struct TestContext* context) test_literals(context); test_keywords(context); test_line_counting(context); + test_comments(context); } |
