Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
dump.c
Go to the documentation of this file.
1/*
2 * dump.c
3 *
4 * Dump representations of both cons space and vector space objects.
5 *
6 *
7 * (c) 2018 Simon Brooke <simon@journeyman.cc>
8 * Licensed under GPL version 2.0, or, at your option, any later version.
9 */
10
11#include <stdint.h>
12#include <stdlib.h>
13#include <string.h>
14#include <stdio.h>
15/*
16 * wide characters
17 */
18#include <wchar.h>
19#include <wctype.h>
20
21#include "memory/conspage.h"
23#include "debug.h"
24#include "memory/hashmap.h"
25#include "ops/intern.h"
26#include "io/io.h"
27#include "io/print.h"
28#include "memory/stack.h"
29#include "memory/vectorspace.h"
30
31
32void dump_string_cell( URL_FILE *output, wchar_t *prefix,
33 struct cons_pointer pointer ) {
34 struct cons_space_object cell = pointer2cell( pointer );
35 if ( cell.payload.string.character == 0 ) {
36 url_fwprintf( output,
37 L"\t\t%ls cell: termination; next at page %d offset %d, count %u\n",
38 prefix,
39 cell.payload.string.cdr.page,
40 cell.payload.string.cdr.offset, cell.count );
41 } else {
42 url_fwprintf( output,
43 L"\t\t%ls cell: character '%lc' (%d) with hash %d; next at page %d offset %d, count %u\n",
44 prefix,
45 ( wint_t ) cell.payload.string.character,
46 cell.payload.string.character,
47 cell.payload.string.hash,
48 cell.payload.string.cdr.page,
49 cell.payload.string.cdr.offset, cell.count );
50 url_fwprintf( output, L"\t\t value: " );
51 print( output, pointer );
52 url_fwprintf( output, L"\n" );
53 }
54}
55
56/**
57 * dump the object at this cons_pointer to this output stream.
58 */
59void dump_object( URL_FILE *output, struct cons_pointer pointer ) {
60 struct cons_space_object cell = pointer2cell( pointer );
61 url_fwprintf( output, L"\t%4.4s (%d) at page %d, offset %d count %u\n",
62 cell.tag.bytes, cell.tag.value, pointer.page, pointer.offset,
63 cell.count );
64
65 switch ( cell.tag.value ) {
66 case CONSTV:
67 url_fwprintf( output,
68 L"\t\tCons cell: car at page %d offset %d, cdr at page %d "
69 L"offset %d, count %u :",
70 cell.payload.cons.car.page,
71 cell.payload.cons.car.offset,
72 cell.payload.cons.cdr.page,
73 cell.payload.cons.cdr.offset, cell.count );
74 print( output, pointer );
75 url_fputws( L"\n", output );
76 break;
77 case EXCEPTIONTV:
78 url_fwprintf( output, L"\t\tException cell: " );
79 dump_stack_trace( output, pointer );
80 break;
81 case FREETV:
82 url_fwprintf( output,
83 L"\t\tFree cell: next at page %d offset %d\n",
84 cell.payload.cons.cdr.page,
85 cell.payload.cons.cdr.offset );
86 break;
87 case INTEGERTV:
88 url_fwprintf( output, L"\t\tInteger cell: value %ld, count %u\n",
89 cell.payload.integer.value, cell.count );
90 if ( !nilp( cell.payload.integer.more ) ) {
91 url_fputws( L"\t\tBIGNUM! More at:\n", output );
92 dump_object( output, cell.payload.integer.more );
93 }
94 break;
95 case KEYTV:
96 dump_string_cell( output, L"Keyword", pointer );
97 break;
98 case LAMBDATV:
99 url_fwprintf( output, L"\t\t\u03bb cell;\n\t\t args: " );
100 print( output, cell.payload.lambda.args );
101 url_fwprintf( output, L";\n\t\t\tbody: " );
102 print( output, cell.payload.lambda.body );
103 url_fputws( L"\n", output );
104 break;
105 case NILTV:
106 break;
107 case NLAMBDATV:
108 url_fwprintf( output, L"\t\tn\u03bb cell; \n\t\targs: " );
109 print( output, cell.payload.lambda.args );
110 url_fwprintf( output, L";\n\t\t\tbody: " );
111 print( output, cell.payload.lambda.body );
112 url_fputws( L"\n", output );
113 break;
114 case RATIOTV:
115 url_fwprintf( output,
116 L"\t\tRational cell: value %ld/%ld, count %u\n",
117 pointer2cell( cell.payload.ratio.dividend ).payload.
118 integer.value,
119 pointer2cell( cell.payload.ratio.divisor ).payload.
120 integer.value, cell.count );
121 break;
122 case READTV:
123 url_fputws( L"\t\tInput stream; metadata: ", output );
124 print( output, cell.payload.stream.meta );
125 url_fputws( L"\n", output );
126 break;
127 case REALTV:
128 url_fwprintf( output, L"\t\tReal cell: value %Lf, count %u\n",
129 cell.payload.real.value, cell.count );
130 break;
131 case STRINGTV:
132 dump_string_cell( output, L"String", pointer );
133 break;
134 case SYMBOLTV:
135 dump_string_cell( output, L"Symbol", pointer );
136 break;
137 case TRUETV:
138 break;
139 case VECTORPOINTTV:{
140 url_fwprintf( output,
141 L"\t\tPointer to vector-space object at %p\n",
142 cell.payload.vectorp.address );
143 struct vector_space_object *vso = cell.payload.vectorp.address;
144 url_fwprintf( output,
145 L"\t\tVector space object of type %4.4s (%d), payload size "
146 L"%d bytes\n",
147 &vso->header.tag.bytes, vso->header.tag.value,
148 vso->header.size );
149
150 switch ( vso->header.tag.value ) {
151 case STACKFRAMETV:
152 dump_frame( output, pointer );
153 break;
154 case HASHTV:
155 dump_map( output, pointer );
156 break;
157 }
158 }
159 break;
160 case WRITETV:
161 url_fputws( L"\t\tOutput stream; metadata: ", output );
162 print( output, cell.payload.stream.meta );
163 url_fputws( L"\n", output );
164 break;
165 }
166}
#define KEYTV
The string KEYW, 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
uint32_t page
the index of the page on which this cell resides
#define STRINGTV
The string STRG, considered as an unsigned int.
#define INTEGERTV
The string INTR, considered as an unsigned int.
#define FREETV
The string FREE, 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.
uint32_t count
the count of the number of references to this cell
uint32_t offset
the index of the cell within the page
#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 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.
#define pointer2cell(pointer)
given a cons_pointer as argument, return the cell.
An indirect pointer to a cons cell.
an object in cons space.
void dump_object(URL_FILE *output, struct cons_pointer pointer)
dump the object at this cons_pointer to this output stream.
Definition dump.c:59
void dump_string_cell(URL_FILE *output, wchar_t *prefix, struct cons_pointer pointer)
Definition dump.c:32
#define url_fputws(ws, f)
Definition fopen.h:51
#define url_fwprintf(f,...)
Definition fopen.h:50
void dump_map(URL_FILE *output, struct cons_pointer pointer)
Definition hashmap.c:138
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 dump_frame(URL_FILE *output, struct cons_pointer frame_pointer)
Dump a stackframe to this stream for debugging.
Definition stack.c:244
void dump_stack_trace(URL_FILE *output, struct cons_pointer pointer)
Definition stack.c:268
#define STACKFRAMETV
Definition stack.h:31
#define HASHTV
Definition vectorspace.h:30
struct vector_space_header header
the header of this object
union vector_space_header::@4 tag
the tag (type) of this vector-space object.
uint64_t size
the size of my payload, in bytes
Definition vectorspace.h:80
a vector_space_object is just a vector_space_header followed by a lump of bytes; what we deem to be i...