Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
namespaces.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
19
20#include "serene/rt/core.h"
21#include "serene/rt/errors.h"
23#include "serene/rt/strings.h"
24#include "serene/rt/symbols.h"
25#include "serene/utils.h"
26
28 PANIC_IF_NULL(ctx);
29 PANIC_IF_NULL(name);
30
31 // Empty string is not a valid namespace name
32 if (srn_string_is_empty(name)) {
33 return srn_errors_make_error(ctx, metadata, EMPTY_NAMESPACE_NAME, nullptr);
34 };
35
37 ns->name = srn_string_copy(ctx, name);
38
41 ns->sym_table = hmap_empty(ctx);
42 ns->src_buffer = nullptr;
43
46
47 return srn_value_make(ctx, VNamespace, metadata, (void *)ns);
48}
49
51 PANIC_IF_NULL(ns);
52 return nullptr;
53}
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
Definition context.c:38
#define ALLOC(ctx, T)
Definition context.h:82
srn_value_t * srn_value_make(srn_context_t *ctx, srn_value_tag_t tag, srn_metadata_t *metadata, void *payload)
Creates a new serene value.
Definition core.c:24
@ VNamespace
Definition core.h:122
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
Definition engine.c:137
@ EMPTY_NAMESPACE_NAME
Definition errors.h:50
hmap_t hmap_empty(srn_context_t *ctx)
Create, initialize and return a new hashmap.
Definition hashmap.c:612
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
srn_namespace_t * srn_namespace_load(srn_namespace_t *ns)
Load the namespace from the filesystem by looking up the file in the filesystem, load the file into t...
Definition namespaces.c:50
srn_value_t * srn_namespace_make(srn_context_t *ctx, srn_metadata_t *metadata, srn_string_t *name)
Creates a new namespace in the give context.
Definition namespaces.c:27
bool srn_string_is_empty(const srn_string_t *s)
Definition strings.c:48
srn_string_t * srn_string_copy(srn_context_t *ctx, const srn_string_t *src)
Copy the src string to dst string.
Definition strings.c:38
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
srn_string_t * name
Definition namespaces.h:49
srn_context_t * root_context
Definition namespaces.h:52
srn_spinlock_t src_buffer_lock
Guards every access to source buffer.
Definition namespaces.h:66
srn_namespace_src_buffer_t * src_buffer
The buffer holding the source code for this namespace.
Definition namespaces.h:63
hmap_t sym_table
A mapping symbol names to (srn_value_t with type symbol).
Definition namespaces.h:57
srn_spinlock_t sym_table_lock
Definition namespaces.h:58
srn_object_id_t id
Definition namespaces.h:50
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
static void srn_spinlock_init(srn_spinlock_t *lock)
Definition utils.h:281