Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
lists.h File Reference
#include <stddef.h>
#include "serene/rt/core.h"
Include dependency graph for lists.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  srn_list_node_t
 A list is a singly linked sequence of values. More...
struct  srn_list_t

Typedefs

typedef struct srn_list_node_t srn_list_node_t
 A list is a singly linked sequence of values.
typedef struct srn_list_t srn_list_t

Functions

srn_value_tsrn_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.
srn_value_tsrn_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.

Typedef Documentation

◆ srn_list_node_t

typedef struct srn_list_node_t srn_list_node_t

A list is a singly linked sequence of values.

Nodes hold a value and a pointer to the next node; the list header caches the length and points at the head node. Empty lists are represented by nil_v; a list value with tag VList always has at least one element.

◆ srn_list_t

typedef struct srn_list_t srn_list_t

Function Documentation

◆ srn_list_cons()

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.

list may be nil_v (treated as the empty list); the input list is not modified — its nodes are shared with the returned list.

Definition at line 51 of file lists.c.

51 {
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
#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
@ ABSURD
Definition errors.h:76
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_list_make()

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.

When count == 0, returns nil_v (empty lists have no allocated representation). Otherwise allocates one node per element plus the list header, all in ctx.

Definition at line 25 of file lists.c.

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}
srn_value_t nil_v
Definition core.c:27
Here is the call graph for this function:
Here is the caller graph for this function: