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

Go to the source code of this file.

Functions

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

Function Documentation

◆ 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:151
#define AS_SYMBOL(value_ref)
Definition core.h:155
@ VQuote
Definition core.h:98
@ VSeq
Definition core.h:97
@ VNamespace
Definition core.h:100
@ VClosure
Definition core.h:99
@ VNil
Definition core.h:91
@ VList
Definition core.h:96
@ VFalse
Definition core.h:93
@ VF64
Definition core.h:95
@ VSymbol
Definition core.h:102
@ VI64
Definition core.h:94
@ VMap
Definition core.h:104
@ VError
VError should be last.
Definition core.h:106
@ VKeyword
Definition core.h:103
@ VTrue
Definition core.h:92
@ VString
Definition core.h:101
#define AS_STRING(value_ref)
Definition core.h:154
#define AS_NS(value_ref)
Definition core.h:153
#define AS_I64(value_ref)
Definition core.h:150
#define AS_MAP(value_ref)
Definition core.h:160
#define AS_ERROR(value_ref)
Definition core.h:156
#define AS_KEYWORD(value_ref)
Definition core.h:158
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 runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:173
char * msg
Definition errors.h:176
srn_error_tag_t tag
Definition errors.h:175
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:40
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
srn_namespace_t * ns
Definition symbols.h:32
srn_string_t * name
Definition symbols.h:31
srn_value_tag_t type
Definition core.h:110
#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_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}
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
Here is the call graph for this function: