2007-10-25 16:27:34 -04:00
|
|
|
/*
|
|
|
|
* Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
* Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 3 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2006-11-23 19:38:26 -05:00
|
|
|
|
|
|
|
|
2007-10-17 09:22:47 -04:00
|
|
|
#include "ikarus-data.h"
|
2006-11-23 19:38:26 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
static void print(FILE* fh, ikptr x);
|
2006-11-23 19:38:26 -05:00
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
void ik_fprint(FILE* fh, ikptr x){
|
2006-11-23 19:38:26 -05:00
|
|
|
print(fh, x);
|
|
|
|
}
|
|
|
|
|
2007-12-23 13:37:48 -05:00
|
|
|
void ik_print(ikptr x){
|
2006-11-23 19:38:26 -05:00
|
|
|
print(stdout, x);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
}
|
|
|
|
|
2008-01-01 04:24:36 -05:00
|
|
|
char* char_string[128] = {
|
2006-11-23 19:38:26 -05:00
|
|
|
"#\\nul","#\\soh","#\\stx","#\\etx","#\\eot","#\\enq","#\\ack","#\\bel",
|
|
|
|
"#\\bs", "#\\tab","#\\newline", "#\\vt", "#\\ff", "#\\return", "#\\so",
|
|
|
|
"#\\si",
|
|
|
|
"#\\dle","#\\dc1","#\\dc2","#\\dc3","#\\dc4","#\\nak","#\\syn","#\\etb",
|
|
|
|
"#\\can","#\\em", "#\\sub","#\\esc","#\\fs", "#\\gs", "#\\rs", "#\\us",
|
|
|
|
"#\\space","#\\!","#\\\"","#\\#","#\\$","#\\%","#\\&","#\\'",
|
|
|
|
"#\\(","#\\)","#\\*","#\\+","#\\,","#\\-","#\\.","#\\/",
|
|
|
|
"#\\0","#\\1","#\\2","#\\3","#\\4","#\\5","#\\6","#\\7",
|
|
|
|
"#\\8","#\\9","#\\:","#\\;","#\\<","#\\=","#\\>","#\\?",
|
|
|
|
"#\\@","#\\A","#\\B","#\\C","#\\D","#\\E","#\\F","#\\G",
|
|
|
|
"#\\H","#\\I","#\\J","#\\K","#\\L","#\\M","#\\N","#\\O",
|
|
|
|
"#\\P","#\\Q","#\\R","#\\S","#\\T","#\\U","#\\V","#\\W",
|
|
|
|
"#\\X","#\\Y","#\\Z","#\\[","#\\\\","#\\]","#\\^","#\\_",
|
|
|
|
"#\\`","#\\a","#\\b","#\\c","#\\d","#\\e","#\\f","#\\g",
|
|
|
|
"#\\h","#\\i","#\\j","#\\k","#\\l","#\\m","#\\n","#\\o",
|
|
|
|
"#\\p","#\\q","#\\r","#\\s","#\\t","#\\u","#\\v","#\\w",
|
|
|
|
"#\\x","#\\y","#\\z","#\\{","#\\|","#\\}","#\\~","#\\del"};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2007-12-23 13:37:48 -05:00
|
|
|
print(FILE* fh, ikptr x){
|
2008-01-01 04:24:36 -05:00
|
|
|
if(is_fixnum(x)){
|
|
|
|
fprintf(fh, "%ld", unfix(x));
|
2006-11-23 19:38:26 -05:00
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
else if(x == false_object){
|
2006-11-23 19:38:26 -05:00
|
|
|
fprintf(fh, "#f");
|
|
|
|
}
|
2008-01-02 20:58:48 -05:00
|
|
|
else if(x == true_object){
|
2006-11-23 19:38:26 -05:00
|
|
|
fprintf(fh, "#t");
|
|
|
|
}
|
2008-01-02 20:58:48 -05:00
|
|
|
else if(x == null_object){
|
2006-11-23 19:38:26 -05:00
|
|
|
fprintf(fh, "()");
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
else if(is_char(x)){
|
2008-01-04 03:49:27 -05:00
|
|
|
unsigned long int i = ((long int)x) >> char_shift;
|
|
|
|
if(i < 128){
|
|
|
|
fprintf(fh, "%s", char_string[i]);
|
|
|
|
} else {
|
|
|
|
fprintf(fh, "#\\x%lx", i);
|
|
|
|
}
|
2006-11-23 19:38:26 -05:00
|
|
|
}
|
2007-05-15 08:56:22 -04:00
|
|
|
#if 0
|
2006-11-23 19:38:26 -05:00
|
|
|
else if(tagof(x) == symbol_tag){
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr str = ref(x, off_symbol_string);
|
2006-11-23 19:38:26 -05:00
|
|
|
fprintf(fh, "%s", str+off_string_data);
|
|
|
|
}
|
2007-05-15 08:56:22 -04:00
|
|
|
#endif
|
2006-11-23 19:38:26 -05:00
|
|
|
else if(tagof(x) == vector_tag){
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr len = ref(x, off_vector_length);
|
2006-11-23 19:38:26 -05:00
|
|
|
if(len == 0){
|
|
|
|
fprintf(fh, "#()");
|
|
|
|
} else {
|
|
|
|
fprintf(fh, "#(");
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr data = x + off_vector_data;
|
2006-11-23 19:38:26 -05:00
|
|
|
print(fh, ref(data, 0));
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr i = (ikptr)wordsize;
|
2006-11-23 19:38:26 -05:00
|
|
|
while(i<len){
|
|
|
|
fprintf(fh, " ");
|
|
|
|
print(fh, ref(data,i));
|
|
|
|
i += wordsize;
|
|
|
|
}
|
|
|
|
fprintf(fh, ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_closure(x)){
|
|
|
|
fprintf(fh, "#<procedure>");
|
|
|
|
}
|
2008-01-01 04:24:36 -05:00
|
|
|
else if(is_pair(x)){
|
2006-11-23 19:38:26 -05:00
|
|
|
fprintf(fh, "(");
|
2008-01-02 20:58:48 -05:00
|
|
|
print(fh, ref(x, off_car));
|
|
|
|
ikptr d = ref(x, off_cdr);
|
2008-04-07 10:20:05 -04:00
|
|
|
/* fprintf(stderr, "d=0x%016lx\n", (long int)d); */
|
2006-11-23 19:38:26 -05:00
|
|
|
while(1){
|
2008-01-01 04:24:36 -05:00
|
|
|
if(is_pair(d)){
|
2006-11-23 19:38:26 -05:00
|
|
|
fprintf(fh, " ");
|
2008-01-02 20:58:48 -05:00
|
|
|
print(fh, ref(d, off_car));
|
|
|
|
d = ref(d, off_cdr);
|
2006-11-23 19:38:26 -05:00
|
|
|
}
|
2008-01-02 20:58:48 -05:00
|
|
|
else if(d == null_object){
|
2006-11-23 19:38:26 -05:00
|
|
|
fprintf(fh, ")");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(fh, " . ");
|
|
|
|
print(fh, d);
|
|
|
|
fprintf(fh, ")");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(tagof(x) == string_tag){
|
2007-12-23 13:37:48 -05:00
|
|
|
ikptr fxlen = ref(x, off_string_length);
|
2006-11-23 19:38:26 -05:00
|
|
|
int len = unfix(fxlen);
|
2008-04-09 03:05:19 -04:00
|
|
|
int * data = (int*)(x + off_string_data);
|
2006-11-23 19:38:26 -05:00
|
|
|
fprintf(fh, "\"");
|
|
|
|
int i;
|
|
|
|
for(i=0; i<len; i++){
|
2008-04-07 12:32:55 -04:00
|
|
|
char c = (data[i]) >> char_shift;
|
2006-11-23 19:38:26 -05:00
|
|
|
if((c == '\\') || (c == '"')){
|
|
|
|
fprintf(fh, "\\");
|
|
|
|
}
|
|
|
|
fprintf(fh, "%c", c);
|
|
|
|
}
|
|
|
|
fprintf(fh, "\"");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(fh, "#<unknown>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|