Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
print.c File Reference
#include "serene/rt/print.h"
#include <inttypes.h>
#include "serene/rt/context.h"
#include "serene/rt/core.h"
#include "serene/rt/errors.h"
#include "serene/rt/impl/seq.h"
#include "serene/rt/keywords.h"
#include "serene/rt/lists.h"
#include "serene/rt/maps.h"
#include "serene/rt/namespaces.h"
#include "serene/rt/seqs.h"
#include "serene/rt/strings.h"
#include "serene/rt/symbols.h"
#include "serene/utils.h"
Include dependency graph for print.c:

Go to the source code of this file.

Functions

static void print_string_payload (FILE *out, const srn_string_t *s)
static void write_bytes (FILE *out, const uint8_t *buf, size_t len)
static void print_list (FILE *out, srn_context_t *ctx, const srn_value_t *v)
static void print_seq (FILE *out, srn_context_t *ctx, const srn_value_t *v)
void srn_value_print (FILE *out, srn_context_t *ctx, const srn_value_t *v)
 Print a value to out in its S-expression form.
void srn_value_println (FILE *out, srn_context_t *ctx, const srn_value_t *v)
 Same as srn_value_print followed by '
'.
static void print_quoted_string (FILE *out, const char *s)
static void print_seq_body (FILE *out, srn_context_t *ctx, const seq_t *body, char open, char close)
void srn_syntax_print (FILE *out, srn_context_t *ctx, const srn_syntax_t *s)
 Print a syntax node to out in its S-expression form.
void srn_syntax_println (FILE *out, srn_context_t *ctx, const srn_syntax_t *s)
 Same as srn_syntax_print followed by '
'.

Function Documentation

◆ print_list()

void print_list ( FILE * out,
srn_context_t * ctx,
const srn_value_t * v )
static

Definition at line 66 of file print.c.

66 {
67 fputc('(', out);
68 srn_list_t *l = AS_LIST(v);
69 bool first = true;
70 for (srn_list_node_t *n = l->head; n != nullptr; n = n->next) {
71 if (!first) {
72 fputc(' ', out);
73 }
74 first = false;
75 srn_value_print(out, ctx, n->value);
76 }
77 fputc(')', out);
78}
int n
Definition acutest.h:525
#define AS_LIST(value_ref)
Definition core.h:179
void srn_value_print(FILE *out, srn_context_t *ctx, const srn_value_t *v)
Print a value to out in its S-expression form.
Definition print.c:97
A list is a singly linked sequence of values.
Definition lists.h:30
srn_list_node_t * head
Definition lists.h:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_quoted_string()

void print_quoted_string ( FILE * out,
const char * s )
static

Definition at line 201 of file print.c.

201 {
202 fputc('"', out);
203 for (const char *p = s; *p != '\0'; p++) {
204 switch (*p) {
205 case '"':
206 fputs("\\\"", out);
207 break;
208 case '\\':
209 fputs("\\\\", out);
210 break;
211 case '\n':
212 fputs("\\n", out);
213 break;
214 case '\t':
215 fputs("\\t", out);
216 break;
217 case '\r':
218 fputs("\\r", out);
219 break;
220 default:
221 fputc((int)(unsigned char)*p, out);
222 break;
223 }
224 }
225 fputc('"', out);
226}
Here is the caller graph for this function:

◆ print_seq()

void print_seq ( FILE * out,
srn_context_t * ctx,
const srn_value_t * v )
static

Definition at line 80 of file print.c.

80 {
81 fputc('[', out);
82 srn_seq_t *s = AS_SEQ(v);
83 for (size_t i = 0; i < s->inner.len; i++) {
84 if (i > 0) {
85 fputc(' ', out);
86 }
88 if (r.maybe_error != nullptr) {
89 fputs("#<seq-error>", out);
90 continue;
91 }
92 srn_value_print(out, ctx, (srn_value_t *)r.data);
93 }
94 fputc(']', out);
95}
#define AS_SEQ(value_ref)
Definition core.h:181
seq_lookup_result_t seq_get(const seq_t *seq, size_t n)
Negative index is not supported.
Definition seq.c:205
seq_elem_t data
Definition seq.h:135
size_t len
logical length.
Definition seq.h:161
A persistent, immutable, indexed sequence.
Definition seqs.h:27
seq_t inner
Definition seqs.h:28
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_seq_body()

void print_seq_body ( FILE * out,
srn_context_t * ctx,
const seq_t * body,
char open,
char close )
static

Definition at line 231 of file print.c.

231 {
232 fputc(open, out);
233 for (size_t i = 0; i < body->len; i++) {
234 if (i > 0) {
235 fputc(' ', out);
236 }
237 seq_lookup_result_t r = seq_get(body, i);
238 if (r.maybe_error != nullptr) {
239 fputs("#<seq-error>", out);
240 continue;
241 }
242 srn_syntax_print(out, ctx, (const srn_syntax_t *)r.data);
243 }
244 fputc(close, out);
245}
void srn_syntax_print(FILE *out, srn_context_t *ctx, const srn_syntax_t *s)
Print a syntax node to out in its S-expression form.
Definition print.c:247
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_string_payload()

