From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.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 iKwSGNMR7maGYQAAe85BDQ:P1 (envelope-from ) for ; Sat, 21 Sep 2024 02:22:43 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id iKwSGNMR7maGYQAAe85BDQ (envelope-from ) for ; Sat, 21 Sep 2024 02:22:43 +0200 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 53137B3EB for ; Sat, 21 Sep 2024 02:22:40 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=UhHNSIKENwlwOOfNlUxTZ87/cUbngAgEpdyh2oOpqeQ=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1726878159; v=1; b=fDjM794gfZMBNkuhTvP44Eb309cGwzm2UIQ2BpQHp6buyE0w7UUy+vz5mOCm2WUedMnZyYYy OkCsjjsmhRwuS0Lc0WTF7kjnl135ZjqRzwh0XXyrWEsOZdXV6oYqchnHYESNFE4ZRbEelKY0Pju FL9UhiP6dSbQrI9dBnGpjUn0oJzyvC3O5duHSe05vnxZCuSVpIl2cdw10mfU8y5DNEVRUUQ2OUL ohaSFYZYNizR26n8H+Zq7aFmim28BFLjkJVHKoGvIwHaoccOfR3AUr6D42SBQFzA5Sr1jo1L33B q2ePWn4y04Cata/aoLfT9H2FaJKj3c3i/CeuSUCedHqjQ== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 8D75E202B6 for ; Sat, 21 Sep 2024 00:22:39 +0000 (UTC) Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) by mail-a.sr.ht (Postfix) with ESMTPS id EE0DD20164 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 21 Sep 2024 00:22:38 +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=1726878158; 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: in-reply-to:in-reply-to:references:references; bh=APaNStw9y43aVyyQ2cIwXTsZ42iMy5oOdoPOKDuswas=; b=tUFuS9ClYyGS2kBmJiwxtPBw5jSEIYZkYyqqK30K5B7OIsp0Gh967SCiVwX++dqPswSprJ bLSBB03bbTxvg4oIQyzrcadxIo/MLdBifISLa8ZmkFxaJkDBLX1p1Z1FeX3+uiuXCehcex 7c2ge+oHFWx87ghHG3xwfyicfnAammXrz1JzRdh3wDkVVt7EQBkVQ2WHzrKhB7Fvn1eSU7 AWC2IYadLa23Hz/wkl2huUAytg/HqYMUP7MNzJIjDUj+kyIfV3GJaBF0kbOlHe2Itxvgee J6ORNoI+FxH2zu8wElTb2Vg/DEURo6hkL00tpeJU0ODeJ9WxRudJ6k8er7ca4w== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 1/3] scope: create scope data structure Date: Sat, 21 Sep 2024 02:20:38 +0200 Message-ID: <20240921002250.902558-2-johnny@johnnyrichard.com> In-Reply-To: <20240921002250.902558-1-johnny@johnnyrichard.com> References: <20240921002250.902558-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Sourcehut-Patchset-Status: UNKNOWN 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-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Scanner: mx13.migadu.com X-Migadu-Queue-Id: 53137B3EB X-TUID: I3FnSOIPR3f2 Signed-off-by: Johnny Richard --- src/scope.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/scope.h | 54 ++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 src/scope.c create mode 100644 src/scope.h diff --git a/src/scope.c b/src/scope.c new file mode 100644 index 0000000..3271856 --- /dev/null +++ b/src/scope.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * 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 . + */ +#include +#include +#include +#include +#include + +#include "scope.h" + +scope_t * +scope_new(arena_t *arena) +{ + assert(arena); + scope_t *scope = (scope_t *)arena_alloc(arena, sizeof(scope_t)); + if (scope == NULL) { + fprintf(stderr, "[FATAL] Out of memory: scope_new: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + scope->arena = arena; + scope->symbols = map_new(arena); + return scope; +} + +symbol_t * +symbol_new(arena_t *arena, string_view_t id) +{ + assert(arena); + symbol_t *symbol = (symbol_t *)arena_alloc(arena, sizeof(symbol_t)); + if (symbol == NULL) { + fprintf(stderr, "[FATAL] Out of memory: symbol_new: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + symbol->id = id; + return symbol; +} + +symbol_t * +scope_lookup(scope_t *scope, string_view_t id) +{ + assert(scope); + while (scope != NULL) { + + char cstr_id[id.size + 1]; + cstr_id[id.size] = 0; + memcpy(cstr_id, id.chars, id.size); + + symbol_t *symbol = (symbol_t *)map_get(scope->symbols, cstr_id); + if (symbol != NULL) { + return symbol; + } + scope = scope->parent; + } + return NULL; +} + +void +scope_insert(scope_t *scope, symbol_t *symbol) +{ + assert(scope); + assert(symbol); + + char id[symbol->id.size + 1]; + id[symbol->id.size] = 0; + memcpy(id, symbol->id.chars, symbol->id.size); + + map_put(scope->symbols, id, symbol); +} + +scope_t * +scope_push(scope_t *scope) +{ + assert(scope); + + scope_t *child = scope_new(scope->arena); + child->parent = scope; + + return child; +} + +scope_t * +scope_pop(scope_t *scope) +{ + assert(scope); + assert(scope->parent); + return scope->parent; +} diff --git a/src/scope.h b/src/scope.h new file mode 100644 index 0000000..ddb0cd0 --- /dev/null +++ b/src/scope.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * 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 . + */ +#ifndef SCOPE_H +#define SCOPE_H + +#include "arena.h" +#include "map.h" +#include "string_view.h" + +typedef struct symbol +{ + string_view_t id; +} symbol_t; + +typedef struct scope +{ + struct scope *parent; + arena_t *arena; + map_t *symbols; +} scope_t; + +scope_t * +scope_new(arena_t *arena); + +symbol_t * +symbol_new(arena_t *arena, string_view_t id); + +symbol_t * +scope_lookup(scope_t *scope, string_view_t id); + +void +scope_insert(scope_t *scope, symbol_t *symbol); + +scope_t * +scope_push(scope_t *scope); + +scope_t * +scope_pop(scope_t *scope); + +#endif /* SCOPE_H */ -- 2.46.0