From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:5f26::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id QGioHfJM4WWZ9gAAbAwnHQ (envelope-from ) for ; Fri, 01 Mar 2024 04:35:14 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id 2HDyGfJM4WWMTwEAqHPOHw (envelope-from ) for ; Fri, 01 Mar 2024 04:35:14 +0100 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; none 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 5CA611C1B5 for ; Fri, 1 Mar 2024 04:35:11 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=2/rPI/u9QHu1JrBjcxO6FXUfwcWMDwH5lsBYEH1huQY=; c=simple/simple; d=lists.sr.ht; h=Date:Subject:From:To:References:In-Reply-To:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1709264111; v=1; b=rIccgClM3dv0uBwSphnB1prye5ZawCUcpErE/nmn526iWmDfG0Y+IqhM3VlfaKgisfNlrcRi eNGArfOimiv34MrIGrWNwscemdzuA2WO/CjlTej3MyCXDLqP3eiIVLIgnU3sPuI+7rtQQS8YweD VbDHEsL8bJcqLIgXBdFvA8Oxzm05AImYk/dOQ7qQysuHX16XBCaIskainzRjFkW8GBgMyY9TiFX TCexfGgfL5bpMYcWggN04YHjTI+fys69p+Kn0qlsWIb27Y08Sm8HUBn8aqNPLN0x2TqMpoMYkR2 hBMnMsBllPd+/fhqaW/Cc9zWB8RVCNAttJGCPLHmkKWlg== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 323A2202C2 for ; Fri, 1 Mar 2024 03:35:11 +0000 (UTC) Received: from snail.cherry.relay.mailchannels.net (snail.cherry.relay.mailchannels.net [23.83.223.170]) by mail-a.sr.ht (Postfix) with ESMTPS id 0EDEF202B3 for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 1 Mar 2024 03:35:09 +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 53935417A1 for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 1 Mar 2024 03:35:08 +0000 (UTC) Received: from fr-int-smtpout5.hostinger.io (unknown [127.0.0.6]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 5F8DD4141F for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 1 Mar 2024 03:35:07 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1709264107; a=rsa-sha256; cv=none; b=UcQQkD20aYBPhvXC8NajyYNs/zHPvG/oqXDTaE079d8UfVg2ycG/MPzeCDYwc4hbccd5aF CnHxzVLffvLhxWGdsgNo+luksuE6GXbEBr+fglf8e8GMBx/0PcxJsmmCDnzaHb5okvq7LT wgygz11qCI3ikO2B1tbiF2HhbJHJdJ83y1C+7H7+zJINISQRqFsoZ31w04Ib9risa84VqE iR6ejtolGMpmWlDsVB8aiOIMnUUDoZcebpIwJ675Sr45v/UVKVxKXIviAHLjdG5H4ZH/sz lIRuV6ujveqNDT0Mm+BnR6nnIqyomaLTq1NOfHd7PLCb+D8jv+Amqby9ugZ2mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1709264107; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4E5/YKAoGYuNw6Fu/CQTLUN9Er/4x22yUAPipTFPApw=; b=EdfOi2/YvjM7ACqr9v8CQVXwbud7c7n/qtv+Va7dH41cVc+oPDgH5EWY10Y2yS0A+SEHC8 Is1fB5u4bKTg84u0By1bMZQ9Jnif4r1/MITIihqeNCpaFHBPHt/8Qjeqh+yKXowlEXJMKO FLLXMzq/5l1fIAcVyfDhbRbXizrgypkoow6GoDUJ4rM+F5Jj+ZWOeNUryBDYV+s0Im01Wi mMREujhFjqDkmRzwwRfKaB8hXd9tcmwtcEN/gI995ZQKg8WYXJ95E0NrYR10jsuQZk+7kP xct6I7FN0zfXfOGsWmLkS2qhku+iNdrHO6XpdmdHQIFAEyKZW8oVfoFZy6vr9Q== ARC-Authentication-Results: i=1; rspamd-7f9dd9fb96-ncb5z; 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-Oafish-Supply: 13cc090160b7f459_1709264107933_2004166329 X-MC-Loop-Signature: 1709264107933:1581851449 X-MC-Ingress-Time: 1709264107933 Received: from fr-int-smtpout5.hostinger.io ([UNAVAILABLE]. [89.116.146.168]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.111.192.177 (trex/6.9.2); Fri, 01 Mar 2024 03:35:07 +0000 Mime-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1709264105; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XS+hwWstwJGME40akld0uk8oBpYG6dDSRcnOR4FEb8Q=; b=MjWHNCLccFieV8kKWTGLVEzyv80xY+ujHFVFuQzW2MOm6Emf152FhTPatNxfW+zcifXuPn EzHwXpWG/NxS4N0yZANBRdc5MOg+HVPEK7ukJf4eq4GJtDuYbrY5+Dfh4sQFaGOEhKF2++ iYGdr9EXRwlJQom0awA7kE4KsBwHJ6CT15yGP3Dth2BQx9X9yRVmu3EXzzmM7gibFV4ebJ VinAxHw0cY40JVr9CJOLQndrLsVijXmBvsST6mhd4bN88DWJwWPF9Ut7UP0QBMJ2ewjXMR ABA9ue13syHD/dPvzxG4vxGqKcha9JRrAg8ofMd2+V8+r6mkZs3MiIAfKKDk4w== Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Fri, 01 Mar 2024 00:34:59 -0300 Message-Id: Subject: Re: [PATCH olang v1 4/4] parser: create simplified parser for tiny AST From: "Carlos Maniero" To: "Johnny Richard" , <~johnnyrichard/olang-devel@lists.sr.ht> X-Mailer: aerc 0.15.2-211-g37d5fc691aff References: <20240228190956.78191-1-johnny@johnnyrichard.com> <20240228190956.78191-5-johnny@johnnyrichard.com> In-Reply-To: <20240228190956.78191-5-johnny@johnnyrichard.com> X-CM-Envelope: MS4xfAzARSnxTOkitaovI316k05qwcQjhtLzHZB5nLfABtLb/6bhawihfTkgWNfDx0sNEJcKpZUttUgJuh9ITHKqgjIGocPfrzcvCZSutBtTFaoCTajqwn8y NhypkPk9ywI5ZD3s/ip8H4XgqgmPKdj21xoCFW9lNQTBhFCSASy5WEFguFZd0AKla+LbiPRQ3h/2t2lV6ON3w790KMhYJzD2pxkbBDGFaRUMCvkFXmfhJ39T 5zZpkN4EWkAIteRUNM19qA== X-CM-Analysis: v=2.4 cv=Ys4JRZYX c=1 sm=1 tr=0 ts=65e14ce9 a=5+VMC1FZ3J4mVPAKpPmAqg==:117 a=5+VMC1FZ3J4mVPAKpPmAqg==:17 a=IkcTkHD0fZMA:10 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=BXDaF_L80NYA:10 a=rEFr9_lTcNeVErhL6EoA:9 a=QEXdDO2ut3YA:10 X-AuthUser: carlos@maniero.me 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-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx12.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 5CA611C1B5 X-TUID: P6YHoYNK2rFy > +ast_node_t * > +ast_make_node_fn_def(arena_t *arena, string_view_t identifier, type_t re= turn_type, ast_node_t *block); > + > +ast_node_t * > +ast_make_node_literal_u32(arena_t *arena, uint32_t value); > + > +ast_node_t * > +ast_make_node_return_stmt(arena_t *arena); > + > +ast_node_t * > +ast_make_node_block(arena_t *arena); s/ast_make/ast_new just to keep consistency with other functions that allocate memory. > +void > +lexer_print_token_highlight(lexer_t *lexer, token_t *token, FILE *stream= ) > +{ > + size_t offset =3D token->location.bol; > + char *str =3D lexer->source.chars + offset; > + > + size_t i =3D 0; > + while ((i + offset) < lexer->source.size && str[i] !=3D '\n' && str[= i] !=3D 0) { > + ++i; > + } > + string_view_t line =3D { .chars =3D str, .size =3D i }; > + fprintf(stream, "" SV_FMT "\n", SV_ARG(line)); > + fprintf(stream, "%*s\n", (int)(token->location.offset - token->locat= ion.bol + 1), "^"); > +} 1. It bothers me a little that the lexer is performing IO operations. IMO, the parser should be responsible for error handling. But I can live with this if you think this is the right place for this function. 2. An alternative is making the lexer returning the line *string_view* and make the parser do the rest. 3. nitpick: Isn't *(i + offset) < lexer->source.size* and *str[i] !=3D 0* redundant? The last char will (or at least should) always be a NULL terminator. 4. I spent some time trying to understand what this function was supposed to do. I believe that having a function that just returns the token's line could help (2). But an alternative here is using the *string_view* struct instead of having *str* and *i*. Something like this: void lexer_print_token_highlight(lexer_t *lexer, token_t *token, FILE *stre= am) { size_t offset =3D token->location.bol; string_view_t line =3D { .chars =3D lexer->source.chars + offset, = .size =3D 0 }; =20 while (line.chars[i] !=3D '\n' && line.chars[i] !=3D 0) { ++line.size; } fprintf(stream, "" SV_FMT "\n", SV_ARG(line)); fprintf(stream, "%*s\n", (int)(token->location.offset - token->loc= ation.bol + 1), "^"); } > + if (!skip_expected_token(parser, TOKEN_COLON)) > + return NULL; > + > + skip_line_feeds(parser->lexer); > + > + type_t fn_return_type; > + if (!parser_parse_type(parser, &fn_return_type)) { > + return NULL; > + } nitpick: I=E2=80=99ve spotted some inconsistency with the use of brackets i= n if statements. Just throwing it out there, but I believe we should be using brackets in all if statements. However, I=E2=80=99m totally fine with removing them in guard clauses, as long as we maintain a consistent style. This is great work man! I'm excited of how close we are now from start the back-end.