Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
reader.h
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <lxsameer@lxsameer.com>
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
19#pragma once
20
21/** @file
22A simple sexp reader to be used with textual IR.
23
24The IR types we use for the runtime have a lispy representation, and this
25reader will parse symbolic expressions in a basic form and different layers
26of IR can use this reader and transform the symbolic expressions to
27a format that they like.
28
29I wrote this parser mostly to act as an inefficient way to parse different
30layers of IR without needing to have multiple parsers. Long in the future
31when we have everything in place, I should write efficient parsers for only
32the crucial layers of IR and avoid this generic one.
33*/
34
35#include <stdint.h>
36
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44#include "serene/rt/errors.h"
45
50
59
60// -----------------------------------------------------------------------------
61// Atoms
62// -----------------------------------------------------------------------------
63typedef struct srn_sexp_atom_t {
64 char *name;
66
67[[gnu::nonnull(1, 2)]]
68srn_sexp_node_t *srn_sexp_make_atom(srn_context_t *ctx, const char *name);
69
70// -----------------------------------------------------------------------------
71// String
72// -----------------------------------------------------------------------------
73typedef struct srn_sexp_str_t {
74 char *data;
76
77[[gnu::nonnull(1)]]
78srn_sexp_node_t *srn_sexp_make_str(srn_context_t *ctx, const char *data);
79
80// -----------------------------------------------------------------------------
81// Number
82// -----------------------------------------------------------------------------
83/// A number literal, kept as the raw text it was written with (e.g. "42", "-3.14",
84/// "+1e10"). The reader only recognises the shape of a number, it does not parse it
85/// into an int/float value or fix its precision, that is a later phase's job.
86typedef struct srn_sexp_num_t {
87 char *text;
89
90[[gnu::nonnull(1, 2)]]
91srn_sexp_node_t *srn_sexp_make_num(srn_context_t *ctx, const char *text);
92
93// -----------------------------------------------------------------------------
94// Cons
95// -----------------------------------------------------------------------------
100
101[[gnu::nonnull(1)]]
104
105// -----------------------------------------------------------------------------
106// List
107// -----------------------------------------------------------------------------
112
113[[gnu::nonnull(1)]]
115
116// -----------------------------------------------------------------------------
117// Node
118// -----------------------------------------------------------------------------
132
133[[gnu::nonnull(1)]]
135
136// -----------------------------------------------------------------------------
137// Parser and tree
138// -----------------------------------------------------------------------------
139typedef struct srn_sexp_parser_t {
141 /// We store whatever the caller passes as reading context to pass it back
142 /// later on in the parsed tree.
144
146 const unsigned char *buffer;
147 size_t len;
148
149 // This should be the same size as the srn_src_location_t.offset
150 uint32_t pos;
152
153typedef struct srn_sexp_tree_t {
155 // Root nodes of the tree. Each IR has a dedicated root node
157 // The reading context that handed to the parser for this read.
160
161/**
162 * Read the symbolic expressions from a buffer. `reading_context` is a piece of data
163 * that the reader might provide as an identity or a piece of data that gives meaning
164 * to the result. For example a namespace key.
165 */
166[[gnu::nonnull(1, 3)]]
168srn_sexp_read_from_string(srn_context_t *ctx, void *reading_context, const char *buf);
169
170/**
171 * Read the symbolic expressions from a file. `reading_context` is a piece of data
172 * that the reader might provide as an identity or a piece of data that gives meaning
173 * to the result. For example a namespace key.
174 */
175[[gnu::nonnull(1, 3)]]
177srn_sexp_read_from_file(srn_context_t *ctx, void *reading_context, const char *path);
178
179// -----------------------------------------------------------------------------
180// Private API, exposed for testing
181// -----------------------------------------------------------------------------
186
188
190
191/**
192 * Convert the given `sexp` to a string for debugging purposes. The string will
193 * contain metadata info as well.
194 */
195[[gnu::nonnull(1, 2)]]
197
198#ifdef __cplusplus
199}
200#endif
A bit-partitioned trie with a tail buffer, used as a growable indexed sequence in the runtime.
Error handling for the runtime.
void * srn_fiber_result_t
What a fiber's entry produces, type-erased.
Definition fiber.h:171
srn_sexp_node_t * srn_sexp_make_list(srn_context_t *ctx)
Definition reader.c:224
srn_sexp_node_type_e
Definition reader.h:51
@ SRN_SEXP_CONS
Definition reader.h:53
@ SRN_SEXP_ATOM
Definition reader.h:52
@ SRN_SEXP_NUM
Definition reader.h:55
@ SRN_SEXP_LIST
Definition reader.h:54
@ SRN_SEXP_STR
Definition reader.h:57
@ SRN_SEXP_SEQ
Definition reader.h:56
srn_sexp_node_t * srn_sexp_make_atom(srn_context_t *ctx, const char *name)
Definition reader.c:176
srn_sexp_node_t * read_expr(srn_sexp_parser_t *parser, srn_error_t **err)
Definition reader.c:552
srn_sexp_node_t * srn_sexp_make_num(srn_context_t *ctx, const char *text)
Definition reader.c:198
const char * srn_sexp_to_debug_string(srn_context_t *ctx, srn_sexp_tree_t *tree)
Convert the given sexp to a string for debugging purposes.
Definition reader.c:860
srn_sexp_tree_t * srn_sexp_read_from_string(srn_context_t *ctx, void *reading_context, const char *buf)
Read the symbolic expressions from a buffer.
Definition reader.c:636
srn_sexp_node_t * srn_sexp_make_str(srn_context_t *ctx, const char *data)
Definition reader.c:187
srn_fiber_result_t read_from_buffer(srn_context_t *ctx, void *arg)
Definition reader.c:584
struct array_list_t array_list_t
Definition reader.h:49
srn_sexp_node_t * srn_sexp_make_node(srn_context_t *ctx, srn_sexp_node_type_e type)
Definition reader.c:234
srn_sexp_node_t * srn_sexp_make_cons(srn_context_t *ctx, srn_sexp_node_t *head, srn_sexp_node_t *tail)
Definition reader.c:211
srn_sexp_tree_t * srn_sexp_read_from_file(srn_context_t *ctx, void *reading_context, const char *path)
Read the symbolic expressions from a file.
Definition reader.c:643
Source manager is what that read the source files and own the buffer that contains the data.
size_t srn_sm_buffer_id_t
void * arg
char path[PATH_MAX]
srn_sexp_parser_t * parser
Definition reader.h:183
srn_sexp_tree_t * tree
Definition reader.h:184
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:173
char * name
Definition reader.h:64
srn_sexp_node_t * tail
Definition reader.h:98
srn_sexp_node_t * head
Definition reader.h:97
srn_sexp_node_t * head
Definition reader.h:110
srn_sexp_list_t * list
Definition reader.h:125
union srn_sexp_node_t::@313353022266020117306231170157030062126255275140 as
srn_sexp_str_t * string
Definition reader.h:128
srn_sexp_cons_t * cons
Definition reader.h:126
srn_sexp_node_type_e type
Definition reader.h:120
srn_sexp_atom_t * atom
Definition reader.h:124
array_list_t * seq
Definition reader.h:127
srn_sexp_num_t * num
Definition reader.h:129
srn_metadata_t * metadata
Definition reader.h:121
A number literal, kept as the raw text it was written with (e.g.
Definition reader.h:86
char * text
Definition reader.h:87
const unsigned char * buffer
Definition reader.h:146
void * reading_context
We store whatever the caller passes as reading context to pass it back later on in the parsed tree.
Definition reader.h:143
uint32_t pos
Definition reader.h:150
srn_context_t * ctx
Definition reader.h:140
const srn_sm_buffer_id_t buffer_id
Definition reader.h:145
char * data
Definition reader.h:74
void * reading_context
Definition reader.h:158
array_list_t exprs
Definition reader.h:156