From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang] spec: parser: enable expression on function level
Date: Thu, 10 Oct 2024 11:46:57 +0000 (UTC) [thread overview]
Message-ID: <20241010114652.242440-1-carlos@maniero.me> (raw)
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
next reply other threads:[~2024-10-10 11:47 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-10 11:46 Carlos Maniero [this message]
2024-10-10 11:47 ` [olang/patches/.build.yml] build success builds.sr.ht
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=20241010114652.242440-1-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