Serene Runtime 1.0.0-dev
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/rt/errors.h"
23#include "serene/utils.h"
24
26 srn_context_t *ctx, srn_metadata_t *metadata, srn_value_t *const *items, size_t count
27) {
28 PANIC_IF_NULL(ctx);
29 if (count == 0) {
30 return &nil_v;
31 }
32 PANIC_IF_NULL(items);
33
34 // Build the chain from the tail back to the head so each node's `next`
35 // already points at an initialized successor.
36 srn_list_node_t *head = nullptr;
37 for (size_t i = count; i-- > 0;) {
39 node->value = items[i];
40 node->next = head;
41 head = node;
42 }
43
44 srn_list_t *list = ALLOC(ctx, srn_list_t);
45 list->len = count;
46 list->head = head;
47 return srn_value_make(ctx, VList, metadata, (void *)list);
48}
49
52 PANIC_IF_NULL(ctx);
53 PANIC_IF_NULL(value);
54 PANIC_IF_NULL(list);
55
56 srn_list_node_t *next_head = nullptr;
57 size_t next_len = 0;
58 if (!IS_NIL(list)) {
59 if (!IS_A(list, VList)) {
60 return srn_errors_make_error(ctx, metadata, ABSURD, "expected a list");
61 }
62 srn_list_t *l = AS_LIST(list);
63 next_head = l->head;
64 next_len = l->len;
65 }
66
68 node->value = value;
69 node->next = next_head;
70
71 srn_list_t *result = ALLOC(ctx, srn_list_t);
72 result->len = next_len + 1;
73 result->head = node;
74 return srn_value_make(ctx, VList, metadata, (void *)result);
75}
#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_value_t nil_v
Definition core.c:27
#define AS_LIST(value_ref)
Definition core.h:179
#define IS_NIL(value_ref)
Definition core.h:170
@ VList
Definition core.h:118
#define IS_A(value_ref, field)
Definition core.h:167
Error handling for the runtime.
@ ABSURD
Definition errors.h:76
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:51
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:25
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
srn_list_node_t * head
Definition lists.h:37
size_t len
Definition lists.h:36
#define PANIC_IF_NULL(ptr)
Definition utils.h:66