Grzegorz Jakacki wrote:
>> However, I must admit that I don't quite like the current design.
>> As different context require different loopup rules, an IMO better
>> approach would have been to provide a polymorphic 'Scope' type
>> and then implement it differently for 'Function', 'Class',
>> 'TemplateClass',
>> etc. Then each scope would know the rules of where to look if a symbol
>> isn't defined locally. This would be more flexible and in particular
>> allow to implement different lookup semantics depending on whether
>> we are parsing C++ or C. What do you think ?
>
>
> Generally looks good. Questions:
>
> * Scopes would replace Environments, right?
yes. 'Scope' would be an abstract base class with methods 'declare'
and 'lookup', as well as a hash for the locally declared symbols.
Subclasses would then implement 'lookup' to not only search locally
but in 'outer' scopes, too, according to the specific lookup semantics
of functions, classes, template classes, etc.
> * What would be connection between Scopes and other objects
> representing e.g. functions or classes ?
Scopes are associated with those objects that are, well, scopes.
Right now I would probably do this in a simple dictionary, but
once we are getting into the AST stuff we could rethink about
this.
> * Who would own Scopes?
Good question. Who's using them ? The parser of course, would
need them, and all the Encoding / TypeInfo instances need them.
May be we need to keep to maintain a 'scope graph' in parallel
to the parse tree / syntax tree. As there are arbitrary many
references to any given scope may be it's a good idea to make
scopes reference counted.
Regards,
Stefan
|