public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v3 2/2] parser: fix unary precedence
Date: Thu, 10 Oct 2024 16:29:34 +0000 (UTC)	[thread overview]
Message-ID: <20241010162918.338338-3-carlos@maniero.me> (raw)
In-Reply-To: <20241010162918.338338-1-carlos@maniero.me>

Unary is only supposed to contain primary expressions. Before this
commit, the code { *a = 1 } was interpreted as { *(a = 1) }.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/parser.c               |  2 +-
 tests/olc/0034_pointers.ol | 43 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 tests/olc/0034_pointers.ol

diff --git a/src/parser.c b/src/parser.c
index 44a4900..177195d 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -303,7 +303,7 @@ parser_parse_factor(parser_t *parser)
         case TOKEN_DASH:
         case TOKEN_TILDE:
         case TOKEN_BANG: {
-            ast_node_t *expr = parser_parse_expr(parser);
+            ast_node_t *expr = parser_parse_factor(parser);
             if (expr == NULL) {
                 return NULL;
             }
diff --git a/tests/olc/0034_pointers.ol b/tests/olc/0034_pointers.ol
new file mode 100644
index 0000000..3f3e80b
--- /dev/null
+++ b/tests/olc/0034_pointers.ol
@@ -0,0 +1,43 @@
+# 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/>.
+
+# A minimal olang program
+fn main(): u32 {
+  var a: u32 = 1
+  var b: u32* = &a
+  *b = 0
+  return a
+}
+
+# xTEST test_compile(exit_code=0)
+#
+# xTEST test_run_binary(exit_code=0)
+#
+# TEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:u32>
+#   `-Block
+#     |-Var_Definition <name:a> <kind:u32>
+#     | `-Literal <kind:u32> <value:1>
+#     |-Var_Definition <name:b> <kind:u32*>
+#     | `-Unary_Operation (&)
+#     |   `-Reference <name:a>
+#     |-Binary_Operation (=)
+#     | |-Unary_Operation (*)
+#     | | `-Reference <name:b>
+#     | `-Literal <kind:u32> <value:0>
+#     `-Return_Statement
+#       `-Reference <name:a>
+# END
-- 
2.46.1


  parent reply	other threads:[~2024-10-10 16:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-10 16:29 [PATCH olang v3 0/2] spec: parser: enable expression on function level Carlos Maniero
2024-10-10 16:29 ` [PATCH olang v3 1/2] " Carlos Maniero
2024-10-10 16:29 ` Carlos Maniero [this message]
2024-10-10 16:30   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-11  0:32 ` [PATCH olang v3 0/2] spec: parser: enable expression on function level 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=20241010162918.338338-3-carlos@maniero.me \
    --to=carlos@maniero.me \
    --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