|
Boost Users : |
Subject: Re: [Boost-users] How to implement a visitor pattern over an AST
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2018-12-10 20:43:59
AMDG
On 12/10/2018 01:14 PM, Michael Powell via Boost-users wrote:
> On Mon, Dec 10, 2018 at 3:00 PM Steven Watanabe via Boost-users
> <boost-users_at_[hidden]> wrote:
>>
>> On 12/10/2018 12:04 PM, Michael Powell via Boost-users wrote:
>>>
>>> I have an AST successfully building, generating text file, and
>>> subsequently Spirit Qi parsing for verification purposes.
>>>
>>> Now I want to implement a visitor pattern over that AST, somehow...
>>> For "calculators" and such, "evaluating" those expressions is one
>>> thing; however, this AST is not that. It is closer akin to a parsed
>>> Xml or Json model/tree.
>>>
>>> In other words, I'd like for it to feel sort of like an iterator if
>>> that's even possible. It should have contextual awareness where it is
>>> at all times, etc, perhaps "incrementing" is a depth first analysis.
>>>
>>
>> It's possible to do this, but making an iterator
>> over a tree is quite a bit more complex than just
>> using a depth-first visitation whenever you need
>> to process the tree.
>
> It is; I actually have a similar pattern in a "string rendering
> visitor" in which I have internally specialized template methods for
> the particular nodes of visitation. One might call that a unary
> visitor, given a single operand.
>
> In this case, I rinse and repeat given the binary case, two operands:
>
> template<typename T>
> bool equals(const T& x, const T& y) const {
> const auto message = "Comparator type '" +
> std::string(typeid(T).name()) + "' unsupported";
> throw std::exception(message.c_str());
> }
>
> template<>
> bool equals<ast::TopLevelType>(const ast::TopLevelType& x, const
> ast::TopLevelType& y) const {
> // ...
> }
>
> And so on... Which also guarantees that I am also walking both tree's
> at the same moment and in like fashion.
>
Yep. Although you can implement tree iterators
using coroutines as demonstrated here:
you lose information about the tree structure. This is
a good thing in that example, but not so much for your
use case.
In Christ,
Steven Watanabe
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net