Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
symbols.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 */
18
19#include "serene/rt/symbols.h"
20
21#include "serene/rt/context.h"
22#include "serene/rt/engine.h"
23#include "serene/rt/errors.h"
26#include "serene/rt/strings.h"
27#include "serene/utils.h"
28
30 srn_string_t *name) {
31 PANIC_IF_NULL(ctx);
32 PANIC_IF_NULL(ns);
33 PANIC_IF_NULL(name);
34
35 srn_symbol_t *sym = ALLOC(ctx, srn_symbol_t);
36 sym->name = srn_string_copy(ctx, name);
37
38 sym->ns = ns;
39
40 return srn_value_make(ctx, VSymbol, metadata, (void *)sym);
41}
42
44 srn_string_t *symbol_name) {
45 PANIC_IF_NULL(ctx);
46 PANIC_IF_NULL(ns);
47 PANIC_IF_NULL(symbol_name);
48
49 if (srn_string_is_empty(symbol_name)) {
50 return srn_errors_make_error(ctx, metadata, EMPTY_SYMBOL_NAME, nullptr);
51 };
52
53 hmap_key_t *lookup_key = hmap_make_key(ctx, &symbol_name->buffer, symbol_name->size);
54
55 void *result = hmap_lookup(ctx, &ns->sym_table, lookup_key, nullptr);
56
57 if (result != nullptr) {
58 // found the symbol, we just have to return it.
59 return (srn_value_t *)result;
60 }
61
63 // Couldn't find the symbol in the sym table, so it's a new symbol, insert it
64 // in the table and return it
65 srn_value_t *sym = srn_symbol_make(ctx, metadata, ns, symbol_name);
66
67 ns->sym_table = hmap_insert(ctx, &ns->sym_table, lookup_key, (void *)sym);
69 return sym;
70}
71
73 srn_string_t *optional_name) {
74 // We don't use the optional name for now
75 UNUSED(optional_name);
76 PANIC_IF_NULL(ctx);
78
79 // Calculate the length
80 int len = snprintf(nullptr, 0, "gensym_%" PRIu64, id);
81 PANIC_IF(len < 0, "This should never happen!");
82
83 // Including the null terminator
84 char *name = srn_allocate(ctx, len + 1, alignof(char));
85 (void)snprintf(name, len + 1, "gensym_%" PRIu64, id);
86
87 srn_value_t *sym_name = srn_string_make(ctx, metadata, name);
88 return srn_symbol_intern(ctx, metadata, ns, AS_STRING(sym_name));
89}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:72
uintptr_t object_id_t
Definition context.h:39
#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
@ VSymbol
Definition core.h:124
#define AS_STRING(value_ref)
Definition core.h:171
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
Definition engine.c:137
@ EMPTY_SYMBOL_NAME
Definition errors.h:51
void * hmap_lookup(srn_context_t *ctx, const hmap_t *hmap, const hmap_key_t *k, void *default_value)
Lookup the given k in the given hmap and return the value if it's been found.
Definition hashmap.c:622
hmap_t hmap_insert(srn_context_t *ctx, const hmap_t *hmap, hmap_key_t *k, void *v)
Insert the given key k with the value v in the given hash hmap and return the new map.
Definition hashmap.c:618
hmap_key_t * hmap_make_key(srn_context_t *ctx, void *data, size_t len)
Create a new key out of the given data, with the given len.
Definition hashmap.c:627
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
srn_value_t * srn_string_make(srn_context_t *ctx, srn_metadata_t *metadata, const char *src)
Create a string from a null terminated C string.
Definition strings.c:50
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
Note: For key equality we use the memcpy function.
Definition hashmap.h:66
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
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
size_t size
Size of the buffer.
Definition strings.h:35
uint8_t buffer[]
The buffer that holds the WTF8 sequence.
Definition strings.h:39
srn_namespace_t * ns
Definition symbols.h:32
srn_string_t * name
Definition symbols.h:31
srn_value_t * srn_symbol_make(srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *name)
Create a new symbol. IT DOES NOT INTERNALIZE THE SYMBOL.
Definition symbols.c:29
srn_value_t * srn_symbol_intern(srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *symbol_name)
Definition symbols.c:43
srn_value_t * srn_symbol_gen(srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *optional_name)
Definition symbols.c:72
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
static void srn_spinlock_lock(srn_spinlock_t *lock)
Definition utils.h:286
#define PANIC_IF(cond, msg)
Definition utils.h:57
static void srn_spinlock_unlock(srn_spinlock_t *lock)
Definition utils.h:277
#define UNUSED(x)
Definition utils.h:43