From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1] lexer: add 'while' keyword token
Date: Tue, 8 Oct 2024 17:53:18 +0200 [thread overview]
Message-ID: <20241008155356.177852-1-johnny@johnnyrichard.com> (raw)
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
docs/info/specification.texi | 3 ++-
src/lexer.c | 5 ++++
src/lexer.h | 1 +
tests/olc/0030_while_statement.ol | 40 +++++++++++++++++++++++++++++++
4 files changed, 48 insertions(+), 1 deletion(-)
create mode 100644 tests/olc/0030_while_statement.ol
diff --git a/docs/info/specification.texi b/docs/info/specification.texi
index 9144133..1bac50f 100644
--- a/docs/info/specification.texi
+++ b/docs/info/specification.texi
@@ -41,8 +41,9 @@ language.
<block> ::= '{' <ows> <statement> <ows> (<end-of-statement> <ows> <statement> <ows>)* <end-of-statement>? <ows> '}'
<function-args> ::= <expression> (<ows> ',' <function-args>)*
<function-call> ::= <function-name> <ows> '(' ( <ows> | <ows> <function-args> <ows> ) ')'
-<statement> ::= <common-statement> | <if-statement> | <return-statement> | <function-call>
+<statement> ::= <common-statement> | <if-statement> | <while-statement> | <return-statement> | <function-call>
<if-statement> ::= 'if' <ws> <expression> <ows> <block>
+<while-statement> ::= 'while' <ws> <expression> <ows> <block>
<return-statement> ::= 'return' <ws> <expression>
(* Statements *)
diff --git a/src/lexer.c b/src/lexer.c
index 4784f1c..5b4bbaa 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -290,6 +290,7 @@ static char *token_kind_str_table[] = {
[TOKEN_RETURN] = "return",
[TOKEN_IF] = "if",
[TOKEN_ELSE] = "else",
+ [TOKEN_WHILE] = "while",
[TOKEN_VAR] = "var",
[TOKEN_LF] = "line_feed",
[TOKEN_OPAREN] = "(",
@@ -424,6 +425,10 @@ lexer_str_to_token_kind(string_view_t text)
return TOKEN_ELSE;
}
+ if (string_view_eq_to_cstr(text, "while")) {
+ return TOKEN_WHILE;
+ }
+
if (string_view_eq_to_cstr(text, "var")) {
return TOKEN_VAR;
}
diff --git a/src/lexer.h b/src/lexer.h
index bb39a70..8beaea8 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -51,6 +51,7 @@ typedef enum token_kind
TOKEN_RETURN,
TOKEN_IF,
TOKEN_ELSE,
+ TOKEN_WHILE,
TOKEN_VAR,
// Equality operators
diff --git a/tests/olc/0030_while_statement.ol b/tests/olc/0030_while_statement.ol
new file mode 100644
index 0000000..eb47ae5
--- /dev/null
+++ b/tests/olc/0030_while_statement.ol
@@ -0,0 +1,40 @@
+# 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 {
+ var i: u32 = 0
+
+ while i < 10 {
+ i = i + 1
+ }
+
+ return i
+}
+
+# XTEST test_compile(exit_code=0)
+#
+# XTEST test_run_binary(exit_code=9)
+#
+# XTEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:u32>
+# `-Block
+# `-Return_Statement
+# `-Literal <kind:u32> <value:0>
+# END
+#
+# TEST test_contains_tokens WITH
+# ./0030_while_statement.ol:19:3: <while>
+# END
base-commit: dbbfdf2acab2b022cc9f0157d4341478d82b8838
--
2.46.0
next reply other threads:[~2024-10-08 13:54 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-08 15:53 Johnny Richard [this message]
2024-10-08 13:54 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-08 15:35 ` [PATCH olang] fixup! lexer: add 'while' keyword token Carlos Maniero
2024-10-08 15:40 ` [PATCH olang v1] " 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=20241008155356.177852-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