Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
print.c
Go to the documentation of this file.
1/*
2 * print.c
3 *
4 * First pass at a printer, for bootstrapping.
5 *
6 * (c) 2017 Simon Brooke <simon@journeyman.cc>
7 * Licensed under GPL version 2.0, or, at your option, any later version.
8 */
9
10#include <ctype.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14/*
15 * wide characters
16 */
17#include <wchar.h>
18#include <wctype.h>
19
20#include "arith/integer.h"
21#include "debug.h"
22#include "io/io.h"
23#include "io/print.h"
24#include "memory/conspage.h"
26#include "memory/hashmap.h"
27#include "memory/stack.h"
28#include "memory/vectorspace.h"
29#include "ops/intern.h"
30#include "time/psse_time.h"
31
32/**
33 * print all the characters in the symbol or string indicated by `pointer`
34 * onto this `output`; if `pointer` does not indicate a string or symbol,
35 * don't print anything but just return.
36 */
37void print_string_contents( URL_FILE *output, struct cons_pointer pointer ) {
38 while ( stringp( pointer ) || symbolp( pointer ) || keywordp( pointer ) ) {
39 struct cons_space_object *cell = &pointer2cell( pointer );
40 wchar_t c = cell->payload.string.character;
41
42 if ( c != '\0' ) {
43 url_fputwc( c, output );
44 }
45 pointer = cell->payload.string.cdr;
46 }
47}
48
49/**
50 * print all the characters in the string indicated by `pointer` onto
51 * the stream at this `output`, prepending and appending double quote
52 * characters.
53 */
54void print_string( URL_FILE *output, struct cons_pointer pointer ) {
55 url_fputwc( btowc( '"' ), output );
56 print_string_contents( output, pointer );
57 url_fputwc( btowc( '"' ), output );
58}
59
60/**
61 * Print a single list cell (cons cell) indicated by `pointer` to the
62 * stream indicated by `output`. if `initial_space` is `true`, prepend
63 * a space character.
64 */
65void
66print_list_contents( URL_FILE *output, struct cons_pointer pointer,
67 bool initial_space ) {
68 struct cons_space_object *cell = &pointer2cell( pointer );
69
70 switch ( cell->tag.value ) {
71 case CONSTV:
72 if ( initial_space ) {
73 url_fputwc( btowc( ' ' ), output );
74 }
75 print( output, cell->payload.cons.car );
76
77 print_list_contents( output, cell->payload.cons.cdr, true );
78 break;
79 case NILTV:
80 break;
81 default:
82 url_fwprintf( output, L" . " );
83 print( output, pointer );
84 }
85}
86
87void print_list( URL_FILE *output, struct cons_pointer pointer ) {
88 url_fputws( L"(", output );
89 print_list_contents( output, pointer, false );
90 url_fputws( L")", output );
91}
92
93void print_map( URL_FILE *output, struct cons_pointer map ) {
94 if ( hashmapp( map ) ) {
95 struct vector_space_object *vso = pointer_to_vso( map );
96
97 url_fputwc( btowc( '{' ), output );
98
99 for ( struct cons_pointer ks = hashmap_keys( map ); !nilp( ks );
100 ks = c_cdr( ks ) ) {
101 struct cons_pointer key = c_car( ks );
102 print( output, key );
103 url_fputwc( btowc( ' ' ), output );
104 print( output, hashmap_get( map, key ) );
105
106 if ( !nilp( c_cdr( ks ) ) ) {
107 url_fputws( L", ", output );
108 }
109 }
110
111 url_fputwc( btowc( '}' ), output );
112 }
113}
114
115void print_vso( URL_FILE *output, struct cons_pointer pointer ) {
116 struct vector_space_object *vso = pointer_to_vso( pointer );
117 switch ( vso->header.tag.value ) {
118 case HASHTV:
119 print_map( output, pointer );
120 break;
121 case STACKFRAMETV:
122 dump_stack_trace( output, pointer );
123 break;
124 // \todo: others.
125 default:
126 fwprintf( stderr, L"Unrecognised vector-space type '%d'\n",
127 vso->header.tag.value );
128 }
129}
130
131/**
132 * stolen from https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc
133 */
134void print_128bit( URL_FILE *output, __int128_t n ) {
135 if ( n == 0 ) {
136 fwprintf( stderr, L"0" );
137 } else {
138 char str[40] = { 0 }; // log10(1 << 128) + '\0'
139 char *s = str + sizeof( str ) - 1; // start at the end
140 while ( n != 0 ) {
141 if ( s == str )
142 return; // never happens
143
144 *--s = "0123456789"[n % 10]; // save last digit
145 n /= 10; // drop it
146 }
147 url_fwprintf( output, L"%s", s );
148 }
149}
150
151
152/**
153 * Print the cons-space object indicated by `pointer` to the stream indicated
154 * by `output`.
155 */
156struct cons_pointer print( URL_FILE *output, struct cons_pointer pointer ) {
157 struct cons_space_object cell = pointer2cell( pointer );
158 char *buffer;
159
160 /*
161 * Because tags have values as well as bytes, this if ... else if
162 * statement can ultimately be replaced by a switch, which will be neater.
163 */
164 switch ( cell.tag.value ) {
165 case CONSTV:
166 print_list( output, pointer );
167 break;
168 case EXCEPTIONTV:
169 url_fputws( L"\nException: ", output );
170 dump_stack_trace( output, pointer );
171 break;
172 case FUNCTIONTV:
173 url_fputws( L"<Function: ", output );
174 print( output, cell.payload.function.meta );
175 url_fputwc( L'>', output );
176 break;
177 case INTEGERTV:
178 struct cons_pointer s = integer_to_string( pointer, 10 );
179 print_string_contents( output, s );
180 dec_ref( s );
181 break;
182 case KEYTV:
183 url_fputws( L":", output );
184 print_string_contents( output, pointer );
185 break;
186 case LAMBDATV:{
187 url_fputws( L"<Anonymous Function: ", output );
188 struct cons_pointer to_print =
190 make_cons( cell.payload.lambda.args,
191 cell.payload.lambda.body ) );
192
193 print( output, to_print );
194
195 dec_ref( to_print );
196 url_fputwc( L'>', output );
197 }
198 break;
199 case NILTV:
200 url_fwprintf( output, L"nil" );
201 break;
202 case NLAMBDATV:{
203 url_fputws( L"<Anonymous Special Form: ", output );
204 struct cons_pointer to_print =
205 make_cons( c_string_to_lisp_symbol( L"n\u03bb" ),
206 make_cons( cell.payload.lambda.args,
207 cell.payload.lambda.body ) );
208
209 print( output, to_print );
210
211 dec_ref( to_print );
212 url_fputwc( L'>', output );
213 }
214 break;
215 case RATIOTV:
216 print( output, cell.payload.ratio.dividend );
217 url_fputws( L"/", output );
218 print( output, cell.payload.ratio.divisor );
219 break;
220 case READTV:
221 url_fwprintf( output, L"<Input stream: " );
222 print( output, cell.payload.stream.meta );
223 url_fputwc( L'>', output );
224 break;
225 case REALTV:
226 /* \todo using the C heap is a bad plan because it will fragment.
227 * As soon as I have working vector space I'll use a special purpose
228 * vector space object */
229 buffer = ( char * ) malloc( 24 );
230 memset( buffer, 0, 24 );
231 /* format it really long, then clear the trailing zeros */
232 sprintf( buffer, "%-.23Lg", cell.payload.real.value );
233 if ( strchr( buffer, '.' ) != NULL ) {
234 for ( int i = strlen( buffer ) - 1; buffer[i] == '0'; i-- ) {
235 buffer[i] = '\0';
236 }
237 }
238 url_fwprintf( output, L"%s", buffer );
239 free( buffer );
240 break;
241 case STRINGTV:
242 print_string( output, pointer );
243 break;
244 case SYMBOLTV:
245 print_string_contents( output, pointer );
246 break;
247 case SPECIALTV:
248 url_fwprintf( output, L"<Special form: " );
249 print( output, cell.payload.special.meta );
250 url_fputwc( L'>', output );
251 break;
252 case TIMETV:
253 url_fwprintf( output, L"<Time: " );
254 print_string( output, time_to_string( pointer ) );
255 url_fputws( L"; ", output );
256 print_128bit( output, cell.payload.time.value );
257 url_fputwc( L'>', output );
258 break;
259 case TRUETV:
260 url_fwprintf( output, L"t" );
261 break;
262 case VECTORPOINTTV:
263 print_vso( output, pointer );
264 break;
265 case WRITETV:
266 url_fwprintf( output, L"<Output stream: " );
267 print( output, cell.payload.stream.meta );
268 url_fputwc( L'>', output );
269 break;
270 default:
271 fwprintf( stderr,
272 L"Error: Unrecognised tag value %d (%4.4s)\n",
273 cell.tag.value, &cell.tag.bytes[0] );
274 // dump_object( stderr, pointer);
275 break;
276 }
277
278 return pointer;
279}
280
281/**
282 * Function; print one complete lisp expression and return NIL. If write-stream is specified and
283 * is a write stream, then print to that stream, else the stream which is the value of
284 * `*out*` in the environment.
285 *
286 * * (print expr)
287 * * (print expr write-stream)
288 *
289 * @param frame my stack_frame.
290 * @param frame_pointer a pointer to my stack_frame.
291 * @param env my environment (from which the stream may be extracted).
292 * @return NIL.
293 */
294struct cons_pointer
295lisp_print( struct stack_frame *frame, struct cons_pointer frame_pointer,
296 struct cons_pointer env ) {
297 debug_print( L"Entering print\n", DEBUG_IO );
298 struct cons_pointer result = NIL;
299 URL_FILE *output;
300 struct cons_pointer out_stream = writep( frame->arg[1] ) ?
301 frame->arg[1] : get_default_stream( false, env );
302
303 if ( writep( out_stream ) ) {
304 debug_print( L"lisp_print: setting output stream\n", DEBUG_IO );
305 debug_dump_object( out_stream, DEBUG_IO );
306 output = pointer2cell( out_stream ).payload.stream.stream;
307 inc_ref( out_stream );
308 } else {
309 output = file_to_url_file( stderr );
310 }
311
312 debug_print( L"lisp_print: about to print\n", DEBUG_IO );
313 debug_dump_object( frame->arg[0], DEBUG_IO );
314
315 result = print( output, frame->arg[0] );
316
317 debug_print( L"lisp_print returning\n", DEBUG_IO );
318 debug_dump_object( result, DEBUG_IO );
319
320 if ( writep( out_stream ) ) {
321 dec_ref( out_stream );
322 } else {
323 free( output );
324 }
325
326 return result;
327}
328
329void println( URL_FILE *output ) {
330 url_fputws( L"\n", output );
331}
332
333/**
334 * @brief `(prinln out-stream)`: Print a new line character to `out-stream`, if
335 * it is specified and is an output stream, else to `*out*`.
336 *
337 * @param frame
338 * @param frame_pointer
339 * @param env
340 * @return `nil`
341 */
342struct cons_pointer
343lisp_println( struct stack_frame *frame, struct cons_pointer frame_pointer,
344 struct cons_pointer env ) {
345 URL_FILE *output;
346 struct cons_pointer out_stream = writep( frame->arg[1] ) ?
347 frame->arg[1] : get_default_stream( false, env );
348
349 if ( writep( out_stream ) ) {
350 output = pointer2cell( out_stream ).payload.stream.stream;
351 inc_ref( out_stream );
352 } else {
353 output = file_to_url_file( stderr );
354 }
355
356 println( output );
357
358 if ( writep( out_stream ) ) {
359 dec_ref( out_stream );
360 } else {
361 free( output );
362 }
363
364 return NIL;
365}
struct cons_pointer c_cdr(struct cons_pointer arg)
Implementation of cdr in C.
struct cons_pointer c_car(struct cons_pointer arg)
Implementation of car in C.
#define KEYTV
The string KEYW, considered as an unsigned int.
#define SPECIALTV
The string SPFM, considered as an unsigned int.
#define writep(conspoint)
true if conspoint points to a write stream cell, else false.
#define VECTORPOINTTV
The string VECP, considered as an unsigned int.
#define SYMBOLTV
The string SYMB, considered as an unsigned int.
union cons_space_object::@2 tag
union cons_space_object::@3 payload
#define NIL
a cons pointer which points to the special NIL cell
#define FUNCTIONTV
The string FUNC, considered as an unsigned int.
#define STRINGTV
The string STRG, considered as an unsigned int.
#define TIMETV
The string TIME, considered as an unsigned int.
#define INTEGERTV
The string INTR, considered as an unsigned int.
#define RATIOTV
The string RTIO, considered as an unsigned int.
#define CONSTV
The string CONS, considered as an unsigned int.
#define nilp(conspoint)
true if conspoint points to the special cell NIL, else false (there should only be one of these so it...
#define NLAMBDATV
The string NLMD, considered as an unsigned int.
#define TRUETV
The string TRUE, considered as an unsigned int.
#define symbolp(conspoint)
true if conspoint points to a symbol cell, else false
#define REALTV
The string REAL, considered as an unsigned int.
struct cons_pointer inc_ref(struct cons_pointer pointer)
increment the reference count of the object at this cons pointer.
#define NILTV
The string NIL, considered as an unsigned int.
#define EXCEPTIONTV
The string EXEP, considered as an unsigned int.
#define stringp(conspoint)
true if conspoint points to a string cell, else false
struct cons_pointer c_string_to_lisp_symbol(wchar_t *symbol)
Return a lisp symbol representation of this wide character string.
#define keywordp(conspoint)
true if conspoint points to a keyword, else false
#define LAMBDATV
The string LMDA, considered as an unsigned int.
#define WRITETV
The string WRIT, considered as an unsigned int.
#define READTV
The string READ, considered as an unsigned int.
struct cons_pointer dec_ref(struct cons_pointer pointer)
Decrement the reference count of the object at this cons pointer.
#define pointer2cell(pointer)
given a cons_pointer as argument, return the cell.
struct cons_pointer make_cons(struct cons_pointer car, struct cons_pointer cdr)
Construct a cons cell from this pair of pointers.
An indirect pointer to a cons cell.
an object in cons space.
A stack frame.
void debug_dump_object(struct cons_pointer pointer, int level)
Like dump_object, q.v., but protected by the verbosity mechanism.
Definition debug.c:136
void debug_print(wchar_t *message, int level)
print this debug message to stderr, if verbosity matches level.
Definition debug.c:41
#define DEBUG_IO
Print messages debugging input/output operations.
Definition debug.h:59
#define url_fputwc(wc, f)
Definition fopen.h:52
#define url_fputws(ws, f)
Definition fopen.h:51
#define url_fwprintf(f,...)
Definition fopen.h:50
struct cons_pointer integer_to_string(struct cons_pointer int_pointer, int base)
return a string representation of this integer, which may be a bignum.
Definition integer.c:454
struct cons_pointer hashmap_get(struct cons_pointer mapp, struct cons_pointer key)
Get a value from a hashmap.
Definition intern.c:221
struct cons_pointer hashmap_keys(struct cons_pointer mapp)
return a flat list of all the keys in the hashmap indicated by map.
Definition intern.c:163
URL_FILE * file_to_url_file(FILE *f)
given this file handle f, return a new url_file handle wrapping it.
Definition io.c:134
struct cons_pointer get_default_stream(bool inputp, struct cons_pointer env)
Resutn the current default input, or of inputp is false, output stream from this environment.
Definition io.c:411
void print_string(URL_FILE *output, struct cons_pointer pointer)
print all the characters in the string indicated by pointer onto the stream at this output,...
Definition print.c:54
void print_vso(URL_FILE *output, struct cons_pointer pointer)
Definition print.c:115
void print_list_contents(URL_FILE *output, struct cons_pointer pointer, bool initial_space)
Print a single list cell (cons cell) indicated by pointer to the stream indicated by output.
Definition print.c:66
void print_list(URL_FILE *output, struct cons_pointer pointer)
Definition print.c:87
struct cons_pointer lisp_println(struct stack_frame *frame, struct cons_pointer frame_pointer, struct cons_pointer env)
(prinln out-stream): Print a new line character to out-stream, if it is specified and is an output st...
Definition print.c:343
void print_map(URL_FILE *output, struct cons_pointer map)
Definition print.c:93
void print_string_contents(URL_FILE *output, struct cons_pointer pointer)
print all the characters in the symbol or string indicated by pointer onto this output; if pointer do...
Definition print.c:37
void println(URL_FILE *output)
Definition print.c:329
struct cons_pointer print(URL_FILE *output, struct cons_pointer pointer)
Print the cons-space object indicated by pointer to the stream indicated by output.
Definition print.c:156
struct cons_pointer lisp_print(struct stack_frame *frame, struct cons_pointer frame_pointer, struct cons_pointer env)
Function; print one complete lisp expression and return NIL.
Definition print.c:295
void print_128bit(URL_FILE *output, __int128_t n)
stolen from https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc
Definition print.c:134
struct cons_pointer time_to_string(struct cons_pointer pointer)
This is temporary, for bootstrapping.
Definition psse_time.c:95
void dump_stack_trace(URL_FILE *output, struct cons_pointer pointer)
Definition stack.c:305
#define STACKFRAMETV
Definition stack.h:31
#define pointer_to_vso(pointer)
given a pointer to a vector space object, return the object.
Definition vectorspace.h:55
#define HASHTV
Definition vectorspace.h:30
#define hashmapp(conspoint)
Definition vectorspace.h:32
struct vector_space_header header
the header of this object
union vector_space_header::@4 tag
the tag (type) of this vector-space object.
a vector_space_object is just a vector_space_header followed by a lump of bytes; what we deem to be i...