Debian Codegraph @

src/ikarus-print.c

raw
1 /*
2 * Ikarus Scheme -- A compiler for R6RS Scheme.
3 * Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19
20
21 #include "ikarus-data.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 static void print(FILE* fh, ikptr x);
26
27 void ik_fprint(FILE* fh, ikptr x){
28 print(fh, x);
29 }
30
31 void ik_print(ikptr x){
32 print(stdout, x);
33 fprintf(stdout, "\n");
34 }
35
36 char* char_string[128] = {
37 "#\\nul","#\\soh","#\\stx","#\\etx","#\\eot","#\\enq","#\\ack","#\\bel",
38 "#\\bs", "#\\tab","#\\newline", "#\\vt", "#\\ff", "#\\return", "#\\so",
39 "#\\si",
40 "#\\dle","#\\dc1","#\\dc2","#\\dc3","#\\dc4","#\\nak","#\\syn","#\\etb",
41 "#\\can","#\\em", "#\\sub","#\\esc","#\\fs", "#\\gs", "#\\rs", "#\\us",
42 "#\\space","#\\!","#\\\"","#\\#","#\\$","#\\%","#\\&","#\\'",
43 "#\\(","#\\)","#\\*","#\\+","#\\,","#\\-","#\\.","#\\/",
44 "#\\0","#\\1","#\\2","#\\3","#\\4","#\\5","#\\6","#\\7",
45 "#\\8","#\\9","#\\:","#\\;","#\\<","#\\=","#\\>","#\\?",
46 "#\\@","#\\A","#\\B","#\\C","#\\D","#\\E","#\\F","#\\G",
47 "#\\H","#\\I","#\\J","#\\K","#\\L","#\\M","#\\N","#\\O",
48 "#\\P","#\\Q","#\\R","#\\S","#\\T","#\\U","#\\V","#\\W",
49 "#\\X","#\\Y","#\\Z","#\\[","#\\\\","#\\]","#\\^","#\\_",
50 "#\\`","#\\a","#\\b","#\\c","#\\d","#\\e","#\\f","#\\g",
51 "#\\h","#\\i","#\\j","#\\k","#\\l","#\\m","#\\n","#\\o",
52 "#\\p","#\\q","#\\r","#\\s","#\\t","#\\u","#\\v","#\\w",
53 "#\\x","#\\y","#\\z","#\\{","#\\|","#\\}","#\\~","#\\del"};
54
55
56
57
58 static void
59 print(FILE* fh, ikptr x){
60 if(is_fixnum(x)){
61 fprintf(fh, "%ld", unfix(x));
62 }
63 else if(x == false_object){
64 fprintf(fh, "#f");
65 }
66 else if(x == true_object){
67 fprintf(fh, "#t");
68 }
69 else if(x == null_object){
70 fprintf(fh, "()");
71 }
72 else if(is_char(x)){
73 unsigned long int i = ((long int)x) >> char_shift;
74 if(i < 128){
75 fprintf(fh, "%s", char_string[i]);
76 } else {
77 fprintf(fh, "#\\x%lx", i);
78 }
79 }
80 #if 0
81 else if(tagof(x) == symbol_tag){
82 ikptr str = ref(x, off_symbol_string);
83 fprintf(fh, "%s", str+off_string_data);
84 }
85 #endif
86 else if(tagof(x) == vector_tag){
87 ikptr fst = ref(x, off_vector_length);
88 if(is_fixnum(fst)){
89 ikptr len = fst;
90 if(len == 0){
91 fprintf(fh, "#()");
92 } else {
93 fprintf(fh, "#(");
94 ikptr data = x + off_vector_data;
95 print(fh, ref(data, 0));
96 ikptr i = (ikptr)wordsize;
97 while(i<len){
98 fprintf(fh, " ");
99 print(fh, ref(data,i));
100 i += wordsize;
101 }
102 fprintf(fh, ")");
103 }
104 } else if (fst == symbol_record_tag){
105 ikptr str = ref(x, off_symbol_record_string);
106 ikptr fxlen = ref(str, off_string_length);
107 int len = unfix(fxlen);
108 int * data = (int*)(str + off_string_data);
109 int i;
110 for(i=0; i<len; i++){
111 char c = (data[i]) >> char_shift;
112 fprintf(fh, "%c", c);
113 }
114 } else {
115 fprintf(fh, "#<unknown fst=0x%p>", (void*)fst);
116 }
117 }
118 else if(is_closure(x)){
119 fprintf(fh, "#<procedure>");
120 }
121 else if(is_pair(x)){
122 fprintf(fh, "(");
123 print(fh, ref(x, off_car));
124 ikptr d = ref(x, off_cdr);
125 /* fprintf(stderr, "d=0x%016lx\n", (long int)d); */
126 while(1){
127 if(is_pair(d)){
128 fprintf(fh, " ");
129 print(fh, ref(d, off_car));
130 d = ref(d, off_cdr);
131 }
132 else if(d == null_object){
133 fprintf(fh, ")");
134 return;
135 }
136 else {
137 fprintf(fh, " . ");
138 print(fh, d);
139 fprintf(fh, ")");
140 return;
141 }
142 }
143 }
144 else if(tagof(x) == string_tag){
145 ikptr fxlen = ref(x, off_string_length);
146 int len = unfix(fxlen);
147 int * data = (int*)(x + off_string_data);
148 fprintf(fh, "\"");
149 int i;
150 for(i=0; i<len; i++){
151 char c = (data[i]) >> char_shift;
152 if((c == '\\') || (c == '"')){
153 fprintf(fh, "\\");
154 }
155 fprintf(fh, "%c", c);
156 }
157 fprintf(fh, "\"");
158 }
159 else if(tagof(x) == bytevector_tag){
160 ikptr fxlen = ref(x, off_bytevector_length);
161 int len = unfix(fxlen);
162 unsigned char* data = (unsigned char*)(x + off_bytevector_data);
163 fprintf(fh, "#vu8(");
164 int i;
165 for(i=0; i<(len-1); i++){
166 fprintf(fh, "%d ", data[i]);
167 }
168 if(i < len){
169 fprintf(fh, "%d", data[i]);
170 }
171 fprintf(fh, ")");
172 }
173 else {
174 fprintf(fh, "#<unknown>");
175 }
176 }
177
178