Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
core.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/core.h"
20
21#include "serene/rt/context.h"
22#include "serene/utils.h"
23
24// The shared sentinels declared `extern` in core.h. One instance each, so their
25// addresses are stable across translation units.
26srn_metadata_t absurd_metadata = {nullptr, nullptr};
30
31// TODO(lxsameer): the tag-only values handed back here (nil_v/true_v/false_v)
32// are shared sentinels that must never be mutated. Make srn_value_make -- and
33// the value API it feeds -- return `const srn_value_t *` so the compiler
34// enforces that, instead of relying on callers to leave them alone.
36srn_value_make(srn_context_t *ctx, srn_value_tag_t tag, srn_metadata_t *metadata, void *payload) {
37 PANIC_IF_NULL(ctx);
38
39 switch (tag) {
40 case VNil:
41 return &nil_v;
42 case VTrue:
43 return &true_v;
44 case VFalse:
45 return &false_v;
46 default:
47 break;
48 }
49
50 srn_value_t *v = ALLOC(ctx, srn_value_t);
51 v->type = tag;
52 v->metadata = metadata;
53
54 // At this point payload should not be null
55 PANIC_IF_NULL(payload);
56
57 switch (tag) {
58 case VI64:
59 v->as.i64 = *(int64_t *)payload;
60 break;
61 case VF64:
62 v->as.f64 = *(double *)payload;
63 break;
64 case VList:
65 v->as.list = (srn_list_t *)payload;
66 break;
67 case VSeq:
68 v->as.seq = (srn_seq_t *)payload;
69 break;
70 case VQuote:
71 PANIC("Not implemented");
72 case VClosure:
73 PANIC("Not implemented");
74 case VNamespace:
75 v->as.ns = (srn_namespace_t *)payload;
76 break;
77 case VString:
78 v->as.string = (srn_string_t *)payload;
79 break;
80 case VSymbol:
81 v->as.symbol = (srn_symbol_t *)payload;
82 break;
83 case VKeyword:
84 v->as.keyword = (srn_keyword_t *)payload;
85 break;
86 case VMap:
87 v->as.map = (srn_map_t *)payload;
88 break;
89 case VError:
90 v->as.error = (srn_error_t *)payload;
91 break;
92 default:
93 PANIC("Should never happen");
94 }
95 return v;
96}
#define ALLOC(ctx, T)
Definition context.h:84
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:36
srn_metadata_t absurd_metadata
We use this for testing. Defined once in core.c.
Definition core.c:26
srn_value_t false_v
Definition core.c:29
srn_value_t true_v
Definition core.c:28
srn_value_t nil_v
Definition core.c:27
@ VQuote
Definition core.h:120
@ VSeq
Definition core.h:119
@ VNamespace
Definition core.h:122
@ VClosure
Definition core.h:121
@ VNil
Definition core.h:113
@ VList
Definition core.h:118
@ VFalse
Definition core.h:115
@ VF64
Definition core.h:117
@ VSymbol
Definition core.h:124
@ VI64
Definition core.h:116
@ VMap
Definition core.h:126
@ VError
VError should be last.
Definition core.h:128
@ VKeyword
Definition core.h:125
@ VTrue
Definition core.h:114
@ VString
Definition core.h:123
enum srn_value_tag_e srn_value_tag_t
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
A keyword, just a name.
Definition keywords.h:29
A persistent, immutable map.
Definition maps.h:29
A persistent, immutable, indexed sequence.
Definition seqs.h:27
srn_metadata_t * metadata
Definition core.h:133
srn_namespace_t * ns
Definition core.h:141
srn_list_t * list
Definition core.h:145
int64_t i64
We represent all the immidiate numbers with this field and cast as we to an appropriate type.
Definition core.h:138
double f64
Definition core.h:139
srn_seq_t * seq
Definition core.h:147
union srn_value_t::@033047061046230251001111174367071167226300135003 as
IMPORTANT NOTE: The size of this union should never be larger than a word.
srn_error_t * error
Definition core.h:144
srn_map_t * map
Definition core.h:148
srn_symbol_t * symbol
Definition core.h:143
srn_value_tag_t type
Definition core.h:132
srn_keyword_t * keyword
Definition core.h:146
srn_string_t * string
Definition core.h:142
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
#define PANIC(msg)
Definition utils.h:53