From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id GE4xO5fDB2fWDwAA62LTzQ:P1 (envelope-from ) for ; Thu, 10 Oct 2024 14:07:52 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id GE4xO5fDB2fWDwAA62LTzQ (envelope-from ) for ; Thu, 10 Oct 2024 14:07:52 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=g+Whrsoy; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b="lA/njJVJ"; dmarc=pass (policy=none) header.from=maniero.me; 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") ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1728562071; a=rsa-sha256; cv=pass; b=x7yiIlYgJXsc6QxiiyRapjxL1iSxKlpdlIK9mgm6szFp6QF1APUdemAJWmeNH1/vud21UH GvwHjxGAhHN5o9m4t9jopvHs8thGr3xkPCgHTnn3s0KFdDUe2rboxivOKuCv45ZHF+2XU+ O2Ss1bs0jZmQj52MX4o/m3LbFDW7FnDkKQCDt0xWBrEH03AiGXmrKWEzlND43K7KFneFSM dn30T/fk22AWi9rgHITFaZIQ/SiOhqZAH5r2kRcvvPfWzxtZUvzkIUVILRMtOJIA1c4LBL 6SuGH6zgoCrL1o4WZav5l34kqOzM9dNaVCktizWqhYVeimpDeIXjPcvPAuxwUA== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=g+Whrsoy; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b="lA/njJVJ"; dmarc=pass (policy=none) header.from=maniero.me; 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") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1728562071; 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=esGu429H/JOqNcuclnX9i1t7QGNSTxKm15IQNheMIKo=; b=CFWB2yssKqNdrXsvI9tUOOo86duHBB5Vr8DlT9u4opYQObUyP66UfZAiKtVHUGJcKrybub +d6vOgPVegwQadJfrhMbsEHJKPF60SrZi8CJLPX+j3U9NQuqSWVOqwidWn4mLDritoYsSg ZnL8Fs8HWPWow+D8+8+z/kk4I9xgc/rHr3O0k77mAPuNnIzjStIH10CCB1/wDmL1RENp3k bebZEQn1S5xqvklCwCqpo7dQs5DUMn/UnV03k39mhqw6u0pUvwLTQRToY9CzlOyeMzCYUh U9J8pQKXk33kfbznZLTesqYqMle6bHcZreWMJG+r1VN3fkjZ9a97XgA7jIyK/g== 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 CF2AE76F2 for ; Thu, 10 Oct 2024 14:07:51 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=A/L9m8oblSw3kg/Q3Igeah7FKpiJFWw2ZajYy7z0R8I=; 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=1728562071; v=1; b=g+WhrsoyDeFUEpwKYGFFIt9i62KUbrirrHC68ElahnZQRvzoIIFRirxNRKOIeYNflJf+N6xb T+KhxOBPLL78pEZdzXckoh2yTEV4wVi75M8s0UwTUZX1YL3p74VHgElR3OLUfdYPYozsvxgODdt nJFn3Txe9YvL2lQY4p2P/mYEPAasse2OuTq9384KAOfQKtDaYgM1v0zGbojnX5qS6Z6mQB6eTug nCEHV29Ap2xR8JEhH3Rb4BYC5BIAXncWniLeLKS8vatPOKnjxx4QHHK2x1PXLPME5gzD4jLzMZI hL4bYNVp2o2bq1fZgX0nx4HhCrGQzCN+1yTYfM/2pl1Vw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 430C520228 for ; Thu, 10 Oct 2024 12:07:51 +0000 (UTC) Received: from rusty.tulip.relay.mailchannels.net (rusty.tulip.relay.mailchannels.net [23.83.218.252]) by mail-a.sr.ht (Postfix) with ESMTPS id 0C12420218 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 10 Oct 2024 12:07:46 +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 8E18D824840 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 10 Oct 2024 12:07:45 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (trex-0.trex.outbound.svc.cluster.local [100.96.176.246]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id CC6398239B6 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 10 Oct 2024 12:07:44 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1728562065; a=rsa-sha256; cv=none; b=WJtQN635YJhXf3IUNEwrXhBj5WsvGYzsc68ZV0E/h1XOeFmmauHGx51xF2ZA4JWxAuvQRi nIxdqlXBsBoNc4FwAzEc2B1zVWX4Q6kTBFi+X3u4GPfXf7f0C9/10rUSffU27w3mwuYwSC kluj4KLTkjj18JlkT3KhwMk0P2Ep1y4f6kzbOo9Q3I8fo/i8Vu3cDRvkcevdWtG3rNExxU HVGA+jTUWei/4Unvw4vDrURxh7Whfx45qxn3/ePvq/+BoHA8+9ubfWWHB4jr0en1UMyImv DJXkZp+V9FHxoMEDrCWSuvKS4mHD71a+c2JCM5IECP5a4hOcnIrkGxJG4h1peQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1728562065; 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=esGu429H/JOqNcuclnX9i1t7QGNSTxKm15IQNheMIKo=; b=fev5X3puCjWpC9PAbTV6ZgLk7pb9vGRDKpcwlDSs05lI8GHX/bG3DiRscEnXE0GxGCb6ly u+q3G9cU8HPOUI+eDTLXANGxOemH+J1RB2rk0JMbNaMlkS7N0W3CCu44bkEFuyfogzl1LB Y9VK6oyD4/QoOvTKjI6p9YDGtgFM5dv1tjwoqUWEqzcao1rtLZLosEaYezxxg7D1DaQCKh ccKxKkwgXgNMTimLKsR+JbqOFDhZIB0SsqHEZFt9QpeX4mOPP/sxzAdI8+0lfTmECePgAk hv0YwTRFb8UkyuyqGz4G2hJUBPlw5klB5F/6vHKzKtoYzGqgF5yT5SCawhZmVA== ARC-Authentication-Results: i=1; rspamd-5fcd6bf96c-9xvp4; 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-Unite-Share: 7d991ad04a9e64a1_1728562065314_259897123 X-MC-Loop-Signature: 1728562065314:1362996496 X-MC-Ingress-Time: 1728562065313 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.176.246 (trex/7.0.2); Thu, 10 Oct 2024 12:07:45 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1728562063; 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=esGu429H/JOqNcuclnX9i1t7QGNSTxKm15IQNheMIKo=; b=lA/njJVJwLdLOca5vgCsbm1JMHeOh2rYbL044Cuva47ia4xeJMtVRadKQbKfsBj3h2e9ZA uf+QEdF4qwdohO56HPSwKzblwbRRw34HguacVU3wu8Oyp94eKMBVCTSxpuDYo9wil1M2KC u2dJSukbLIFSfOmroFT0YSnb2S7tVHkhUL/iaxS2pz8TnXVVdwCRxJvJ1u4UBfH5KS2Tpo 12hBsQhqFdqEra5I6qrSzCPYTYg8DdlMRO5gh9/tcH+b8PK3derVApqWD7IrV8tX6kYSEZ GDfB09BzkUtUX6JXtwV8Qu42iU336vStRdQZ1ceiGQxbIJdiJlpdadAFkS3Psw== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v2] spec: parser: enable expression on function level Message-ID: <20241010120717.243191-2-carlos@maniero.me> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Thu, 10 Oct 2024 12:07:42 +0000 (UTC) X-CM-Analysis: v=2.4 cv=bZY5U/PB c=1 sm=1 tr=0 ts=6707c38f a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=5BosuNzstWIAoMYjI1wA:9 a=BXDaF_L80NY05PYiAFlV:22 X-CM-Envelope: MS4xfEPtN9GYiiBjCB1xiXX/Dz45JB4GOjRNRTLCm1NQNreVjvGy6pU5VWZ18gPfx5NdQ2JPxpzGL4MRkiyD6In6HJHP6RHVMSHVOOtG4UaZQlll7DTBFx3a rhyteBxpMy2MWIhWQHF8hB7K09EJroBEIuojUPdh8zal/bATbAasePbTBK53OzTButhPHF8qCwp47kOiJhBsmnZ3CxIJKHI+cQT/+TdXsceEz9RRZ4S95LRW 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: -0.80 X-Spam-Score: -0.80 X-Migadu-Queue-Id: CF2AE76F2 X-Migadu-Scanner: mx12.migadu.com X-TUID: gqN6hZsch9Jn 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 --- v2: add missing expression on 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..8653f79 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