lundi 4 mai 2015

Visitor Design Pattern with pure functions?

I am writing a program in C++ that parses JSON files and interprets the result in order to create a data structure. The content of the JSON files triggers creation of data structures (i.e. arrays), as well as functions that perform some processing on the data structure I am building.

It is similar to parsing a programming language, sometimes you want to create new variables, and sometimes you just want to compute a value from different variables.

The problem

What I do is that I have an Abstract Syntax Tree, and I want to explore it and build a complex data structure considering what is in the syntax tree.

To do so, I decided to implement my "interpreter" using the Visitor design pattern.

What disturbs me is that the Visitor pattern heavily relies on side effects, since all visit() functions return void. I have to rely on a state of the visitor. And, well, I do not like side effects. I already ran into problems related to the order of executions of functions.

What I found

I saw that it was possible to use dynamic_cast to test over the type of the element I am passing as a parameter, but that would mean writing a massive function with many if-then-else statements, I am not sure it would be easier to debug, and I'm not certain I would get rid of the state.

Another option would be to return, for each one of the visit() function, the data structure I am building. But then, I would have to pass it as a parameter as well, and that would mean passing it as a parameter of accept as well and then my visitor becomes much less transparent.

Question

Is there a way to achieve something similar (i.e. explore a hierarchy of classes while building a structure), using only pure functions?

Aucun commentaire:

Enregistrer un commentaire