public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang] spec: parser: enable expression on function level
@ 2024-10-10 11:46 Carlos Maniero
  2024-10-10 11:47 ` [olang/patches/.build.yml] build success builds.sr.ht
  0 siblings, 1 reply; 2+ messages in thread
From: Carlos Maniero @ 2024-10-10 11:46 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

Previously, assignment statements were restricted to variable
assignments. This commit extends their functionality by:

    1. Enabling unary operators on the left-hand side (LHS) of
       assignments: This allows for expressions like { *a = 1 } to be
       valid.

    2. Permitting function calls within assignment statements: This
       enables calling functions without explicitly capturing their
       return values, as demonstrated in { my_fn() }.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 docs/info/specification.texi | 18 +++++++--------
 src/parser.c                 | 44 ++++++++----------------------------
 2 files changed, 19 insertions(+), 43 deletions(-)

diff --git a/docs/info/specification.texi b/docs/info/specification.texi
index a466331..921644a 100644
--- a/docs/info/specification.texi
+++ b/docs/info/specification.texi
@@ -48,8 +48,7 @@ language.
 
 (* Statements *)
 <end-of-statement>    ::= ';' | <line-break>
-<common-statement>    ::= <variable-definition> | <constant-definition> | <assignment>
-<assignment>          ::= <variable-name> <ows> <assignment-operator> <ows> <expression>
+<common-statement>    ::= <variable-definition> | <constant-definition>
 <assignment-operator> ::= '='
                         | '*='
                         | '/='
@@ -74,7 +73,8 @@ language.
 <cmp-relational-expression> ::= <bitwise-shift-expression> (<ows> ('<' | '>' | '<=' | '>=') <ows> <bitwise-shift-expression>)*
 <bitwise-shift-expression> ::= <additive-expression> (<ows> ('<<' | '>>') <ows> <additive-expression>)*
 <additive-expression> ::= <multiplicative-expression> (<ows> ('+' | '-') <ows> <multiplicative-expression>)*
-<multiplicative-expression> ::= <primary-expression> (<ows> ('*' | '/' | '%') <ows> <primary-expression>)*
+<multiplicative-expression> ::= <assignment-expression> (<ows> ('*' | '/' | '%') <ows> <assignment-expression>)*
+<assignment-expression> ::= <primary-expression> (<ows> <assignment-operator> <ows> <primary-expression>)*
 <primary-expression> ::= <integer-literal>
                        | <variable-name>
                        | <function-call>
@@ -82,12 +82,12 @@ language.
                        | '(' <ows>  <expression> <ows> ')'
 
 <unary-expression> ::= <unary-operator> <ows> <primary-expression>
-<unary-operator> ::= &
-                   | *
-                   | +
-                   | -
-                   | ~
-                   | !
+<unary-operator> ::= '&'
+                   | '*'
+                   | '+'
+                   | '-'
+                   | '~'
+                   | '!'
 
 (* Identifiers *)
 <type>                ::= 'u32'
diff --git a/src/parser.c b/src/parser.c
index f712bfc..12a7e2c 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -52,9 +52,6 @@ parser_parse_while_stmt(parser_t *parser);
 static ast_node_t *
 parser_parse_var_def(parser_t *parser);
 
-static ast_node_t *
-parser_parse_var_assign_stmt(parser_t *parser);
-
 static ast_node_t *
 parser_parse_fn_definition(parser_t *parser);
 
@@ -292,6 +289,15 @@ parser_parse_factor(parser_t *parser)
             if (token.kind == TOKEN_OPAREN) {
                 list_t *args = parser_parse_fn_args(parser);
                 return ast_new_node_fn_call(parser->arena, token_id.loc, token_id.value, args);
+            } else if (token.kind == TOKEN_EQ) {
+                if (!skip_expected_token(parser, TOKEN_EQ)) {
+                    return NULL;
+                }
+
+                ast_node_t *ref = ast_new_node_ref(parser->arena, token_id.loc, token_id.value);
+                ast_node_t *expr = parser_parse_expr(parser);
+
+                return ast_new_node_var_assign_stmt(parser->arena, token.loc, ref, expr);
             }
 
             return ast_new_node_ref(parser->arena, token_id.loc, token_id.value);
@@ -536,20 +542,11 @@ StartLoop:
             node = parser_parse_var_def(parser);
             break;
         }
-        case TOKEN_ID: {
-            lexer_lookahead(parser->lexer, &next_token, 2);
-            if (!expected_token(&next_token, TOKEN_EQ)) {
-                return NULL;
-            }
-            node = parser_parse_var_assign_stmt(parser);
-            break;
-        }
         case TOKEN_CCURLY: {
             goto EndLoop;
         }
         default: {
-            // FIXME: write a better error message
-            goto EndLoop;
+            node = parser_parse_expr(parser);
         }
     }
 
@@ -711,27 +708,6 @@ parser_parse_var_def(parser_t *parser)
     return var_node;
 }
 
-static ast_node_t *
-parser_parse_var_assign_stmt(parser_t *parser)
-{
-    token_t token_id;
-
-    if (!expected_next_token(parser, &token_id, TOKEN_ID)) {
-        return NULL;
-    }
-
-    token_t token_eq;
-
-    if (!expected_next_token(parser, &token_eq, TOKEN_EQ)) {
-        return NULL;
-    }
-
-    ast_node_t *ref = ast_new_node_ref(parser->arena, token_id.loc, token_id.value);
-    ast_node_t *expr = parser_parse_expr(parser);
-
-    return ast_new_node_var_assign_stmt(parser->arena, token_eq.loc, ref, expr);
-}
-
 static bool
 skip_expected_token(parser_t *parser, token_kind_t expected_kind)
 {

base-commit: d45df92d81f00059ecd1f6478bac7e4511d9fd8d
-- 
2.46.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-10-10 11:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-10 11:46 [PATCH olang] spec: parser: enable expression on function level Carlos Maniero
2024-10-10 11:47 ` [olang/patches/.build.yml] build success builds.sr.ht

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