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#pragma once
19
20#include <stddef.h>
21
22#include "serene/rt/core.h"
23
24typedef struct srn_context_t srn_context_t;
25
26/// A list is a singly linked sequence of values. Nodes hold a value and a
27/// pointer to the next node; the list header caches the length and points at
28/// the head node. Empty lists are represented by nil_v; a list value with
29/// tag `VList` always has at least one element.
34
35typedef struct srn_list_t {
36 size_t len;
39
40/// Build a list from an array of elements. When `count == 0`, returns nil_v
41/// (empty lists have no allocated representation). Otherwise allocates one
42/// node per element plus the list header, all in `ctx`.
43[[gnu::nonnull(1)]] srn_value_t *srn_list_make(srn_context_t *ctx,
44 srn_metadata_t *metadata,
45 srn_value_t *const *items,
46 size_t count);
47
48/// Prepend `value` to `list`, returning a new list. `list` may be nil_v
49/// (treated as the empty list); the input list is not modified — its nodes
50/// are shared with the returned list.
51[[gnu::nonnull(1, 3, 4)]] srn_value_t *srn_list_cons(srn_context_t *ctx,
52 srn_metadata_t *metadata,
53 srn_value_t *value,
54 srn_value_t *list);
srn_value_t * srn_list_cons(srn_context_t *ctx, srn_metadata_t *metadata, srn_value_t *value, srn_value_t *list)
Prepend value to list, returning a new list.
Definition lists.c:48
srn_value_t * srn_list_make(srn_context_t *ctx, srn_metadata_t *metadata, srn_value_t *const *items, size_t count)
Build a list from an array of elements.
Definition lists.c:24
A list is a singly linked sequence of values.
Definition lists.h:30
struct srn_list_node_t * next
Definition lists.h:32
srn_value_t * value
Definition lists.h:31
Since all the values are immutable and persistent.
Definition lists.h:36
srn_pair_t * head
Definition lists.h:38
size_t len
Definition lists.h:36