From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 1/3] scope: create scope data structure
Date: Sat, 21 Sep 2024 02:20:38 +0200 [thread overview]
Message-ID: <20240921002250.902558-2-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240921002250.902558-1-johnny@johnnyrichard.com>
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
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 <https://www.gnu.org/licenses/>.
+ */
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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 <https://www.gnu.org/licenses/>.
+ */
+#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
next prev parent reply other threads:[~2024-09-21 0:22 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-21 0:20 [PATCH olang v1 0/3] compiler: enable full compilation for vars Johnny Richard
2024-09-21 0:20 ` Johnny Richard [this message]
2024-09-21 0:20 ` [PATCH olang v1 2/3] checker: create checker and populate scope on ast nodes Johnny Richard
2024-09-21 0:20 ` [PATCH olang v1 3/3] codegen: add support scopes and symbols lookups for var Johnny Richard
2024-09-21 0:23 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-21 0:59 ` [PATCH olang v1 0/3] compiler: enable full compilation for vars Carlos Maniero
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240921002250.902558-2-johnny@johnnyrichard.com \
--to=johnny@johnnyrichard.com \
--cc=~johnnyrichard/olang-devel@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox