Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
lists.h
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#pragma once
20
21#include "serene/rt/core/values.h"
22// -----------------------------------------------------------------------------
23// Pairs/Cons & Lists
24// We refer to cons as pairs just to reduce the confusing situations where we
25// don't know what cons are we talking about, lisp cons? cons the functions?
26// cons the constructor?
27// -----------------------------------------------------------------------------
33
34/// Since all the values are immutable and persistent. A List is just a metadata
35/// on top of a chain of pairs
36typedef struct srn_list_t {
37 srn_header_t *header;
39 size_t length;
41
42static inline bool srn_is_pair(srn_value_t v) {
43 return ((v & TAG_PRIM_MASK) == TAG_PAIR);
44}
45
46static inline bool srn_is_list(srn_value_t v) {
47 return ((v & TAG_PRIM_MASK) == TAG_LIST);
48}
49
50/// Create a new pair of values in the given context `ctx`.
51[[gnu::nonnull(1)]] srn_value_t srn_make_pair(srn_context_t *ctx,
53 srn_value_t left,
54 srn_value_t right);
55
56/// Create a new list and empty list
57[[gnu::nonnull(1)]] srn_value_t srn_make_list(srn_context_t *ctx);
58
59/// Add the given value `v` to the front of the list and returns the new list.
60[[gnu::nonnull(1)]] srn_value_t srn_conj_list(srn_context_t *ctx,
62 srn_list_t *head, srn_value_t v);
srn_value_t srn_make_list(srn_context_t *ctx)
Create a new list and empty list.
Definition lists.c:31
static bool srn_is_pair(srn_value_t v)
Definition lists.h:42
srn_value_t srn_make_pair(srn_context_t *ctx, srn_src_location_t *loc, srn_value_t left, srn_value_t right)
Create a new pair of values in the given context ctx.
static bool srn_is_list(srn_value_t v)
Definition lists.h:46
srn_value_t srn_conj_list(srn_context_t *ctx, srn_src_location_t *loc, srn_list_t *head, srn_value_t v)
Add the given value v to the front of the list and returns the new list.
Since all the values are immutable and persistent.
Definition lists.h:36
srn_header_t * header
Definition lists.h:37
srn_pair_t * head
Definition lists.h:38
size_t length
Definition lists.h:39
srn_header_t * header
Definition lists.h:29
srn_value_t right
Definition lists.h:31
srn_value_t left
Definition lists.h:30