Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
lists.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#include "serene/rt/lists.h"
19
20#include "serene/rt/context.h"
21#include "serene/rt/core.h"
22#include "serene/utils.h"
23
25 size_t count) {
26 PANIC_IF_NULL(ctx);
27 if (count == 0) {
28 return &nil_v;
29 }
30 PANIC_IF_NULL(items);
31
32 // Build the chain from the tail back to the head so each node's `next`
33 // already points at an initialized successor.
34 srn_list_node_t *head = nullptr;
35 for (size_t i = count; i-- > 0;) {
37 node->value = items[i];
38 node->next = head;
39 head = node;
40 }
41
42 srn_list_t *list = ALLOC(ctx, srn_list_t);
43 list->len = count;
44 list->head = head;
45 return srn_value_make(ctx, VList, metadata, (void *)list);
46}
47
49 srn_value_t *list) {
50 PANIC_IF_NULL(ctx);
51 PANIC_IF_NULL(value);
52 PANIC_IF_NULL(list);
53
54 srn_list_node_t *next_head = nullptr;
55 size_t next_len = 0;
56 if (!IS_NIL(list)) {
57 srn_list_t *l = AS_LIST(list);
58 next_head = l->head;
59 next_len = l->len;
60 }
61
63 node->value = value;
64 node->next = next_head;
65
66 srn_list_t *result = ALLOC(ctx, srn_list_t);
67 result->len = next_len + 1;
68 result->head = node;
69 return srn_value_make(ctx, VList, metadata, (void *)result);
70}
#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
#define AS_LIST(value_ref)
Definition core.h:174
#define IS_NIL(value_ref)
Definition core.h:165
@ VList
Definition core.h:118
static srn_value_t nil_v
Definition core.h:156
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
#define PANIC_IF_NULL(ptr)
Definition utils.h:64