I am looking for a way to implement an S-expression reader (to be used later on with both an Scheme interpreter and a compiler), but I've been asking myself how (if at all) I should write an AST for it.
I've been reading SICP, and this is quite straightforward from within Scheme, but I'm looking to implement the interpreter and compiler in C++, in OO fashion.
Please, keep in mind that I'm doing this only for learning purposes, so I'm not really looking for the easiest or quickest way of doing this, but rather the correct and reusable way of doing it.
I've seen in some Scheme implementations that people parse s-expressions and readily output cons cells, something like this:
struct Sexpr
{
};
struct Cons : public Sexpr
{
Sexpr* left;
Sexpr* right;
};
struct IntAtom : Sexpr
{
int value;
};
And one subclass of Sexpr for each kind of Scheme Atom
, or something along those lines.
I'm not sure, but this seems like a hack to me... Shouldn't this work be done by an interpreter rather than the reader?
What I want to know is if this is considered the best (or correct) way of reading S-expressions, or is this more a job of the interpreter than the parser? Should the parser have its own AST instead of relying on cons cells?