Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
seqs.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/seqs.h"
19
20#include "serene/rt/context.h"
21#include "serene/rt/core.h"
22#include "serene/rt/errors.h"
23#include "serene/rt/impl/seq.h"
24#include "serene/utils.h"
25
27 PANIC_IF_NULL(ctx);
28
29 srn_seq_t *s = ALLOC(ctx, srn_seq_t);
30 s->inner = seq_empty(ctx);
31
32 if (s->inner.maybe_error != nullptr) {
33 return srn_errors_err_to_value(ctx, metadata, s->inner.maybe_error);
34 }
35
36 return srn_value_make(ctx, VSeq, metadata, (void *)s);
37}
38
40 srn_value_t *value) {
41 PANIC_IF_NULL(ctx);
42 PANIC_IF_NULL(seq);
43 PANIC_IF_NULL(value);
44
45 if (!IS_A(seq, VSeq)) {
46 return srn_errors_make_error(ctx, metadata, ABSURD, "expected a seq");
47 }
48 srn_seq_t *old = AS_SEQ(seq);
49 srn_seq_t *neu = ALLOC(ctx, srn_seq_t);
50 neu->inner = seq_push(ctx, &old->inner, (seq_elem_t)value);
51 if (neu->inner.maybe_error != nullptr) {
52 return srn_errors_err_to_value(ctx, metadata, neu->inner.maybe_error);
53 }
54 return srn_value_make(ctx, VSeq, metadata, (void *)neu);
55}
#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
@ VSeq
Definition core.h:119
#define AS_SEQ(value_ref)
Definition core.h:176
#define IS_A(value_ref, field)
Definition core.h:162
@ ABSURD
Definition errors.h:45
seq_t seq_push(const srn_context_t *ctx, const seq_t *seq, seq_elem_t x)
Definition seq.c:87
seq_t seq_empty(const srn_context_t *ctx)
Definition seq.c:76
This is an implementation of bit - partitioned, persistent, immutable sequence For more information,...
void * seq_elem_t
We use generic pointers to refer to internal nodes, leaf nodes and even elements.
Definition seq.h:131
srn_value_t * srn_seq_empty(srn_context_t *ctx, srn_metadata_t *metadata)
Definition seqs.c:26
srn_value_t * srn_seq_conj(srn_context_t *ctx, srn_metadata_t *metadata, srn_value_t *seq, srn_value_t *value)
Definition seqs.c:39
A persistent, immutable, indexed sequence.
Definition seqs.h:27
seq_t inner
Definition seqs.h:28
#define PANIC_IF_NULL(ptr)
Definition utils.h:64