Added printing functions.

Pascal J. Bourguignon [2013-02-01 01:40]
Added printing functions.
Filename
ast.c
diff --git a/ast.c b/ast.c
index ea47429..c2ed065 100644
--- a/ast.c
+++ b/ast.c
@@ -118,6 +118,7 @@ void error_signal(const char* fname,const char* message){



+
 typedef struct object {
     object_type type;
     union {
@@ -263,12 +264,12 @@ object* last   (object* list){
     return(list);}

 object* nth(int n,object* list){
-    while((0<n) and list){
+    while((0<n--) and list){
         list=cdr(list);}
     return(car(list));}

 object* nthcdr(int n,object* list){
-    while((0<n) and list){
+    while((0<--n) and list){
         list=cdr(list);}
     return(list);}

@@ -810,6 +811,103 @@ int ceiling_to_nearest_prime(int n){



+
+
+object* prin1_object(object* object,FILE* file);
+object* print_list(object* object,FILE* file){
+    if(null(object)){
+        fprintf(file,"nil");
+    }else{
+        fprintf(file,"(");
+        prin1_object(car(object),file);
+        while(consp(object=cdr(object))){
+            fprintf(file," ");
+            prin1_object(car(object),file);}
+        if(not(null(object))){
+            fprintf(file," . ");
+            prin1_object(object,file);}
+        fprintf(file,")");}
+    return(object);}
+
+
+object* print_string(object* object,FILE* file){
+    fprintf(file,"\"");
+    vector* v=vector_value(object);
+    int count=vector_length(v);
+    for(int i=0;i<count;i++){
+        char ch=vector_get_char(v,i);
+        if(('"'==ch)or('\\'==ch)){
+            fprintf(file,"\\%c",ch);
+        }else{
+            fprintf(file,"%c",ch);}}
+    fprintf(file,"\"");
+    return(object);}
+
+
+object* print_vector(object* object,FILE* file){
+    vector* v=vector_value(object);
+    int count=vector_length(v);
+    const char* sep="";
+    switch(vector_element_type(v)){
+      case ot_char:
+          print_string(object,file);
+          break;
+      case ot_int:
+          fprintf(file,"#(");
+          for(int i=0;i<count;i++){
+              fprintf(file,"%s%d",sep,vector_get_int(v,i));
+              sep=" ";}
+          fprintf(file,")");
+          break;
+      case ot_t:
+          fprintf(file,"#(");
+          for(int i=0;i<count;i++){
+              prin1_object(vector_get(v,i),file);
+              sep=" ";}
+          fprintf(file,")");
+          break;
+      default:
+          fprintf(file,"#<vector>");
+          break;
+    }
+    return(object);}
+
+
+object* prin1_object(object* object,FILE* file){
+    if(null(object)){
+        fprintf(file,"nil");
+    }else{
+        switch(type_of(object)){
+          case ot_CXCursor:
+              fprintf(file,"#<CXCursor %d>",sxhash(object));
+              break;
+          case ot_int:
+              fprintf(file,"%d",integer_value(object));
+              break;
+          case ot_char:
+              fprintf(file,"#\%c",character_value(object));
+              break;
+          case ot_cons:
+              print_list(object,file);
+              break;
+          case ot_vector:
+              if(stringp(object)){
+                  print_string(object,file);
+              }else{
+                  print_vector(object,file);
+              }
+              break;
+          case ot_hashtable:
+              fprintf(file,"#<hashtable :count %d>",hashtable_count(hashtable_value(object)));
+              break;
+          default:
+              fprintf(file,"#<OBJECT-OF-UNKNOWN-TYPE>");
+              break;
+        }}
+    return(object);}
+
+
+
 //// arguments

 int argv_count(const char* const* argv){
@@ -1110,7 +1208,7 @@ object* cxstring(CXString theCXString){
     clang_disposeString(theCXString);
     return(result);}

-void print_string(CXString string){
+void print_cxstring(CXString string){
     const char* cstring=clang_getCString(string);
     if(nil==cstring){
         printf("nil");
@@ -1130,7 +1228,7 @@ void print_location(CXSourceLocation location){
     if(nil==file){
         printf("nil");
     }else{
-        print_string(clang_getFileName(file));
+        print_cxstring(clang_getFileName(file));
     }
     printf(" :line %u :column %u :offset %u)",line,column,offset);}

@@ -1335,7 +1433,7 @@ void print_floating_literal(CXCursor cursor,int margin){
 void print_imaginary_literal(CXCursor cursor,int margin){
 }

-void print_string_literal(CXCursor cursor,int margin){
+void print_cxstring_literal(CXCursor cursor,int margin){
 }

 void print_character_literal(CXCursor cursor,int margin){
@@ -1657,7 +1755,7 @@ printer cursor_printer(int cursorKind){
       case CXCursor_IntegerLiteral:                     return print_integer_literal;
       case CXCursor_FloatingLiteral:                    return print_floating_literal;
       case CXCursor_ImaginaryLiteral:                   return print_imaginary_literal;
-      case CXCursor_StringLiteral:                      return print_string_literal;
+      case CXCursor_StringLiteral:                      return print_cxstring_literal;
       case CXCursor_CharacterLiteral:                   return print_character_literal;
       case CXCursor_ParenExpr:                          return print_paren_expr;
       case CXCursor_UnaryOperator:                      return print_unary_operator;
@@ -1820,7 +1918,7 @@ void print_slots(CXCursor cxcursor,int kind,int margin){
     if(includedFile){
         newline(margin);
         printf(" :included-file ");
-        print_string(clang_getFileName(includedFile));
+        print_cxstring(clang_getFileName(includedFile));
     }


@@ -2050,10 +2148,9 @@ enum CXChildVisitResult visit_cursor_collect_children(CXCursor cxcursor,CXCursor

 void register_cursors(hashtable* cursors,CXCursor rootCursor){
     visit_cursor_closure closure;
-    closure.visited=make_hashtable(10000);
+    closure.visited=cursors;
     closure.linkedCursors=nil;
-    visit_cursor_collect_children(rootCursor,clang_getNullCursor(),&closure);
-}
+    visit_cursor_collect_children(rootCursor,clang_getNullCursor(),&closure);}

 typedef struct{
     bool present;
ViewGit