From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id 4AKDHLUZBWf2FQEA62LTzQ:P1 (envelope-from ) for ; Tue, 08 Oct 2024 13:38:29 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id 4AKDHLUZBWf2FQEA62LTzQ (envelope-from ) for ; Tue, 08 Oct 2024 13:38:29 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=YTQ94SkB; dkim=pass header.d=johnnyrichard.com header.s=key1 header.b=4eUuUCFW; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; dmarc=pass (policy=quarantine) header.from=johnnyrichard.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1728387509; 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=TFfp3GidkQIYzxrtpkFU9I7VIgWSapSg5AH2Z1+IuuI=; b=GFOEkcR8mHE3ttRlk+Vd8EGgnTdPhPhYOWEcmrzEaNYNu9e6DWm2Pq9LTUL9aypolU4BcR P8kBJ3j+V+8yIOQVrDAJ5HsUPFzdWy14PiJCrgEfv2UyfQ/9QPC7WY8a1xrON2UlbazBIN a6VERz7VsG9oilciy8sL7OO6W2eiM29bT3HdXOJ6JY+pPfoizP62EdiR8RRinDeatmRuYz aUghm+cKvH5JWW9W65jJa1DveMMNds/Tkie7vfgj3DsTJPCyIwciFtC9fV9Vvyc5cCU/I5 fB07gsP0AtgDMPZdMvboIzDnbckYUuBAPTkl63hAJGyihsTJBYOYX7jG5N4Krw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=YTQ94SkB; dkim=pass header.d=johnnyrichard.com header.s=key1 header.b=4eUuUCFW; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; dmarc=pass (policy=quarantine) header.from=johnnyrichard.com ARC-Seal: i=1; s=key1; d=johnnyrichard.com; t=1728387509; a=rsa-sha256; cv=none; b=PKexzKsmp2qpDEX1GalJyYkTsZyZVSD/ie3+S+IECSZ0smqoGvITKVZkuQ3y0UV9+WTQFL DIVw3MHd0seWyjs+PAuOthLZ8nUrjfWDRXZ0m+jPPOhCdEHzb6BHSNQnR3O+r6W+LZ+1Qt migu0mKQpKpD5FcFURuDhhKNMASSpnAWeSdVdiCmp0c2h43961zGtumUYXnDJXcxuUjJHF whHECHQq50AeG09oDrZ6qOfsMbA8MFmjPonc3bFYfEjmBib1af8HZOsQkaZPoMV346vzDW JqdOLBC/g3TKPAGWRqUYEVtT8Xy+NzxSgQVKVAZv0v7WDlw3NH75/6KBHv1yWA== 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 ECAF9422BA for ; Tue, 08 Oct 2024 13:38:25 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=0HlX5dTRkBRMziuhgrRyz67PwB7WbL29sOI8k07FKuc=; 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=1728387504; v=1; b=YTQ94SkBb+8JUlnRcKC8FzMc9/KeKR40pT6M2bONI2IxeeYMw1V2j9gzp2urknVS1DnVGcDx N8R5x/+P1X2hedteIZlh568f6If62NbC/3FO1qfWo0l479NbolTArAvOhuVp24ftgWvR5bV978g qUEK4G/HT8yiUp8UxoiD66pXszPbnDW6R3fyMq2ZDNGDQ2cfLR/feNtkT0h0b4eOqGVqxo5oKA+ 4dxq6gkX6KgDyCi2256C8xxZIh3c4ll+gzAhogFerXz130Xvezwrn4uJdqkGUGuu39nLKqX0h1N 5TwIlwY1TAvh5aLbKMVa0iJmc1NtHRaGzJfijY2BoWI3A== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id C684A20154 for ; Tue, 08 Oct 2024 11:38:24 +0000 (UTC) Received: from out-184.mta1.migadu.com (out-184.mta1.migadu.com [95.215.58.184]) by mail-a.sr.ht (Postfix) with ESMTPS id 178B22013A for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 08 Oct 2024 11:38:24 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1728387502; 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=TFfp3GidkQIYzxrtpkFU9I7VIgWSapSg5AH2Z1+IuuI=; b=4eUuUCFWnOEKQ3iqejSmFtgjqRpDqcRxwswr5I+AGSlqkyMdoe6Kq00lTI4CNqKC9ybbUO NkJ+b9WvlcPk5O9xX3qYYfeEb3YjAlg3RW64CG4kUBo1biaIKbmmUj4AHnB76sqcI0s4wY /Hd000KjxNhRNb60seAEcuXE6YgdB47B7BEqIADus/NQscFPZaZ9KMRi130DhTPdcbMtcy aaeaVVP/asNu807rM9HZIBEhBj74q7ZyyP9Rg86/zioClu9E0R0D0WIVvHcNEV2H6qA5cj GIjJhfSkOS8jqpHXbQq5E9QT1foq2zMWLC8XtQ8Dc7/s0vFDYnQyRnE6dTTiiA== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1] parser: add support for parsing var assignments Date: Tue, 8 Oct 2024 15:37:47 +0200 Message-ID: <20241008133753.87352-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx11.migadu.com X-Migadu-Spam-Score: -1.62 X-Spam-Score: -1.62 X-Migadu-Queue-Id: ECAF9422BA X-TUID: x8iY/B77Iorl Signed-off-by: Johnny Richard --- src/ast.c | 14 +++++++++++++ src/ast.h | 12 +++++++++++ src/checker.c | 8 +++++++ src/parser.c | 37 ++++++++++++++++++++++++++++++++- src/pretty_print_ast.c | 14 +++++++++++++ tests/olc/0029_var_assigment.ol | 37 +++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/olc/0029_var_assigment.ol diff --git a/src/ast.c b/src/ast.c index 1224305..531415c 100644 --- a/src/ast.c +++ b/src/ast.c @@ -144,6 +144,20 @@ ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id) return node_ref; } +ast_node_t * +ast_new_node_var_assign_stmt(arena_t *arena, token_loc_t loc, ast_node_t *ref, ast_node_t *expr) +{ + ast_node_t *node_var_assign_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + assert(node_var_assign_stmt); + + node_var_assign_stmt->kind = AST_NODE_VAR_ASSIGN_STMT; + node_var_assign_stmt->loc = loc; + node_var_assign_stmt->as_var_assign_stmt.ref = ref; + node_var_assign_stmt->as_var_assign_stmt.expr = expr; + + return node_var_assign_stmt; +} + ast_node_t * ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr) { diff --git a/src/ast.h b/src/ast.h index f9a23b5..dce7cd8 100644 --- a/src/ast.h +++ b/src/ast.h @@ -36,6 +36,7 @@ typedef enum AST_NODE_FN_CALL, AST_NODE_VAR_DEF, AST_NODE_BINARY_OP, + AST_NODE_VAR_ASSIGN_STMT, AST_NODE_RETURN_STMT, AST_NODE_IF_STMT, AST_NODE_LITERAL, @@ -146,6 +147,13 @@ typedef struct ast_binary_op ast_node_t *rhs; } ast_binary_op_t; +typedef struct ast_var_assign_stmt +{ + ast_node_meta_t meta; + ast_node_t *ref; + ast_node_t *expr; +} ast_var_assign_stmt_t; + typedef struct ast_return_stmt { ast_node_meta_t meta; @@ -176,6 +184,7 @@ typedef union ast_node ast_literal_t as_literal; ast_ref_t as_ref; ast_block_t as_block; + ast_var_assign_stmt_t as_var_assign_stmt; ast_return_stmt_t as_return_stmt; ast_if_stmt_t as_if_stmt; } ast_node_t; @@ -206,6 +215,9 @@ ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value); ast_node_t * ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id); +ast_node_t * +ast_new_node_var_assign_stmt(arena_t *arena, token_loc_t loc, ast_node_t *ref, ast_node_t *expr); + ast_node_t * ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr); diff --git a/src/checker.c b/src/checker.c index ac4102a..4523b79 100644 --- a/src/checker.c +++ b/src/checker.c @@ -117,6 +117,14 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) return; } + case AST_NODE_VAR_ASSIGN_STMT: { + ast_var_assign_stmt_t var_assign_stmt = ast->as_var_assign_stmt; + + populate_scope(checker, scope, var_assign_stmt.ref); + populate_scope(checker, scope, var_assign_stmt.expr); + return; + } + case AST_NODE_RETURN_STMT: { ast_return_stmt_t return_stmt = ast->as_return_stmt; diff --git a/src/parser.c b/src/parser.c index 35c8107..d16b79d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -49,6 +49,9 @@ parser_parse_if_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); @@ -469,6 +472,14 @@ 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; } @@ -487,7 +498,6 @@ StartLoop: goto StartLoop; EndLoop: - skip_line_feeds(parser->lexer); if (!skip_expected_token(parser, TOKEN_CCURLY)) { return NULL; } @@ -603,6 +613,31 @@ 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); + + // FIXME: The expected line feed should be parsed from parent call + // according to the grammar rules + skip_line_feeds(parser->lexer); + + 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) { diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c index 2541544..3282166 100644 --- a/src/pretty_print_ast.c +++ b/src/pretty_print_ast.c @@ -193,6 +193,20 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) } return node; } + case AST_NODE_VAR_ASSIGN_STMT: { + pretty_print_node_t *node = pretty_print_node_new(arena); + ast_var_assign_stmt_t var_assign_stmt = ast->as_var_assign_stmt; + + node->name = "Var_Assigment"; + + pretty_print_node_t *ref = ast_node_to_pretty_print_node(var_assign_stmt.ref, arena); + pretty_print_node_t *expr = ast_node_to_pretty_print_node(var_assign_stmt.expr, arena); + + list_append(node->children, ref); + list_append(node->children, expr); + + return node; + } case AST_NODE_RETURN_STMT: { pretty_print_node_t *node = pretty_print_node_new(arena); ast_return_stmt_t return_stmt = ast->as_return_stmt; diff --git a/tests/olc/0029_var_assigment.ol b/tests/olc/0029_var_assigment.ol new file mode 100644 index 0000000..6c5c46f --- /dev/null +++ b/tests/olc/0029_var_assigment.ol @@ -0,0 +1,37 @@ +# Copyright (C) 2024 olang mantainers +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +fn main(): u32 { + var code: u32 = 1 + code = 0 + return code +} + +# XTEST test_compile(exit_code=0) + +# XTEST test_run_binary(exit_code=0) + +# TEST test_ast WITH +# Translation_Unit +# `-Function_Definition +# `-Block +# |-Var_Definition +# | `-Literal +# |-Var_Assigment +# | |-Reference +# | `-Literal +# `-Return_Statement +# `-Reference +# END base-commit: f493e84234cc21dafc987f07be5a5f7dea9e8c60 -- 2.46.0