« first day (1597 days earlier)      last day (1247 days later) » 

user12698128
12:19 PM
@PeterT great, thanks
 
5:30 PM
how to "check" if a std::shared_ptr can be dynamically casted to something else?
I just need to check; I don't need the casted pointer.
I am currently wrapping std::dynamic_pointer_cast around a try-catch block but this seems overkill.
 
nwp
Why not use a regular dynamic_cast which returns a nullptr on fail?
 
@nwp that would also work but I was wondering if there was a nicer way to do it
    auto ifBody = stmt.getIfBody();
    try
    {
        // single statement if
        std::dynamic_pointer_cast<ast::CompoundStatement>(ifBody);
        ifBody->accept(*this);
    }
    catch(const std::bad_cast&)
    {
        IndentTracker itracker(indent);
        stmt.getIfBody()->accept(*this);
    }
 
nwp
if (dynamic_cast<ast::CompoundStatement*>(ifBody)) {
    ifBody->accept(*this);
}
else {
    IndentTracker itracker(indent);
    stmt.getIfBody()->accept(*this);
}
 
@nwp actually this is quite neat
 
nwp
Looks reasonable enough to me.
 
5:38 PM
yeah, that's what I am going to do
very reasonable; not sure why I didn't think of it
 
nwp
Also nice, I'm writing a parser too at the moment.
Though, do consider using ANTLR.
 
@nwp I am using it to generate the parse tree. I have manually constructed an AST from the parse tree. I don't think ANTLR4 can generate ASTs directly.
also ANTLR4 doesn't seem to be working with C++20 (lots of errors in ANTLR4 generated code with -std=c++2a)
 
nwp
Dunnow. I thought you'd just attach actions on the different tokens and don't need an AST.
 
 
5 hours later…
nwp
10:47 PM
> -gt Generate code for Abstract-Syntax Trees. (source)
 

« first day (1597 days earlier)      last day (1247 days later) »