A s-expr interpreter in #cplusplus

I have been working on my interpreter, and switched from parsing BASIC to parsing s-expr. The code is available on github. It’s crude, to be sure, and the inputs are fixed, but it does handle parse trees.

You can define variables, and it has a single arithmetic operator: addition. It’s really just a proof-of-concept. It’s quite a large amount of code, at just over 450 lines. I would really like to reduce the code base.

My goal is to come up with a kind of “pre-Scheme”, a language from which parsers can be created. I had planned to make the functionality extensible, but so far I have hard-coded the funtion definitions into blang_apply(). I am confident that the code could easily accommodate my proposed modifications.

The main class is “cell”, which holds a variant type. I actually used the C++ 2017 variant type at one point, but I couldn’t figure out how to set a value. I have abandoned that representation, as I grew frustrated with trying to make it work.

One thing I notice about C++ is that I often find that “some hammering is required”. Sum types (aka algebraic data types) fit naturally into Haskell syntax.  C++ seems to require a lot more effort.

This is an interesting philosophical question: should C++ by expanded by classes, or by syntax extensions? Classes certainly have one big advantage: you don’t need a new compiler. But they do have the disadvantage in that syntax can be easier to work with. After all, if product types are part of the language, then why not sum types?

Regardless of that, I now think my cell class is too unwieldy. Just as I planned to have a plug-in functions, I now think it is a good idea to have plug-in types.

One approach to types would be to use C++ class hierarchies to achieve polymorphism. I think that is the wrong approach, though. That would make it more difficult to extend dynamically. I prefer the idea of having “slots” for construction and destruction.

The current implementation keeps with the tradition of its previous incarnation as regards to deterministic garbage collection. No garbage collector is necessary.

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s