From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.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 sL1QI+dnBmcffQEAqHPOHw:P1 (envelope-from ) for ; Wed, 09 Oct 2024 13:24:23 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id sL1QI+dnBmcffQEAqHPOHw (envelope-from ) for ; Wed, 09 Oct 2024 13:24:23 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=DnPKGSY1; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=qdg9jleA; 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=none) header.from=maniero.me; 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=1728473063; 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=VqLaKRB8venLjTZs/DlhW0NvK5ow9bzBOkf/MrECL+k=; b=CH9V7pvycEXMdXtXqq0/FAlmmbgOzd+MG8nvadQFeKzR/zuy6I+q2YCdXBfexc3hwn8b0J 3E1lPX3hF7W3HNxdasKxhcS4ZJKhHADK2RWoybnqDtzupUH5ID1yfD+Curij9WxHzb3Slq dlmjbPHaly5/JMP97AxxUSsHglKxrIt123nLUYwOYPPCY516ur+u/qosKkWAhjV2pPvebs vQf8Ta2yUbqXDXpUMGOI7cOjs023q5kl/o0Bj9gwh+r1pRoz+F9RtPrTWBolwvJZphaSuM TeOD762MSp1+rYUOV07ElKyf6jgwAHkwAh5bK+p+w/G42avdRz+icYYR6NIguA== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=DnPKGSY1; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=qdg9jleA; 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=none) header.from=maniero.me; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1728473063; a=rsa-sha256; cv=pass; b=uVvkZkf0LARWFFIo+BKqIsj4IVia3WYTuf03BCu621wCLU8whZ9k4NggLGWHTh5mNbuBC3 YMG/rvSnUkYgrbXN+b9Of/R6DWFmVXJJZIKD+3a5IEdPZHc1tDiwTKyq2i7xjiLcrqdDWh kIjDdyYqmO8ShY81rkbCt6f6GXg534GUG6Eqr1/bc1sz8vERBt/Eh2pCIRZm6p3TLvbY9J tu9Ih1ocFvu8imBv0mwyNbWRt8xRomZfdZvH8dmf0CHI0Yz8uMutERmMLbnKmpcB8Q/GyU smHf4dhC302oCH4X6UIEsEgZeINMpFDcTdUs22Z67TTE0JviJr4iruQp9X2Bgg== 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 29731858E9 for ; Wed, 09 Oct 2024 13:24:23 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=VXJYJ+Q2W0Ji8JeUVwN+8yvXyECxF3d0PR15um1CUtA=; 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=1728473062; v=1; b=DnPKGSY1eu4z44ZvZCw702hGeiAQx2Ik+T5LoXfhn1ZuIZwIcCbkXRxesyNHDj+1HW3SXOxI +5XK4m0SxeIJ9VNdaLeZ4ZdcjM1DRPmR0DvIJ4pAQDp3lLD4OnZMKC93P+0BtK1SEK/lTjqRqoI KOYkOKHFG+AUQJRspF83Q+cvUNkB3NLrAHPRX/zsaIE4+l36TjljTwL6g6K3DDKXqWvbG56WToI IElXErwjn1INjIR/o5hgr7zxF2MqVk+r9Q/DuZYdhmu1/wMGtW1zhhuDGRncEN2+mgkfsYBpaKo t5iSJiDPLjf0Rqrs4aaxdo6SBvKoxmTMqIxyQ5diu+mVA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 85B9D2025C for ; Wed, 09 Oct 2024 11:24:22 +0000 (UTC) Received: from cross.elm.relay.mailchannels.net (cross.elm.relay.mailchannels.net [23.83.212.46]) by mail-a.sr.ht (Postfix) with ESMTPS id 5BD7F20219 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 09 Oct 2024 11:24:21 +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 45E0C83D60 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 9 Oct 2024 11:24:19 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (100-96-80-146.trex-nlb.outbound.svc.cluster.local [100.96.80.146]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 8F34782D84 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 9 Oct 2024 11:24:18 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1728473059; a=rsa-sha256; cv=none; b=vHkSGW+fSCgcSQwzpOwPdn7erBFD9KVlYj6Agu3BV3SzoQ6GzsoTCzpNNaBfhPItHNX98G 0S0FYd+bZ0PzDiT/8F2j5QSHKv1jj2ygcADKMb6mrkA7RKtezPWQui9h1ZSZ+WmDa949lK KjAzQHJNX+zCJpQtfHcHsFlSrA/6lROHfO1+VJNaWdUGa3dlwE2w/PEXJ78o9Y+D0wr/tv Fgr2Sy5A5gD3Jiz9PWLvSgIV2PWiJH3760AYora945HRh9BkIwSQHlfd4ahcWYe0+wd4PM u3WTe/5Bj2XT9DhRIYmBSODx2JB/MH29TO2sgnJrGYTZOMyxRK2YltPW8xmaGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1728473058; 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=VqLaKRB8venLjTZs/DlhW0NvK5ow9bzBOkf/MrECL+k=; b=dsKWZgR81z9VZGRlTa2HxDbwG+szNSKsw9YGF9X2S2jdwkVNwnwXnQk+SPe6T/wLvaWOjE mkX65VOMVtv/uoxJOaSyGfr5ip2DAfyG3I50Hf0pi1TxkLRA0uTZrq9zgnJ6efQ5Dh4bQW nY5TaaU1SgRIUAh0qt6t2n96tM7TU20FQqVYH8dxdRnN7jdC+f37NGFqtNz8Uh1INzfW7R 8+6ZLtnUt1BKfCQ2+1NvGpEM3c2Kw51x0a8BWrd+8R2mc5ca49AlQY8LEXZy/xoiyr+l78 MBOiZlsoLUkEC/yrv8ArjGiANyPc2d/RyrEZhBeU8ZJBjdH7verTN5ZWewU3Rw== ARC-Authentication-Results: i=1; rspamd-b4cd7f459-5vzgk; 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-Suffer-Unite: 658411e43174ec59_1728473059185_981678014 X-MC-Loop-Signature: 1728473059185:708281876 X-MC-Ingress-Time: 1728473059185 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.80.146 (trex/7.0.2); Wed, 09 Oct 2024 11:24:19 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1728473056; 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=VqLaKRB8venLjTZs/DlhW0NvK5ow9bzBOkf/MrECL+k=; b=qdg9jleAJChiikDPCligOB6GiTi2YJdKEWjlmztXLAy/xFbNewl//XDwgMdB9lMEJhxDXm AmQ2uNCkrxQ7RLVKQWqHfcUnPN9GJOdS0JT8MFbUMI4qUQ7m+4OLGEPBUMy+pKXSwfOEPG SftvpET27ZmQXRYBKu1ZQEW7BsB1XdzBFZi34TXbv6aZRtE9MAAm7BFM9EgvRhdz9TlvMr M+G0m4LVvR5TkMIdG25g9hHT0gG+7Ln8HGznH88u8V6qsGXxHSAb0rCPSwwg+rfYcz7+N3 Onxi30uuJ0GJ6duY7bcV77fOtu2SnsUgzr02MkUITmadGvKn1FFMRFdQtkDq6Q== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang] parser: returns an unknown type instead of a SV Message-ID: <20241009112410.142426-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 11:24:16 +0000 (UTC) X-CM-Envelope: MS4xfJsWtW8OkS4aY/Sk733XOxpyPIRShQ7MJrOCr2TCYPfH3PEJTVVgX09njKiCLl/3ruiaSNomnviUpIUItve8JajDt+8FgM8YCyCn1U/l2Q+qxTeg460L LmL1svz7yl2QDKV/J0wGPpsT2eDwyQVRh8GTHrcyzIBDmnppJAtDK0ngMihV0g7Cp4+bdRL0hcljZQ+s5n+u1N5xhCM3tsa4dGevN2bSeNmtnYNVesPS+Wua X-CM-Analysis: v=2.4 cv=bZY5U/PB c=1 sm=1 tr=0 ts=670667e0 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=7gCqH15QqibmWVgCW9QA: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-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx11.migadu.com X-Migadu-Spam-Score: -1.61 X-Spam-Score: -1.61 X-Migadu-Queue-Id: 29731858E9 X-TUID: Yc0HvlwWG41K To support pointer, it is more convenient for the AST to have an *type_t* structure instead of an string view, once the type_t can support multiple shapes, such as pointers. The parser is still not resolving the type which is a checker responsibility. However, it returns a unknown type which is resolved by the checker. Signed-off-by: Carlos Maniero --- src/ast.c | 8 +++--- src/ast.h | 12 ++++---- src/checker.c | 59 ++++++++++++++++++++++++++++++++++++-- src/codegen_linux_x86_64.c | 16 +++++++---- src/parser.c | 12 ++++++-- src/pretty_print_ast.c | 4 +-- src/scope.c | 2 +- src/scope.h | 4 +-- src/type.c | 36 +++++------------------ src/type.h | 25 +++++++++++----- 10 files changed, 115 insertions(+), 63 deletions(-) diff --git a/src/ast.c b/src/ast.c index d6ec70c..79daca6 100644 --- a/src/ast.c +++ b/src/ast.c @@ -44,7 +44,7 @@ ast_new_node_fn_def(arena_t *arena, token_loc_t loc, string_view_t id, list_t *params, - string_view_t return_type, + type_t *return_type, ast_node_t *block) { assert(arena); @@ -86,7 +86,7 @@ ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t * } ast_node_t * -ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, string_view_t type, ast_node_t *value) +ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *type, ast_node_t *value) { ast_node_t *node_var_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_var_def); @@ -217,13 +217,13 @@ ast_new_node_block(arena_t *arena) } ast_fn_param_t * -ast_new_fn_param(arena_t *arena, string_view_t id, string_view_t type_id) +ast_new_fn_param(arena_t *arena, string_view_t id, type_t *type) { ast_fn_param_t *fn_param = (ast_fn_param_t *)arena_alloc(arena, sizeof(ast_fn_param_t)); assert(fn_param); fn_param->id = id; - fn_param->type_id = type_id; + fn_param->type = type; return fn_param; } diff --git a/src/ast.h b/src/ast.h index 238435e..fccc303 100644 --- a/src/ast.h +++ b/src/ast.h @@ -66,7 +66,7 @@ typedef struct ast_translation_unit typedef struct ast_fn_param { string_view_t id; - string_view_t type_id; + type_t *type; } ast_fn_param_t; typedef struct ast_fn_definition @@ -74,7 +74,7 @@ typedef struct ast_fn_definition ast_node_meta_t meta; string_view_t id; list_t *params; - string_view_t return_type; + type_t *return_type; ast_node_t *block; scope_t *scope; } ast_fn_definition_t; @@ -91,7 +91,7 @@ typedef struct ast_var_definition { ast_node_meta_t meta; string_view_t id; - string_view_t type; + type_t *type; ast_node_t *value; scope_t *scope; } ast_var_definition_t; @@ -206,14 +206,14 @@ ast_new_node_fn_def(arena_t *arena, token_loc_t loc, string_view_t id, list_t *params, - string_view_t return_type, + type_t *return_type, ast_node_t *block); ast_node_t * ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *args); ast_node_t * -ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, string_view_t type, ast_node_t *value); +ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *type, ast_node_t *value); ast_node_t * ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs); @@ -240,6 +240,6 @@ ast_node_t * ast_new_node_block(arena_t *arena); ast_fn_param_t * -ast_new_fn_param(arena_t *arena, string_view_t id, string_view_t type_id); +ast_new_fn_param(arena_t *arena, string_view_t id, type_t *type); #endif /* AST_H */ diff --git a/src/checker.c b/src/checker.c index 36202bd..c688b7a 100644 --- a/src/checker.c +++ b/src/checker.c @@ -37,6 +37,55 @@ checker_new(arena_t *arena) return checker; } +static type_t +type_from_id(string_view_t id) +{ + type_t type = { 0 }; + type.id = id; + if (string_view_eq_to_cstr(id, "u8")) { + type.kind = TYPE_PRIMITIVE; + type.as_primitive.size = 1; + type.as_primitive.kind = TYPE_U8; + return type; + } + if (string_view_eq_to_cstr(id, "u16")) { + type.kind = TYPE_PRIMITIVE; + type.as_primitive.size = 2; + type.as_primitive.kind = TYPE_U16; + return type; + } + if (string_view_eq_to_cstr(id, "u32")) { + type.kind = TYPE_PRIMITIVE; + type.as_primitive.size = 4; + type.as_primitive.kind = TYPE_U32; + return type; + } + if (string_view_eq_to_cstr(id, "u64")) { + type.kind = TYPE_PRIMITIVE; + type.as_primitive.size = 8; + type.as_primitive.kind = TYPE_U64; + return type; + } + + // FIXME: handle user defined types + assert(0 && "unknown type"); +} + +/** + * transform unknown types into actual types + */ +static void +type_resolve(type_t *type) +{ + switch (type->kind) { + case TYPE_UNKNOWN: + *type = type_from_id(type->as_unknown.id); + break; + case TYPE_PRIMITIVE: + break; + } +} + void checker_check(checker_t *checker, ast_node_t *ast) { @@ -67,7 +116,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) ast_fn_definition_t *fn_def = &ast->as_fn_def; fn_def->scope = scope_push(scope); - symbol_t *symbol = symbol_new(checker->arena, fn_def->id, type_from_id(fn_def->return_type)); + type_resolve(fn_def->return_type); + symbol_t *symbol = symbol_new(checker->arena, fn_def->id, fn_def->return_type); scope_insert(scope, symbol); list_item_t *item = list_head(fn_def->params); @@ -75,7 +125,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) while (item != NULL) { ast_fn_param_t *param = (ast_fn_param_t *)item->value; - symbol_t *symbol = symbol_new(checker->arena, param->id, type_from_id(param->type_id)); + type_resolve(param->type); + symbol_t *symbol = symbol_new(checker->arena, param->id, param->type); scope_insert(fn_def->scope, symbol); item = list_next(item); @@ -156,7 +207,9 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) case AST_NODE_VAR_DEF: { string_view_t id = ast->as_var_def.id; - symbol_t *symbol = symbol_new(checker->arena, id, type_from_id(ast->as_var_def.type)); + type_resolve(ast->as_var_def.type); + + symbol_t *symbol = symbol_new(checker->arena, id, ast->as_var_def.type); scope_insert(scope, symbol); ast->as_var_def.scope = scope; diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 10005b9..703f3b5 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -165,7 +165,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr size_t offset = codegen_linux_x86_64_get_stack_offset(codegen, symbol); - size_t bytes = type_to_bytes(&symbol->type); + size_t bytes = type_to_bytes(symbol->type); fprintf(codegen->out, " mov -%ld(%%rbp), %s\n", offset, get_reg_for(REG_ACCUMULATOR, bytes)); return bytes; @@ -195,7 +195,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr fprintf(codegen->out, " call " SV_FMT "\n", SV_ARG(fn_call.id)); - return type_to_bytes(&symbol->type); + return type_to_bytes(symbol->type); } case AST_NODE_BINARY_OP: { ast_binary_op_t bin_op = expr_node->as_bin_op; @@ -570,7 +570,7 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) codegen_linux_x86_64_emit_expression(codegen, var_def.value); } - size_t type_size = type_to_bytes(&symbol->type); + size_t type_size = type_to_bytes(symbol->type); fprintf(codegen->out, " mov %s, -%ld(%%rbp)\n", @@ -593,7 +593,7 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) codegen_linux_x86_64_emit_expression(codegen, var_assign.expr); - size_t type_size = type_to_bytes(&symbol->type); + size_t type_size = type_to_bytes(symbol->type); fprintf(codegen->out, " mov %s, -%ld(%%rbp)\n", get_reg_for(REG_ACCUMULATOR, type_size), offset); break; @@ -682,6 +682,9 @@ type_to_bytes(type_t *type) case TYPE_PRIMITIVE: { return type->as_primitive.size; } + case TYPE_UNKNOWN: { + assert(0 && "cannot calculate size of an unknown type: probably a parser issue."); + } } assert(0 && "unreachable"); @@ -702,7 +705,7 @@ calculate_fn_local_size(scope_t *scope) for (size_t i = 0; i < scope->symbols->size; ++i) { symbol_t *symbol = (symbol_t *)kvs[i]->value; - local_size += type_to_bytes(&symbol->type); + local_size += type_to_bytes(symbol->type); } size_t max_child_local_size = 0; @@ -748,7 +751,8 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ fprintf(codegen->out, " mov %s, -%ld(%%rbp)\n", - get_reg_for(x86_call_args[i], symbol->type.as_primitive.size), + // FIXME: Type may not be an as_primitive + get_reg_for(x86_call_args[i], symbol->type->as_primitive.size), offset); // FIXME: add offset according to the param size diff --git a/src/parser.c b/src/parser.c index 5d6290b..6875b42 100644 --- a/src/parser.c +++ b/src/parser.c @@ -371,7 +371,7 @@ parser_parse_fn_params(parser_t *parser) string_view_t type_id; parser_parse_type(parser, &type_id); - ast_fn_param_t *param = ast_new_fn_param(parser->arena, token.value, type_id); + ast_fn_param_t *param = ast_new_fn_param(parser->arena, token.value, type_new_unknown(parser->arena, type_id)); list_append(params, param); skip_line_feeds(parser->lexer); @@ -420,7 +420,12 @@ 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, fn_return_type, block); + 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); } static bool @@ -652,7 +657,8 @@ 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, var_type, expr); + 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); return var_node; } diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c index 0c5d2d5..5feb270 100644 --- a/src/pretty_print_ast.c +++ b/src/pretty_print_ast.c @@ -113,7 +113,7 @@ pretty_print_new_fn_param(ast_fn_param_t *param, arena_t *arena) { pretty_print_node_t *node = pretty_print_node_new(arena); char name[256]; - sprintf(name, "Param_Definition ", SV_ARG(param->id), SV_ARG(param->type_id)); + sprintf(name, "Param_Definition ", SV_ARG(param->id), SV_ARG(param->type->id)); node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); return node; @@ -148,7 +148,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) sprintf(name, "Function_Definition ", SV_ARG(fn_def.id), - SV_ARG(fn_def.return_type)); + SV_ARG(fn_def.return_type->id)); node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); diff --git a/src/scope.c b/src/scope.c index 81f610a..e483fbe 100644 --- a/src/scope.c +++ b/src/scope.c @@ -49,7 +49,7 @@ scope_new(arena_t *arena) } symbol_t * -symbol_new(arena_t *arena, string_view_t id, type_t type) +symbol_new(arena_t *arena, string_view_t id, type_t *type) { assert(arena); symbol_t *symbol = (symbol_t *)arena_alloc(arena, sizeof(symbol_t)); diff --git a/src/scope.h b/src/scope.h index 92a5a4f..08eb681 100644 --- a/src/scope.h +++ b/src/scope.h @@ -26,7 +26,7 @@ typedef struct symbol { string_view_t id; - type_t type; + type_t *type; } symbol_t; typedef struct scope @@ -41,7 +41,7 @@ scope_t * scope_new(arena_t *arena); symbol_t * -symbol_new(arena_t *arena, string_view_t id, type_t type); +symbol_new(arena_t *arena, string_view_t id, type_t *type); symbol_t * scope_lookup(scope_t *scope, string_view_t id); diff --git a/src/type.c b/src/type.c index 64147a2..9de5650 100644 --- a/src/type.c +++ b/src/type.c @@ -17,35 +17,13 @@ #include "type.h" #include "assert.h" -type_t -type_from_id(string_view_t id) +type_t * +type_new_unknown(arena_t *arena, string_view_t id) { - type_t type = { 0 }; - if (string_view_eq_to_cstr(id, "u8")) { - type.kind = TYPE_PRIMITIVE; - type.as_primitive.size = 1; - type.as_primitive.kind = TYPE_U8; - return type; - } - if (string_view_eq_to_cstr(id, "u16")) { - type.kind = TYPE_PRIMITIVE; - type.as_primitive.size = 2; - type.as_primitive.kind = TYPE_U16; - return type; - } - if (string_view_eq_to_cstr(id, "u32")) { - type.kind = TYPE_PRIMITIVE; - type.as_primitive.size = 4; - type.as_primitive.kind = TYPE_U32; - return type; - } - if (string_view_eq_to_cstr(id, "u64")) { - type.kind = TYPE_PRIMITIVE; - type.as_primitive.size = 8; - type.as_primitive.kind = TYPE_U64; - return type; - } + type_t *type = arena_alloc(arena, sizeof(type_t)); + assert(type); - // FIXME: handle user defined types - assert(0 && "unknown type"); + type->kind = TYPE_UNKNOWN; + type->as_unknown.id = id; + return type; } diff --git a/src/type.h b/src/type.h index 1da3a11..97a2a41 100644 --- a/src/type.h +++ b/src/type.h @@ -16,9 +16,11 @@ */ #ifndef TYPE_H #define TYPE_H +#include "arena.h" #include "string_view.h" typedef enum { + TYPE_UNKNOWN, TYPE_PRIMITIVE } type_kind_t; @@ -32,20 +34,29 @@ typedef enum typedef struct type_primitive { - short size; + type_kind_t _type_kind; + string_view_t id; type_primitive_kind_t kind; + short size; } type_primitive_t; -typedef struct type +typedef struct type_unknown { + type_kind_t _type_kind; string_view_t id; - type_kind_t kind; - union +} type_unknown_t; + +typedef union +{ + struct { - type_primitive_t as_primitive; + type_kind_t kind; + string_view_t id; }; + type_unknown_t as_unknown; + type_primitive_t as_primitive; } type_t; -type_t -type_from_id(string_view_t id); +type_t * +type_new_unknown(arena_t *arena, string_view_t id); #endif base-commit: 2407c8150403c373df86e66b5886102c9f143193 -- 2.46.0