void print_string_payload ( FILE * out,
const srn_string_t * s )
static

Definition at line 32 of file print.c.

32 {
33 fputc('"', out);
34 for (size_t i = 0; i < s->len; i++) {
35 uint8_t c = s->buffer[i];
36 switch (c) {
37 case '"':
38 fputs("\\\"", out);
39 break;
40 case '\\':
41 fputs("\\\\", out);
42 break;
43 case '\n':
44 fputs("\\n", out);
45 break;
46 case '\t':
47 fputs("\\t", out);
48 break;
49 case '\r':
50 fputs("\\r", out);
51 break;
52 default:
53 fputc((int)c, out);
54 break;
55 }
56 }
57 fputc('"', out);
58}
uint8_t buffer[]
The buffer that holds the WTF8 sequence.
Definition strings.h:66
size_t len
length of the WTF-8 sequence in bytes
Definition strings.h:64
Here is the caller graph for this function:

◆ srn_syntax_print()

void srn_syntax_print ( FILE * out,
srn_context_t * ctx,
const srn_syntax_t * s )

Print a syntax node to out in its S-expression form.

Numbers are emitted from their stored raw text (no formatting normalization). Maps print as {k v k v} because the syntax-level container is a flat seq.

Definition at line 247 of file print.c.

247 {
248 PANIC_IF_NULL(out);
249 PANIC_IF_NULL(ctx);
250 PANIC_IF_NULL(s);
251
252 switch (s->type) {
253 case SNil:
254 fputs("nil", out);
255 break;
256 case STrue:
257 fputs("true", out);
258 break;
259 case SFalse:
260 fputs("false", out);
261 break;
262
263 case SSymbol:
264 fputs(s->as.symbol, out);
265 break;
266 case SNumber:
267 fputs(s->as.number, out);
268 break;
269
270 case SString:
272 break;
273
274 case SKeyword:
275 fputc(':', out);
276 fputs(s->as.keyword, out);
277 break;
278
279 case SList:
280 print_seq_body(out, ctx, s->as.list, '(', ')');
281 break;
282 case SSeq:
283 print_seq_body(out, ctx, s->as.seq, '[', ']');
284 break;
285 case SMap:
286 print_seq_body(out, ctx, s->as.map, '{', '}');
287 break;
288
289 case SQuote:
290 fputc('\'', out);
291 srn_syntax_print(out, ctx, s->as.quote);
292 break;
293
294 case SError: {
295 srn_error_t *e = s->as.error;
296 (void)fprintf(out, "#<error tag=%d", (int)e->tag);
297 if (e->msg != nullptr) {
298 (void)fprintf(out, " %s", e->msg);
299 }
300 fputc('>', out);
301 break;
302 }
303
304 default:
305 fputs("#<unknown>", out);
306 break;
307 }
308}
@ SQuote
Definition core.h:81
@ SKeyword
Definition core.h:83
@ SFalse
Definition core.h:76
@ SSymbol
Definition core.h:77
@ SMap
Definition core.h:84
@ SNumber
Definition core.h:78
@ SList
Definition core.h:79
@ SNil
Definition core.h:74
@ SError
SError should be last.
Definition core.h:86
@ SString
Definition core.h:82
@ SSeq
Definition core.h:80
@ STrue
Definition core.h:75
static void print_quoted_string(FILE *out, const char *s)
Definition print.c:201
static void print_seq_body(FILE *out, srn_context_t *ctx, const seq_t *body, char open, char close)
Definition print.c:231
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
char * msg
Definition errors.h:127
srn_error_tag_t tag
Definition errors.h:126
const char * string
Definition core.h:97
const char * number
Definition core.h:96
seq_t * seq
Definition core.h:101
union srn_syntax_t::@333325137367112222275110336355257173264057161216 as
IMPORTANT NOTE: The size of this union should never be larger than a word.
seq_t * list
Definition core.h:99
struct srn_syntax_t * quote
Definition core.h:103
const char * symbol
For syntax only, we represent numbers as a strings,.
Definition core.h:95
seq_t * map
Definition core.h:102
const char * keyword
Definition core.h:100
srn_syntax_tag_t type
Definition core.h:90
srn_error_t * error
Definition core.h:98
#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_syntax_println()

void srn_syntax_println ( FILE * out,
srn_context_t * ctx,
const srn_syntax_t * s )

Same as srn_syntax_print followed by '
'.

Definition at line 310 of file print.c.

310 {
311 srn_syntax_print(out, ctx, s);
312 fputc('\n', out);
313}
Here is the call graph for this function:

◆ srn_value_print()

void srn_value_print ( FILE * out,
srn_context_t * ctx,
const srn_value_t * v )

Print a value to out in its S-expression form.

Atoms round-trip with the reader; compounds without a public iterator (VMap) print as opaque placeholders for now.

Definition at line 97 of file print.c.

