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 "memory/conspage.h"
22#include "memory/hashmap.h"
23#include "arith/integer.h"
24#include "ops/intern.h"
25#include "memory/stack.h"
26#include "io/print.h"
27#include "time/psse_time.h"
28#include "memory/vectorspace.h"
29
30/**
31 * print all the characters in the symbol or string indicated by `pointer`
32 * onto this `output`; if `pointer` does not indicate a string or symbol,
33 * don't print anything but just return.
34 */
35void print_string_contents( URL_FILE *output, struct cons_pointer pointer ) {
36 while ( stringp( pointer ) || symbolp( pointer ) || keywordp( pointer ) ) {
37 struct cons_space_object *cell = &pointer2cell( pointer );
38 wchar_t c = cell->payload.string.character;
39
40 if ( c != '\0' ) {
41 url_fputwc( c, output );
42 }
43 pointer = cell->payload.string.cdr;
44 }
45}
46
47/**
48 * print all the characters in the string indicated by `pointer` onto
49 * the stream at this `output`, prepending and appending double quote
50 * characters.
51 */
52void print_string( URL_FILE *output, struct cons_pointer pointer ) {
53 url_fputwc( btowc( '"' ), output );
54 print_string_contents( output, pointer );
55 url_fputwc( btowc( '"' ), output );
56}
57
58/**
59 * Print a single list cell (cons cell) indicated by `pointer` to the
60 * stream indicated by `output`. if `initial_space` is `true`, prepend
61 * a space character.
62 */
63void
64print_list_contents( URL_FILE *output, struct cons_pointer pointer,
65 bool initial_space ) {
66 struct cons_space_object *cell = &pointer2cell( pointer );
67
68 switch ( cell->tag.value ) {
69 case CONSTV:
70 if ( initial_space ) {
71 url_fputwc( btowc( ' ' ), output );
72 }
73 print( output, cell->payload.cons.car );
74
75 print_list_contents( output, cell->payload.cons.cdr, true );
76 break;
77 case NILTV:
78 break;
79 default:
80 url_fwprintf( output, L" . " );
81 print( output, pointer );
82 }
83}
84
85void print_list( URL_FILE *output, struct cons_pointer pointer ) {
86 url_fputws( L"(", output );
87 print_list_contents( output, pointer, false );
88 url_fputws( L")", output );
89}
90
91void print_map( URL_FILE *output, struct cons_pointer map ) {
92 if ( hashmapp( map ) ) {
93 struct vector_space_object *vso = pointer_to_vso( map );
94
95 url_fputwc( btowc( '{' ), output );
96
97 for ( struct cons_pointer ks = hashmap_keys( map ); !nilp( ks );
98 ks = c_cdr( ks ) ) {
99 struct cons_pointer key = c_car( ks );
100 print( output, key );
101 url_fputwc( btowc( ' ' ), output );
102 print( output, hashmap_get( map, key ) );
103
104 if ( !nilp( c_cdr( ks ) ) ) {
105 url_fputws( L", ", output );
106 }
107 }
108
109 url_fputwc( btowc( '}' ), output );
110 }
111}
112
113void print_vso( URL_FILE *output, struct cons_pointer pointer ) {
114 struct vector_space_object *vso = pointer_to_vso( pointer );
115 switch ( vso->header.tag.value ) {
116 case HASHTV:
117 print_map( output, pointer );
118 break;
119 // \todo: others.
120 default:
121 fwprintf( stderr, L"Unrecognised vector-space type '%d'\n",
122 vso->header.tag.value );
123 }
124}
125
126/**
127 * stolen from https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc
128 */
129void print_128bit( URL_FILE *output, __int128_t n ) {
130 if ( n == 0 ) {
131 fwprintf( stderr, L"0" );
132 } else {
133 char str[40] = { 0 }; // log10(1 << 128) + '\0'
134 char *s = str + sizeof( str ) - 1; // start at the end
135 while ( n != 0 ) {
136 if ( s == str )
137 return; // never happens
138
139 *--s = "0123456789"[n % 10]; // save last digit
140 n /= 10; // drop it
141 }
142 url_fwprintf( output, L"%s", s );
143 }
144}
145
146
147/**
148 * Print the cons-space object indicated by `pointer` to the stream indicated
149 * by `output`.
150 */
151struct cons_pointer print( URL_FILE *output, struct cons_pointer pointer ) {
152 struct cons_space_object cell = pointer2cell( pointer );
153 char *buffer;
154
155 /*
156 * Because tags have values as well as bytes, this if ... else if
157 * statement can ultimately be replaced by a switch, which will be neater.
158 */
159 switch ( cell.tag.value ) {
160 case CONSTV:
161 print_list( output, pointer );
162 break;
163 case EXCEPTIONTV:
164 url_fputws( L"\nException: ", output );
165 dump_stack_trace( output, pointer );
166 break;
167 case FUNCTIONTV:
168 url_fputws( L"<Function: ", output );
169 print( output, cell.payload.function.meta );
170 url_fputwc( L'>', output );
171 break;
172 case INTEGERTV:
173 struct cons_pointer s = integer_to_string( pointer, 10 );
174 print_string_contents( output, s );
175 dec_ref( s );
176 break;
177 case KEYTV:
178 url_fputws( L":", output );
179 print_string_contents( output, pointer );
180 break;
181 case LAMBDATV:{
182 url_fputws( L"<Anonymous Function: ", output );
183 struct cons_pointer to_print =
185 make_cons( cell.payload.lambda.args,
186 cell.payload.lambda.body ) );
187
188 print( output, to_print );
189
190 dec_ref( to_print );
191 url_fputwc( L'>', output );
192 }
193 break;
194 case NILTV:
195 url_fwprintf( output, L"nil" );
196 break;
197 case NLAMBDATV:{
198 url_fputws( L"<Anonymous Special Form: ", output );
199 struct cons_pointer to_print =
200 make_cons( c_string_to_lisp_symbol( L"n\u03bb" ),
201 make_cons( cell.payload.lambda.args,
202 cell.payload.lambda.body ) );
203
204 print( output, to_print );
205
206 dec_ref( to_print );
207 url_fputwc( L'>', output );
208 }
209 break;
210 case RATIOTV:
211 print( output, cell.payload.ratio.dividend );
212 url_fputws( L"/", output );
213 print( output, cell.payload.ratio.divisor );
214 break;
215 case READTV:
216 url_fwprintf( output, L"<Input stream: " );
217 print( output, cell.payload.stream.meta );
218 url_fputwc( L'>', output );
219 break;
220 case REALTV:
221 /* \todo using the C heap is a bad plan because it will fragment.
222 * As soon as I have working vector space I'll use a special purpose
223 * vector space object */
224 buffer = ( char * ) malloc( 24 );
225 memset( buffer, 0, 24 );
226 /* format it really long, then clear the trailing zeros */
227 sprintf( buffer, "%-.23Lg", cell.payload.real.value );
228 if ( strchr( buffer, '.' ) != NULL ) {
229 for ( int i = strlen( buffer ) - 1; buffer[i] == '0'; i-- ) {
230 buffer[i] = '\0';
231 }
232 }
233 url_fwprintf( output, L"%s", buffer );
234 free( buffer );
235 break;
236 case STRINGTV:
237 print_string( output, pointer );
238 break;
239 case SYMBOLTV:
240 print_string_contents( output, pointer );
241 break;
242 case SPECIALTV:
243 url_fwprintf( output, L"<Special form: " );
244 print( output, cell.payload.special.meta );
245 url_fputwc( L'>', output );
246 break;
247 case TIMETV:
248 url_fwprintf( output, L"<Time: " );
249 print_string( output, time_to_string( pointer ) );
250 url_fputws( L"; ", output );
251 print_128bit( output, pointer2cell( pointer ).payload.time.value );
252 url_fputwc( L'>', output );
253 break;
254 case TRUETV:
255 url_fwprintf( output, L"t" );
256 break;
257 case VECTORPOINTTV:
258 print_vso( output, pointer );
259 break;
260 case WRITETV:
261 url_fwprintf( output, L"<Output stream: " );
262 print( output, cell.payload.stream.meta );
263 url_fputwc( L'>', output );
264 break;
265 default:
266 fwprintf( stderr,
267 L"Error: Unrecognised tag value %d (%4.4s)\n",
268 cell.tag.value, &cell.tag.bytes[0] );
269 break;
270 }
271
272 return pointer;
273}
274
275void println( URL_FILE *output ) {
276 url_fputws( L"\n", output );
277}
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 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 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.
#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.
#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:220
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:162
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:52
void print_vso(URL_FILE *output, struct cons_pointer pointer)
Definition print.c:113
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:64
void print_list(URL_FILE *output, struct cons_pointer pointer)
Definition print.c:85
void print_map(URL_FILE *output, struct cons_pointer map)
Definition print.c:91
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:35
void println(URL_FILE *output)
Definition print.c:275
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:151
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:129
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:268
#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...