public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1] fe: lexer: add single line comments support
Date: Thu, 28 Mar 2024 16:58:38 +0100	[thread overview]
Message-ID: <20240328155859.607264-1-johnny@johnnyrichard.com> (raw)

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 examples/main_exit.ol        |  3 +++
 src/lexer.c                  |  7 +++++++
 src/parser.c                 |  1 +
 tests/integration/cli_test.c | 31 +++++++++++++++++--------------
 4 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/examples/main_exit.ol b/examples/main_exit.ol
index c86fc68..8952017 100644
--- a/examples/main_exit.ol
+++ b/examples/main_exit.ol
@@ -1,3 +1,6 @@
+# Expected:
+# - output: "" 
+
 fn main(): u32 {
   return 0
 }
diff --git a/src/lexer.c b/src/lexer.c
index 801e4d0..684cad1 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -76,6 +76,13 @@ lexer_next_token(lexer_t *lexer, token_t *token)
     }
 
     while (lexer_is_not_eof(lexer)) {
+        if (current_char == '#') {
+            while (current_char != '\n' && lexer_is_not_eof(lexer)) {
+                lexer_skip_char(lexer);
+                current_char = lexer_current_char(lexer);
+            }
+        }
+
         if (isalpha(current_char)) {
             size_t start_offset = lexer->offset;
             while (isalnum(current_char) && lexer_is_not_eof(lexer)) {
diff --git a/src/parser.c b/src/parser.c
index 76ef91a..b800870 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -62,6 +62,7 @@ parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena, char *file_path)
 ast_node_t *
 parser_parse_program(parser_t *parser)
 {
+    skip_line_feeds(parser->lexer);
     ast_node_t *fn = parser_parse_fn_definition(parser);
     if (fn == NULL) {
         return NULL;
diff --git a/tests/integration/cli_test.c b/tests/integration/cli_test.c
index d46471b..e7ae059 100644
--- a/tests/integration/cli_test.c
+++ b/tests/integration/cli_test.c
@@ -25,20 +25,23 @@ test_cli_dump_tokens_example_main_exit(const MunitParameter params[], void *user
     cli_result_t compilation_result = cli_runner_compiler_dump_tokens("../../examples/main_exit.ol");
     munit_assert_int(compilation_result.exec.exit_code, ==, 0);
     munit_assert_string_equal(compilation_result.exec.stdout_buf,
-                              "../../examples/main_exit.ol:1:1: <fn>\n"
-                              "../../examples/main_exit.ol:1:4: <identifier>\n"
-                              "../../examples/main_exit.ol:1:8: <(>\n"
-                              "../../examples/main_exit.ol:1:9: <)>\n"
-                              "../../examples/main_exit.ol:1:10: <:>\n"
-                              "../../examples/main_exit.ol:1:12: <identifier>\n"
-                              "../../examples/main_exit.ol:1:16: <{>\n"
-                              "../../examples/main_exit.ol:1:17: <line_feed>\n"
-                              "../../examples/main_exit.ol:2:3: <return>\n"
-                              "../../examples/main_exit.ol:2:10: <number>\n"
-                              "../../examples/main_exit.ol:2:11: <line_feed>\n"
-                              "../../examples/main_exit.ol:3:1: <}>\n"
-                              "../../examples/main_exit.ol:3:2: <line_feed>\n"
-                              "../../examples/main_exit.ol:4:1: <EOF>\n");
+                              "../../examples/main_exit.ol:1:12: <line_feed>\n"
+                              "../../examples/main_exit.ol:2:16: <line_feed>\n"
+                              "../../examples/main_exit.ol:3:1: <line_feed>\n"
+                              "../../examples/main_exit.ol:4:1: <fn>\n"
+                              "../../examples/main_exit.ol:4:4: <identifier>\n"
+                              "../../examples/main_exit.ol:4:8: <(>\n"
+                              "../../examples/main_exit.ol:4:9: <)>\n"
+                              "../../examples/main_exit.ol:4:10: <:>\n"
+                              "../../examples/main_exit.ol:4:12: <identifier>\n"
+                              "../../examples/main_exit.ol:4:16: <{>\n"
+                              "../../examples/main_exit.ol:4:17: <line_feed>\n"
+                              "../../examples/main_exit.ol:5:3: <return>\n"
+                              "../../examples/main_exit.ol:5:10: <number>\n"
+                              "../../examples/main_exit.ol:5:11: <line_feed>\n"
+                              "../../examples/main_exit.ol:6:1: <}>\n"
+                              "../../examples/main_exit.ol:6:2: <line_feed>\n"
+                              "../../examples/main_exit.ol:7:1: <EOF>\n");
     return MUNIT_OK;
 }
 

base-commit: 117a06874c48c64e8ad4befbab244670f4f9ca9c
-- 
2.44.0


             reply	other threads:[~2024-03-28 14:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28 15:58 Johnny Richard [this message]
2024-03-28 14:59 ` [olang/patches/.build.yml] build failed builds.sr.ht
2024-03-28 16:46   ` Johnny Richard
2024-03-28 23:53 ` [PATCH olang v1] fe: lexer: add single line comments support Carlos Maniero

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240328155859.607264-1-johnny@johnnyrichard.com \
    --to=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox