Writing a printList method for a Scheme interpreter in C
        Posted  
        
            by 
                Rehan Rasool
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Rehan Rasool
        
        
        
        Published on 2012-10-22T02:03:04Z
        Indexed on 
            2012/10/23
            5:04 UTC
        
        
        Read the original article
        Hit count: 254
        
I am new to C and working on making an interpreter for Scheme. I am trying to get a suitable printList method to traverse through the structure.
The program takes in an input like:
(a (b c))
and internally represent it as:
[""][ ][ ]-->  [""][ ][/]
     |              |              
   ["A"][/][/]     [""][ ][ ]-->  [""][ ][/]     
                        |              |                 
                      ["B"][/][/]    ["C"][/][/]
Right now, I just want the program to take in the input, make the appropriate cell structure internally and print out the cell structure, thereby getting
(a (b c))
at the end.
Here is my struct:
typedef struct conscell *List;
struct conscell {
char symbol;
struct conscell *first;
struct conscell *rest;
};
void printList(char token[20]){
    List current = S_Expression(token, 0);
     printf("(");
printf("First Value? %c \n", current->first->symbol);
printf("Second value? %c \n", current->rest->first->first->symbol);
printf("Third value? %c \n", current->rest->first->rest->first->symbol);
printf(")");
}
In the main method, I get the first token and call:
printList(token);
I tested the values again for the sublists and I think it is working. However, I will need a method to traverse through the whole structure. Please look at my printList code again. The print calls are what I have to type, to manually get the (a (b c)) list values. So I get this output:
First value? a
First value? b
First value? c
It is what I want, but I want a method to do it using a loop, no matter how complex the structure is, also adding brackets where appropriate, so in the end, I should get:
(a (b c))
which is the same as the input.
Can anyone please help me with this?
© Stack Overflow or respective owner