From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.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 6EijF9t0BmdaEwEA62LTzQ:P1 (envelope-from ) for ; Wed, 09 Oct 2024 14:19:39 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id 6EijF9t0BmdaEwEA62LTzQ (envelope-from ) for ; Wed, 09 Oct 2024 14:19:39 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=Znu8ZVzd; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=OOmmNbDU; 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=1728476379; 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=OcV74C53uq26B7crM5uhpTUoJeIFV2ljvahRntjg+qY=; b=2E+zIx7vSbJD2MT+Noy8HrXbZGqvzXXM6aqzSSYGF2zkLfL+dNsMy/mfBtqvQc0evXwaAv JsSt6NWMo34AIjiS75adiD2wsXy06H3hIjfLKsD7PhSJBVSfWPjf11rSHXZ30JiktT/t3u Q8cs+t13Aw3PyNUP7/no5LVNMee6eYhNAFnwf9nwGMGamQeicicfhzXquqKnMnEBMBmeim UytyXXia31bRSlj9Vfwe9UPvU9rRR5xgTp00qR4TMxD2stDheZk3aGV71IFwf29yR7Wx9X 2cWLpJoZu8q//VcDiyyEmYza480KPDpvMUTbOMQKpxBlYuRUu6tHArfkiYKlZQ== ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1728476379; a=rsa-sha256; cv=pass; b=1iyzMw5G7XE9+g06NoyuR0vipOpv+4yXd/bhTSsGxW+7DWbVAoOTHSQSZUw8VsClUaZvWR DNweaXKmucioKG34d0fNUKqsiTobyRDiWBZ11ny64QzAc6n8rAgQ+KewzRRAKgkFBslt+P MQz9KwlD+A7gKaGsf5P5JnXgZUT8smtDgdOGYnk6ssWm70EZQFivmeIhnxMnkhcR+Nkg09 lz1d352DT1WUKccLqeWhx/uedWY1/bzTHXJIrI98PHYMLINOR+joaaLWId1f+EaetwiASe uzlfHwQx7XahhcaGQT8p0flPWWIxHpDZkJIuV0IMigappg7irF1QXsr3R4bbig== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=Znu8ZVzd; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=OOmmNbDU; 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 2902B793B9 for ; Wed, 09 Oct 2024 14:19:39 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=rNDqD9gWtNNLipgPxXSwqLGyHQbI5pn/7gpbgDwx3JI=; 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=1728476373; v=1; b=Znu8ZVzdSSsDmQbum1Dg49pfANwI49Pjvo0w4nQmflwzhjh7zqKlUW9II/xVLoPLEzPA7WR4 dzRMiw5ZzxOCA97Ggsr305h6DC000EQRz+sYqYVLaHYJAVczXqGMJkkuyRktAuNzJT6Ksgxjou1 teJwbiyRZj1z295b3E9OEeUWakRKh+AuPIZjwJJZ9VAcOgcxklBOjUhEpYUsA+pSFaED5SgJsbc CHXSWZXjk6V1OfnjO3C1/764NnBihWLjqzL6RUyHQxvFGWGhujA0QNQpnkh+mblU8/LFEjRV+1k poUVLrsymr0U1p6dhVBgszd0GsP/zMBP1Qxm2na5J+u2Q== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id D2CEC20259 for ; Wed, 09 Oct 2024 12:19:33 +0000 (UTC) Received: from eastern.ash.relay.mailchannels.net (eastern.ash.relay.mailchannels.net [23.83.222.55]) by mail-a.sr.ht (Postfix) with ESMTPS id C42202021D for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 09 Oct 2024 12:19:32 +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 A16608453EB for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 9 Oct 2024 12:19:30 +0000 (UTC) Received: from fr-int-smtpout5.hostinger.io (100-100-145-219.trex-nlb.outbound.svc.cluster.local [100.100.145.219]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id B57EE845AB7 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 9 Oct 2024 12:19:29 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1728476370; a=rsa-sha256; cv=none; b=uqsaIHn+W+gFq4hFmwBL2GxyhorPmLl4sOHDVU0Kxx8BFBGVJegcygJsM+jYeY79Fbr4YN JbAAchfQ/YYd2QV70c6Lqxr7a78uXDrpNIlsGDbMEFlgwKuOzC69YfehvNR3HaeoqaDnsr FCVvBhknz8AXaXtQBw62Isn5VRRhgoex2/hq2n8RXADlxlI3Fqiecmv51xqoNTBW+l7pms j8R/QDnUNf3NiqmfQUamrjK6OmZ0Zex3Fq0/kvsSJ/B7tt5LYAy6OyrN7LjPqAnAwWl5Sp v9jpCCICivtvjhJD8crRqcI2LBAoduVex+Yr+gNF1hYv4pJbDulSSPmUUYfI1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1728476370; 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=OcV74C53uq26B7crM5uhpTUoJeIFV2ljvahRntjg+qY=; b=WmhvUvIqkDPpTGTNF+VsRdWmFocgc0HjsU4p495g7/vzSEan8AjeULG1dtSHKy9UNM0xLG +zTGXMvhWjmMelwOgMCw07u3nDEACynvV8OnN26rlF/h8XMVRCF9AGpe7ZlF/gyYIpayli lM2dFkcP20NsBe0u+iut0mGB+V4t68kfQBeVM80zvN+c2sPpSSSfBNGXK+k3MlMbSZETu2 MqpQYExnag5n/tY/LmANTPj8U0ijGs54grkTK405lbF1eyxo93w08Ds5pKtTgyKgTbysQv z/eeI+3X2MFE43iDMlmk1xLaD7FfGZC1CMiC79uPNuW9QdABgThboHDXsg2BSA== ARC-Authentication-Results: i=1; rspamd-54f99b5bc4-2fzrz; 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-Illegal-Skirt: 31d48eda532bea05_1728476370400_4240376811 X-MC-Loop-Signature: 1728476370400:1889867181 X-MC-Ingress-Time: 1728476370399 Received: from fr-int-smtpout5.hostinger.io (fr-int-smtpout5.hostinger.io [89.116.146.168]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.100.145.219 (trex/7.0.2); Wed, 09 Oct 2024 12:19:30 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1728476367; 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=OcV74C53uq26B7crM5uhpTUoJeIFV2ljvahRntjg+qY=; b=OOmmNbDUjGqmS5XiwRlXjcxlnchx+jilQwkYNKvCALkEQeE96MxENjrMhU5yzIjEZ2XS// Ixmk/VyZ2X8bEHbfQTVUZLfhsRd09Cg6YB+DGRzdEg9DOwZKu8LFZyL4aKQ6kARp8q/E7K s7xgK0oepAj3Zz5JzQ2JlunrJeyzsF1NKvHcwiT+gPigTDR5zL4Lx30YIag0Jjh5UHrl1h 5FJbpuejFfR0sNoVwAIxVDjRFQQUByBXTMFc2I+zRAo0G+Fjwid32OV40ZkGMyRq2UHT5v JwkGsAnJJKs71EG5cF1Ifp/VkMHEUgwYXyfMmGtsYhRsRAD7TzYrxUQT38ghaw== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang] parser: parse pointer types Message-ID: <20241009121921.205868-1-carlos@maniero.me> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Wed, 09 Oct 2024 12:19:27 +0000 (UTC) X-CM-Envelope: MS4xfEPZGcfV8J1v9lC/1zL6IlJDUIaEy7un494wrBW2kEl2d/tpJK38c8rbIs4Rpg0QXxJOSLmJorDuPFu4yzlGN0srmQXHsiGKXPMU/o0jZtfGAxgit7io 0kMgOHfGECXfYhZuN4bAg23OK2vc44h1/nS1GAz2BMdMZBAIMCZQ2raN9e/TfQJrPrQ8JNfVrzRkVY9gU3CzL693GnJg8kf+2VBq8vp7NrHFzVXW6fsuE9ki X-CM-Analysis: v=2.4 cv=Z6G+H2RA c=1 sm=1 tr=0 ts=670674cf a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=1qFGK3Qn2huI5GEYduEA:9 a=BXDaF_L80NY05PYiAFlV:22 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.89 X-Spam-Score: -4.89 X-Migadu-Queue-Id: 2902B793B9 X-Migadu-Scanner: mx13.migadu.com X-TUID: 5HhCJ57xN0mr Signed-off-by: Carlos Maniero --- src/checker.c | 2 ++ src/codegen_linux_x86_64.c | 3 ++ src/parser.c | 59 +++++++++++++++++++++++--------------- src/pretty_print_ast.c | 2 +- src/type.c | 14 ++++++++- src/type.h | 19 ++++++++++-- 6 files changed, 72 insertions(+), 27 deletions(-) diff --git a/src/checker.c b/src/checker.c index c688b7a..7a9a7b6 100644 --- a/src/checker.c +++ b/src/checker.c @@ -81,6 +81,8 @@ type_resolve(type_t *type) case TYPE_UNKNOWN: *type = type_from_id(type->as_unknown.id); break; + case TYPE_PTR: + type_resolve(type->as_ptr.type); case TYPE_PRIMITIVE: break; } diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 703f3b5..ac268ad 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -682,6 +682,9 @@ type_to_bytes(type_t *type) case TYPE_PRIMITIVE: { return type->as_primitive.size; } + case TYPE_PTR: { + return 8; + } case TYPE_UNKNOWN: { assert(0 && "cannot calculate size of an unknown type: probably a parser issue."); } diff --git a/src/parser.c b/src/parser.c index 6875b42..0bfa282 100644 --- a/src/parser.c +++ b/src/parser.c @@ -34,8 +34,8 @@ expected_next_token(parser_t *parser, token_t *token, token_kind_t kind); static bool expected_token(token_t *token, token_kind_t kind); -static bool -parser_parse_type(parser_t *parser, string_view_t *type); +static type_t * +parser_parse_type(parser_t *parser); static ast_node_t * parser_parse_block(parser_t *parser); @@ -368,10 +368,13 @@ parser_parse_fn_params(parser_t *parser) return NULL; } - string_view_t type_id; - parser_parse_type(parser, &type_id); + type_t *type = parser_parse_type(parser); - ast_fn_param_t *param = ast_new_fn_param(parser->arena, token.value, type_new_unknown(parser->arena, type_id)); + if (type == NULL) { + return NULL; + } + + ast_fn_param_t *param = ast_new_fn_param(parser->arena, token.value, type); list_append(params, param); skip_line_feeds(parser->lexer); @@ -408,8 +411,9 @@ parser_parse_fn_definition(parser_t *parser) return NULL; } - string_view_t fn_return_type; - if (!parser_parse_type(parser, &fn_return_type)) { + type_t *ret_type = parser_parse_type(parser); + + if (ret_type == NULL) { return NULL; } @@ -420,21 +424,16 @@ parser_parse_fn_definition(parser_t *parser) return NULL; } - return ast_new_node_fn_def(parser->arena, - fn_name_token.loc, - fn_name_token.value, - params, - type_new_unknown(parser->arena, fn_return_type), - block); + return ast_new_node_fn_def(parser->arena, fn_name_token.loc, fn_name_token.value, params, ret_type, block); } -static bool -parser_parse_type(parser_t *parser, string_view_t *type) +static type_t * +parser_parse_type(parser_t *parser) { skip_line_feeds(parser->lexer); if (!skip_expected_token(parser, TOKEN_COLON)) { - return false; + return NULL; } skip_line_feeds(parser->lexer); @@ -442,12 +441,27 @@ parser_parse_type(parser_t *parser, string_view_t *type) token_t token; if (!expected_next_token(parser, &token, TOKEN_ID)) { - return false; + return NULL; } - *type = token.value; + token_t ptr_token; - return true; + lexer_peek_next(parser->lexer, &ptr_token); + + type_t *type = type_new_unknown(parser->arena, token.value); + + if (ptr_token.kind == TOKEN_STAR) { + if (!skip_expected_token(parser, TOKEN_STAR)) { + return NULL; + } + string_view_t ptr_id = token.value; + + ptr_id.size = ptr_token.value.chars - token.value.chars + ptr_token.value.size; + + return type_new_ptr(parser->arena, ptr_id, type); + } + + return type; } static ast_node_t * @@ -642,8 +656,8 @@ parser_parse_var_def(parser_t *parser) return NULL; } - string_view_t var_type; - if (!parser_parse_type(parser, &var_type)) { + type_t *type = parser_parse_type(parser); + if (type == NULL) { return NULL; } @@ -657,8 +671,7 @@ parser_parse_var_def(parser_t *parser) return NULL; } - ast_node_t *var_node = ast_new_node_var_def( - parser->arena, token_id.loc, token_id.value, type_new_unknown(parser->arena, var_type), expr); + ast_node_t *var_node = ast_new_node_var_def(parser->arena, token_id.loc, token_id.value, type, expr); return var_node; } diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c index 5feb270..edc6320 100644 --- a/src/pretty_print_ast.c +++ b/src/pretty_print_ast.c @@ -274,7 +274,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) ast_var_definition_t var = ast->as_var_def; char name[256]; - sprintf(name, "Var_Definition ", SV_ARG(var.id)); + sprintf(name, "Var_Definition ", SV_ARG(var.id), SV_ARG(var.type->id)); node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); diff --git a/src/type.c b/src/type.c index 9de5650..4a8d6f4 100644 --- a/src/type.c +++ b/src/type.c @@ -24,6 +24,18 @@ type_new_unknown(arena_t *arena, string_view_t id) assert(type); type->kind = TYPE_UNKNOWN; - type->as_unknown.id = id; + type->id = id; + return type; +} + +type_t * +type_new_ptr(arena_t *arena, string_view_t id, type_t *ref_type) +{ + type_t *type = arena_alloc(arena, sizeof(type_t)); + assert(type); + + type->kind = TYPE_PTR; + type->id = id; + type->as_ptr.type = ref_type; return type; } diff --git a/src/type.h b/src/type.h index 97a2a41..d930a88 100644 --- a/src/type.h +++ b/src/type.h @@ -18,10 +18,14 @@ #define TYPE_H #include "arena.h" #include "string_view.h" + +typedef union type type_t; + typedef enum { TYPE_UNKNOWN, - TYPE_PRIMITIVE + TYPE_PRIMITIVE, + TYPE_PTR } type_kind_t; typedef enum @@ -46,7 +50,14 @@ typedef struct type_unknown string_view_t id; } type_unknown_t; -typedef union +typedef struct type_ptr +{ + type_kind_t _type_kind; + string_view_t id; + type_t *type; +} type_ptr_t; + +typedef union type { struct { @@ -55,8 +66,12 @@ typedef union }; type_unknown_t as_unknown; type_primitive_t as_primitive; + type_ptr_t as_ptr; } type_t; type_t * type_new_unknown(arena_t *arena, string_view_t id); + +type_t * +type_new_ptr(arena_t *arena, string_view_t id, type_t *type); #endif base-commit: 7f8968454f0aece10c70340f17422d13d14cca79 -- 2.46.0