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 1/2] lexer: add support to else keyword token
Date: Tue, 10 Sep 2024 01:54:58 +0200	[thread overview]
Message-ID: <20240909235611.84414-2-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240909235611.84414-1-johnny@johnnyrichard.com>

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/lexer.c                                   |  5 ++++
 src/lexer.h                                   |  1 +
 .../integration/tests/0023_else_statement.ol  | 26 +++++++++++++++++++
 3 files changed, 32 insertions(+)
 create mode 100644 tests/integration/tests/0023_else_statement.ol

diff --git a/src/lexer.c b/src/lexer.c
index 8147e4d..2129788 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -283,6 +283,7 @@ static char *token_kind_str_table[] = {
     [TOKEN_FN] = "fn",
     [TOKEN_RETURN] = "return",
     [TOKEN_IF] = "if",
+    [TOKEN_ELSE] = "else",
     [TOKEN_LF] = "line_feed",
     [TOKEN_OPAREN] = "(",
     [TOKEN_CPAREN] = ")",
@@ -414,6 +415,10 @@ lexer_str_to_token_kind(string_view_t text)
         return TOKEN_IF;
     }
 
+    if (string_view_eq_to_cstr(text, "else")) {
+        return TOKEN_ELSE;
+    }
+
     if (string_view_eq_to_cstr(text, "return")) {
         return TOKEN_RETURN;
     }
diff --git a/src/lexer.h b/src/lexer.h
index 2ee4405..98a6d87 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -39,6 +39,7 @@ typedef enum token_kind
     TOKEN_FN,
     TOKEN_RETURN,
     TOKEN_IF,
+    TOKEN_ELSE,
 
     // Equality operators
     TOKEN_CMP_EQ,
diff --git a/tests/integration/tests/0023_else_statement.ol b/tests/integration/tests/0023_else_statement.ol
new file mode 100644
index 0000000..60096e2
--- /dev/null
+++ b/tests/integration/tests/0023_else_statement.ol
@@ -0,0 +1,26 @@
+# Copyright (C) 2024 olang mantainers
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+fn main(): u32 {
+  if 0 != 0 {
+    return 1
+  } else {
+    return 0
+  }
+}
+
+# TEST test_contains_tokens WITH
+# ./tests/0023_else_statement.ol:19:5: <else>
+# END
-- 
2.46.0


  parent reply	other threads:[~2024-09-09 21:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-09 23:54 [PATCH olang v1 0/2] parser: implement else branching Johnny Richard
2024-09-09 22:06 ` Carlos Maniero
2024-09-09 23:54 ` Johnny Richard [this message]
2024-09-09 23:54 ` [PATCH olang v1 2/2] parser: add support to else branch Johnny Richard

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=20240909235611.84414-2-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