Writing a printList method for a Scheme interpreter in C
- by Rehan Rasool
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?