Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
symbols.c File Reference
Include dependency graph for symbols.c:

Go to the source code of this file.

Functions

srn_value_tsrn_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.
 
srn_value_tsrn_symbol_intern (srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *symbol_name)
 
srn_value_tsrn_symbol_gen (srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *optional_name)
 

Function Documentation

◆ srn_symbol_gen()

srn_value_t * srn_symbol_gen ( srn_context_t * ctx,
srn_metadata_t * metadata,
srn_namespace_t * ns,
srn_string_t * optional_name )

Definition at line 72 of file symbols.c.

73 {
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 AS_STRING(value_ref)
Definition core.h:171
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
Definition engine.c:137
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
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
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
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
#define PANIC_IF(cond, msg)
Definition utils.h:57
#define UNUSED(x)
Definition utils.h:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_symbol_intern()

srn_value_t * srn_symbol_intern ( srn_context_t * ctx,
srn_metadata_t * metadata,
srn_namespace_t * ns,
srn_string_t * symbol_name )

Definition at line 43 of file symbols.c.

44 {
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}
@ 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
bool srn_string_is_empty(const srn_string_t *s)
Definition strings.c:48
Note: For key equality we use the memcpy function.
Definition hashmap.h:66
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_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
static void srn_spinlock_lock(srn_spinlock_t *lock)
Definition utils.h:286
static void srn_spinlock_unlock(srn_spinlock_t *lock)
Definition utils.h:277
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_symbol_make()

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 at line 29 of file symbols.c.

30 {
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}
#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
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_namespace_t * ns
Definition symbols.h:32
srn_string_t * name
Definition symbols.h:31
Here is the call graph for this function:
Here is the caller graph for this function: