From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id wHu4H7q+B2eX3gAAqHPOHw:P1 (envelope-from ) for ; Thu, 10 Oct 2024 13:47:06 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id wHu4H7q+B2eX3gAAqHPOHw (envelope-from ) for ; Thu, 10 Oct 2024 13:47:06 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=NHnCZYcz; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=IYejG5pU; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1"); dmarc=pass (policy=none) header.from=maniero.me ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1728560826; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=Kxey+wTsKBRNdTOmFuWvhPTqAzkYrclMiw21eLDXQ1U=; b=f/WWJqv48IltKev1AfP6c5SHhUYhQZzJN2jL+7t8deUN8/crc9/NPcokhB92HQecF+6E6q JPrsj48gfEJ/Niht0r3xxFWFQx0QvOP86IYR3xNgtidapDop8R6HX/xbCCG0vgiU6AP18l h8zAGoRgQbGYbt8FNh5VZrIsjl63SfPwTlQ/9O79HsAMpJbMn+WbcAQvA6OzUFKq3zT5j/ T6td5CZEM3tC0kiaPEipAxpTw4VDptrkbh4BW7Wgk+arE63Q5UvbRwa/+9/zmpcUSJWfdq 9SUp/HWgnlMEzSfu+9F00IYzxXK32ywTePeEmTt+PJxLAcRSwlngmwDlC3buzA== ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1728560826; a=rsa-sha256; cv=pass; b=JoFKw7Q2KF2NCYG1NIZRmb6a6f04/l/3jCR8VxGI/KNXSpWhwn+NQzLqLWhNO9guzyGGG6 OAP+rwyECcSE1kofIeoJBfirkrZDwFw4IganeK7PeMvljxOJu8SxoFm2ErrORPvS+pfMeh b7fYFEhisrfv3OUQjets78PluYxE787u9evQvfG7FG4hnFgcrOSZcmJcaYXQf3QctTS4aG /V/th1wa4QcFCZz4XO7okFfqisuxZAJOgRtOREaIyfL2Hlgzgm4PQwMiCmwT+MuYoGIOp8 3qG2NQNSvoTdBHS5Akd1QQxN+mrzI5NhMvgztTTWc1Y9lunfj4TZGPy43qAdLg== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=NHnCZYcz; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=IYejG5pU; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1"); dmarc=pass (policy=none) header.from=maniero.me Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 550BC72C3 for ; Thu, 10 Oct 2024 13:47:04 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=NhVyRMEvvB24UxN1uskz3ml4r8Bi5NvdzGWjLNj0RBE=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1728560823; v=1; b=NHnCZYcz2LZSK+vNWGNnPOtRq1znp5hB/widAy9YL5+ogl+fnCanA0T0dVBIOySKep88Nts5 pisWndBCYbPRY6CxoFWr0PmkINVOvfVtyMWacH9HU824zO+RQA87oclVW3oV3n5fX2+EiJMvjUo 0fEYiU+orkmcupDFmo6jeH1p2VDxUBT9DOYJJ2ys3rsGMTg3NT98P9qZhWhKN7KPXveHlJfSi8R 5+xHDVOsZ2+y+FM8hHn1nvl8hpWKKiszNN8ONZRYki1+SyeCxHFaJFetQ5gYPPiXOpgHWIMymbs ErcmXHB9WP+PlUHyCAy7Hs6ETu6QB5FpQzwK4Hdjbe4tA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id D066E20226 for ; Thu, 10 Oct 2024 11:47:03 +0000 (UTC) Received: from cockroach.ash.relay.mailchannels.net (cockroach.ash.relay.mailchannels.net [23.83.222.37]) by mail-a.sr.ht (Postfix) with ESMTPS id BB69720134 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 10 Oct 2024 11:47:02 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 9F8AF9425B4 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 10 Oct 2024 11:47:00 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (100-96-89-196.trex-nlb.outbound.svc.cluster.local [100.96.89.196]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id EE6E9942A77 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 10 Oct 2024 11:46:59 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1728560820; a=rsa-sha256; cv=none; b=0hedSX8AJVta0fhvpph7rnKFtgaRuAlddi6v083Z7BRQeylTeBZ+ZA8XlEh4ks02V7OIxh B0Z0N9heIPVq1IXsadxZsbtuvUWHkmC7UAbXaprF5P9oflUYgSmwlr7aaMK1oiLFxCm4y9 YzFu5nTO6vDoDVYYlz9NCrKZ4AXxFNTLnAyUrDQfkeDVstx5OCzIdyjolRxMhhdF2BNAvD XkxUqe5TJzFWKkG27proGPG1olTmlykyH+Too8oL+IY6ET/oz7Sv8trxM1Q1pYIji3ZOm5 nT6dReJ/pZXm6QJ7gv9MpLNCJxyzh7y7sD0/rXOL1mJa2KIXGWAKbPRkeTbJcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1728560820; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=Kxey+wTsKBRNdTOmFuWvhPTqAzkYrclMiw21eLDXQ1U=; b=751NMGL+z+72KOXQXmwX4HJJQ3Wvx8SKfhhqqMvBRwnRfukD6FgMKfAGgAzHjv8yCYCffk CkWITJ/c90M11Z23+n4X42xG8cEKws1L8H8WktO3yXov0jOiqnLnQ8VkH+vAYEVDUnDb0O 2AnOlkG7p6cT/2cUH5gJENVhky22S/sY88z6UPu4vZl0X2z4/oyKvx6LrGK7mhRgwm1Qta nV264ZNJ2b/bH67ws26U0m8uoSj8pff9xFOiIGhdw2u+0KM7rJ6tOTxV5Ul1aAuxxfJncS 8Q+CMCmdF9EqX35HgOFHkCM4HmDUZTyAdtGfP2Sjup/VGZOz77wu7tWbSi3Uhg== ARC-Authentication-Results: i=1; rspamd-5fcd6bf96c-hrs9w; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Hysterical-Turn: 1128d7e80a0454d9_1728560820438_2985507366 X-MC-Loop-Signature: 1728560820438:3352621108 X-MC-Ingress-Time: 1728560820437 Received: from nl-srv-smtpout1.hostinger.io (nl-srv-smtpout1.hostinger.io [145.14.150.87]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.96.89.196 (trex/7.0.2); Thu, 10 Oct 2024 11:47:00 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1728560817; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Kxey+wTsKBRNdTOmFuWvhPTqAzkYrclMiw21eLDXQ1U=; b=IYejG5pUtA6pbFva0RjEJT7MAtYNMImxt8GsouO71gfrQ20DZEJAtw465ICuB/So6R+oM5 ImVHTicme8s8TGZW8ubNRCXETMXblUSJC7Vl6/V9VDrVyCc+b7ELI+oQykomZD3uzlOMPH FXryGwnDXF5MFMehAVFht8kZBUI/ZRRSZzKSghjDt8+bmxueWL0/yVkk2TqtmzGO1pE1xp sRGzLwHd4uBPud6yBm6Wuy28B0Fv/ySbjBxwUiw+7cwfDdowFkoJ9GMlGvGA7bYhs7wxIB T6hxMDs/3e2ikCz3/6DgF2Y+FfnB00H64R7DMBdkDNWLwP59tkC196P3bfsE9g== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang] spec: parser: enable expression on function level Message-ID: <20241010114652.242440-1-carlos@maniero.me> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Thu, 10 Oct 2024 11:46:57 +0000 (UTC) X-CM-Analysis: v=2.4 cv=bZY5U/PB c=1 sm=1 tr=0 ts=6707beb1 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=5BosuNzstWIAoMYjI1wA:9 a=BXDaF_L80NY05PYiAFlV:22 X-CM-Envelope: MS4xfLa9VPIuSEu1AECyQy2n8t5UE3HPrFrSK4SuX9P6sxfnfp0B5BnN5Maq8z2mCe9ggmZylt74Vak2KJcdzupRmPUQRtFaP+UOa3ufwrYW0EjoWiBpkYyU pr9LvGf75FvmKA0gaopKLgvXDVa9yp3/0Cd6Bt+35EU4/eJy+cREdEdluQEZzWlt6/5LiSIQkMuyAKaGQwXhPE3MxhB0Z1/rJeK74TLUzp0T23zotRS707yB X-AuthUser: carlos@maniero.me X-Sourcehut-Patchset-Status: PROPOSED List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Spam-Score: -4.88 X-Spam-Score: -4.88 X-Migadu-Queue-Id: 550BC72C3 X-Migadu-Scanner: mx13.migadu.com X-TUID: 9hKdQXPrM1to 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 --- 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 *) ::= ';' | - ::= | | - ::= + ::= | ::= '=' | '*=' | '/=' @@ -74,7 +73,8 @@ language. ::= ( ('<' | '>' | '<=' | '>=') )* ::= ( ('<<' | '>>') )* ::= ( ('+' | '-') )* - ::= ( ('*' | '/' | '%') )* + ::= ( ('*' | '/' | '%') )* + ::= ( )* ::= | | @@ -82,12 +82,12 @@ language. | '(' ')' ::= - ::= & - | * - | + - | - - | ~ - | ! + ::= '&' + | '*' + | '+' + | '-' + | '~' + | '!' (* Identifiers *) ::= '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