97 {
98 PANIC_IF_NULL(out);
99 PANIC_IF_NULL(ctx);
100 PANIC_IF_NULL(v);
101
102 switch (v->type) {
103 case VNil:
104 fputs("nil", out);
105 break;
106 case VTrue:
107 fputs("true", out);
108 break;
109 case VFalse:
110 fputs("false", out);
111 break;
112
113 case VI64:
114 (void)fprintf(out, "%" PRId64, AS_I64(v));
115 break;
116
117 case VF64:
118 // %.17g is enough digits to round-trip a double exactly.
119 (void)fprintf(out, "%.17g", AS_F64(v));
120 break;
121
122 case VString:
124 break;
125
126 case VSymbol: {
127 srn_symbol_t *sym = AS_SYMBOL(v);
128 // The qualified form, so the printed atom names one symbol, symbols
129 // intern per namespace and the bare name alone is ambiguous across them.
130 if (sym->ns != nullptr) {
131 write_bytes(out, sym->ns->name->buffer, sym->ns->name->len);
132 fputc('/', out);
133 }
134 write_bytes(out, sym->name->buffer, sym->name->len);
135 break;
136 }
137
138 case VKeyword: {
140 fputc(':', out);
141 write_bytes(out, k->name->buffer, k->name->len);
142 break;
143 }
144
145 case VNamespace: {
146 srn_namespace_t *ns = AS_NS(v);
147 fputs("#<ns ", out);
148 write_bytes(out, ns->name->buffer, ns->name->len);
149 fputc('>', out);
150 break;
151 }
152
153 case VList:
154 print_list(out, ctx, v);
155 break;
156
157 case VSeq:
158 print_seq(out, ctx, v);
159 break;
160
161 case VMap: {
162 // CHAMT walker is not exposed; show size only for now.
163 srn_map_t *m = AS_MAP(v);
164 (void)fprintf(out, "#<map size=%zu>", m->inner.len);
165 break;
166 }
167
168 case VClosure:
169 fputs("#<closure>", out);
170 break;
171
172 case VError: {
173 srn_error_t *e = AS_ERROR(v);
174 (void)fprintf(out, "#<error tag=%d", (int)e->tag);
175 if (e->msg != nullptr) {
176 (void)fprintf(out, " %s", e->msg);
177 }
178 fputc('>', out);
179 break;
180 }
181
182 case VQuote:
183 fputs("#<quote>", out);
184 break;
185
186 default:
187 fputs("#<unknown>", out);
188 break;
189 }
190}
#define AS_F64(value_ref)
Definition core.h:173
#define AS_SYMBOL(value_ref)
Definition core.h:177
@ VQuote
Definition core.h:120
@ VSeq
Definition core.h:119
@ VNamespace
Definition core.h:122
@ VClosure
Definition core.h:121
@ VNil
Definition core.h:113
@ VList
Definition core.h:118
@ VFalse
Definition core.h:115
@ VF64
Definition core.h:117
@ VSymbol
Definition core.h:124
@ VI64
Definition core.h:116
@ VMap
Definition core.h:126
@ VError
VError should be last.
Definition core.h:128
@ VKeyword
Definition core.h:125
@ VTrue
Definition core.h:114
@ VString
Definition core.h:123
#define AS_STRING(value_ref)
Definition core.h:176
#define AS_NS(value_ref)
Definition core.h:175
#define AS_I64(value_ref)
Definition core.h:172
#define AS_MAP(value_ref)
Definition core.h:182
#define AS_ERROR(value_ref)
Definition core.h:178
#define AS_KEYWORD(value_ref)
Definition core.h:180
static void print_list(FILE *out, srn_context_t *ctx, const srn_value_t *v)
Definition print.c:66
static void print_seq(FILE *out, srn_context_t *ctx, const srn_value_t *v)
Definition print.c:80
static void print_string_payload(FILE *out, const srn_string_t *s)
Definition print.c:32
static void write_bytes(FILE *out, const uint8_t *buf, size_t len)
Definition print.c:60
size_t len
Number of key/value pairs in the map.
Definition hashmap.h:97
A keyword, just a name.
Definition keywords.h:29
srn_string_t * name
Definition keywords.h:30
A persistent, immutable map.
Definition maps.h:29
hmap_t inner
Definition maps.h:30
srn_string_t * name
Definition namespaces.h:45
srn_namespace_t * ns
Definition symbols.h:32
srn_string_t * name
Definition symbols.h:31
srn_value_tag_t type
Definition core.h:132
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_value_println()

void srn_value_println ( FILE * out,
srn_context_t * ctx,
const srn_value_t * v )

Same as srn_value_print followed by '
'.

Definition at line 192 of file print.c.

192 {
193 srn_value_print(out, ctx, v);
194 fputc('\n', out);
195}
Here is the call graph for this function:

◆ write_bytes()

void write_bytes ( FILE * out,
const uint8_t * buf,
size_t len )
inlinestatic

Definition at line 60 of file print.c.

60 {
61 if (len > 0) {
62 (void)fwrite(buf, 1, len, out);
63 }
64}
Here is the caller graph for this function: