I am developing an interpreter of a simple programming language.
- There are tree-structured dynamic data, where each of the data nodes has an associated (owned) type-object.
- There are several kinds of the types - ListType, StructType etc. - and one special - TypeRef. The TypeRef holds a name (a string) which refers to some concrete type.
- There is a (nearly) global map (dictionary) - I call it the environment - (
std::map<string, Type*>
) that translates type-names into type-objects.
The problem is, that there might be several programs loaded and each of them might have different types associated with the same names. This single assumption makes it impossible to have one global (static) environment, which would otherwise be a perfect solution.
Therefore, it seems that I need either (#1) a pointer to an environment in each type-object or (#2) to perform every operation in the context of the environment (e.g. by providing it everywhere as the first argument).
The problems I see:
(#1) Information redundancy, because all connected data nodes would have the same environment. Environments would only be different for strictly separate data.
(#2) A lot of trivial passing of the environment to subroutines, obfuscating the code.
It seems to me that this problem matches a general pattern I would call nearly static data. Any ideas what would be the best solution?