Changes in / [172d9342:f6964ef]


Ignore:
Location:
src/AST
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r172d9342 rf6964ef  
    1010// Created On       : Thu May 09 15::37::05 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri May 17 12:13:00 2019
    13 // Update Count     : 3
     12// Last Modified On : Fri May 17 16:01:00 2019
     13// Update Count     : 4
    1414//
    1515
     
    4141//================================================================================================
    4242class ConverterNewToOld : public ast::Visitor {
     43        BaseSyntaxNode * node = nullptr;
     44
     45        template<typename T>
     46        struct Getter {
     47                ConverterNewToOld & visitor;
     48
     49                template<typename U>
     50                T * accept1( const ast::ptr<U> & ptr ) {
     51                        ptr->accept( visitor );
     52                        T * ret = strict_dynamic_cast< T * >( visitor.node );
     53                        visitor.node = nullptr;
     54                        return ret;
     55                }
     56
     57                template<typename U>
     58                std::list< T * > acceptL( const U & container ) {
     59                        std::list< T * > ret;
     60                        for (auto ptr : container ) {
     61                                ret.emplace_back( accept1( ptr ) );
     62                        }
     63                        return ret;
     64                }
     65        };
     66
     67    template<typename T>
     68    Getter<T> get() {
     69        return Getter<T>{ *this };
     70    }
     71
     72        Label makeLabel(Statement * labelled, const ast::Label& label) {
     73                return Label(
     74                        label.name,
     75                        labelled,
     76                        get<Attribute>().acceptL(label.attributes)
     77                );
     78        }
     79
     80        template<template <class...> class C>
     81        std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
     82                std::list<Label> ret;
     83                for (auto label : labels) {
     84                        ret.push_back( makeLabel(labelled, label) );
     85                }
     86                return ret;
     87        }
     88
    4389public:
    44         template<typename T>
    45         T * get() {
    46                 T * ret = strict_dynamic_cast< T * >( node );
    47                 node = nullptr;
    48                 return ret;
     90        Declaration * decl( const ast::Decl * declNode ) {
     91                return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
    4992        }
    5093
    5194private:
    52         BaseSyntaxNode * node = nullptr;
    53 
    54         // All the visit functions:
    55 
    5695        const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
    5796                (void)node;
     
    105144
    106145        const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
    107                 (void)node;
     146                auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
     147                stmt->location = node->location;
     148                stmt->labels = makeLabelL( stmt, node->labels );
     149                this->node = stmt;
    108150                return nullptr;
    109151        }
    110152
    111153        const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
    112                 (void)node;
     154                auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) );
     155                stmt->location = node->location;
     156                stmt->labels = makeLabelL( stmt, node->labels );
     157                this->node = stmt;
    113158                return nullptr;
    114159        }
    115160
    116161        const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
    117                 (void)node;
     162                auto stmt = new AsmStmt(
     163                        node->isVolatile,
     164                        get<Expression>().accept1( node->instruction ),
     165                        get<Expression>().acceptL( node->output ),
     166                        get<Expression>().acceptL( node->input ),
     167                        get<ConstantExpr>().acceptL( node->clobber ),
     168                        makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
     169                );
     170                stmt->location = node->location;
     171                stmt->labels = makeLabelL( stmt, node->labels );
     172                this->node = stmt;
    118173                return nullptr;
    119174        }
    120175
    121176        const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
    122                 (void)node;
     177                auto stmt = new DirectiveStmt( node->directive );
     178                stmt->location = node->location;
     179                stmt->labels = makeLabelL( stmt, node->labels );
     180                this->node = stmt;
    123181                return nullptr;
    124182        }
    125183
    126184        const ast::Stmt * visit( const ast::IfStmt * node ) override final {
    127                 (void)node;
     185                auto stmt = new IfStmt(
     186                        get<Expression>().accept1( node->cond ),
     187                        get<Statement>().accept1( node->thenPart ),
     188                        get<Statement>().accept1( node->elsePart ),
     189                        get<Statement>().acceptL( node->inits )
     190                );
     191                stmt->location = node->location;
     192                stmt->labels = makeLabelL( stmt, node->labels );
     193                this->node = stmt;
     194                return nullptr;
     195        }
     196
     197        const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
     198                auto stmt = new SwitchStmt(
     199                        get<Expression>().accept1( node->cond ),
     200                        get<Statement>().acceptL( node->stmts )
     201                );
     202                stmt->location = node->location;
     203                stmt->labels = makeLabelL( stmt, node->labels );
     204                this->node = stmt;
     205                return nullptr;
     206        }
     207
     208        const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
     209                auto stmt = new CaseStmt(
     210                        get<Expression>().accept1( node->cond ),
     211                        get<Statement>().acceptL( node->stmts ),
     212                        node->isDefault()
     213                );
     214                stmt->location = node->location;
     215                stmt->labels = makeLabelL( stmt, node->labels );
     216                this->node = stmt;
    128217                return nullptr;
    129218        }
    130219
    131220        const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
    132                 (void)node;
     221                auto inits = get<Statement>().acceptL( node->inits );
     222                auto stmt = new WhileStmt(
     223                        get<Expression>().accept1( node->cond ),
     224                        get<Statement>().accept1( node->body ),
     225                        inits,
     226                        node->isDoWhile
     227                );
     228                stmt->location = node->location;
     229                stmt->labels = makeLabelL( stmt, node->labels );
     230                this->node = stmt;
    133231                return nullptr;
    134232        }
    135233
    136234        const ast::Stmt * visit( const ast::ForStmt * node ) override final {
    137                 (void)node;
    138                 return nullptr;
    139         }
    140 
    141         const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
    142                 (void)node;
    143                 return nullptr;
    144         }
    145 
    146         const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
    147                 (void)node;
     235                auto stmt = new ForStmt(
     236                        get<Statement>().acceptL( node->inits ),
     237                        get<Expression>().accept1( node->cond ),
     238                        get<Expression>().accept1( node->inc ),
     239                        get<Statement>().accept1( node->body )
     240                );
     241                stmt->location = node->location;
     242                stmt->labels = makeLabelL( stmt, node->labels );
     243                this->node = stmt;
    148244                return nullptr;
    149245        }
    150246
    151247        const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
    152                 (void)node;
     248                BranchStmt * stmt;
     249                if (node->computedTarget) {
     250                        stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
     251                                BranchStmt::Goto );
     252                } else {
     253                        BranchStmt::Type type;
     254                        switch (node->kind) {
     255                        #define CASE(n) \
     256                        case ast::BranchStmt::n: \
     257                                type = BranchStmt::n; \
     258                                break
     259                        CASE(Goto);
     260                        CASE(Break);
     261                        CASE(Continue);
     262                        CASE(FallThrough);
     263                        CASE(FallThroughDefault);
     264                        #undef CASE
     265                        default:
     266                                assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
     267                        }
     268
     269                        // The labels here are also weird.
     270                        stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
     271                        stmt->target = makeLabel( stmt, node->target );
     272                }
     273                stmt->location = node->location;
     274                stmt->labels = makeLabelL( stmt, node->labels );
     275                this->node = stmt;
    153276                return nullptr;
    154277        }
    155278
    156279        const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
    157                 (void)node;
     280                auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
     281                stmt->location = node->location;
     282                stmt->labels = makeLabelL( stmt, node->labels );
     283                this->node = stmt;
    158284                return nullptr;
    159285        }
    160286
    161287        const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
    162                 (void)node;
     288                ThrowStmt::Kind kind;
     289                switch (node->kind) {
     290                case ast::ThrowStmt::Terminate:
     291                        kind = ThrowStmt::Terminate;
     292                        break;
     293                case ast::ThrowStmt::Resume:
     294                        kind = ThrowStmt::Resume;
     295                        break;
     296                default:
     297                        assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
     298                }
     299                auto stmt = new ThrowStmt(
     300                        kind,
     301                        get<Expression>().accept1( node->expr ),
     302                        get<Expression>().accept1( node->target )
     303                );
     304                stmt->location = node->location;
     305                stmt->labels = makeLabelL( stmt, node->labels );
     306                this->node = stmt;
    163307                return nullptr;
    164308        }
    165309
    166310        const ast::Stmt * visit( const ast::TryStmt * node ) override final {
    167                 (void)node;
     311                auto handlers = get<CatchStmt>().acceptL( node->handlers );
     312                auto stmt = new TryStmt(
     313                        get<CompoundStmt>().accept1( node->body ),
     314                        handlers,
     315                        get<FinallyStmt>().accept1( node->finally )
     316                );
     317                stmt->location = node->location;
     318                stmt->labels = makeLabelL( stmt, node->labels );
     319                this->node = stmt;
    168320                return nullptr;
    169321        }
    170322
    171323        const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
    172                 (void)node;
     324                CatchStmt::Kind kind;
     325                switch (node->kind) {
     326                case ast::CatchStmt::Terminate:
     327                        kind = CatchStmt::Terminate;
     328                        break;
     329                case ast::CatchStmt::Resume:
     330                        kind = CatchStmt::Resume;
     331                        break;
     332                default:
     333                        assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
     334                }
     335                auto stmt = new CatchStmt(
     336                        kind,
     337                        get<Declaration>().accept1( node->decl ),
     338                        get<Expression>().accept1( node->cond ),
     339                        get<Statement>().accept1( node->body )
     340                );
     341                stmt->location = node->location;
     342                stmt->labels = makeLabelL( stmt, node->labels );
     343                this->node = stmt;
    173344                return nullptr;
    174345        }
    175346
    176347        const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
    177                 (void)node;
     348                auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
     349                stmt->location = node->location;
     350                stmt->labels = makeLabelL( stmt, node->labels );
     351                this->node = stmt;
    178352                return nullptr;
    179353        }
    180354
    181355        const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
    182                 (void)node;
     356                auto stmt = new WaitForStmt;
     357                stmt->clauses.reserve( node->clauses.size() );
     358                for ( auto clause : node->clauses ) {
     359                        stmt->clauses.push_back({{
     360                                        get<Expression>().accept1( clause.target.function ),
     361                                        get<Expression>().acceptL( clause.target.arguments ),
     362                                },
     363                                get<Statement>().accept1( clause.stmt ),
     364                                get<Expression>().accept1( clause.cond ),
     365                        });
     366                }
     367                stmt->timeout = {
     368                        get<Expression>().accept1( node->timeout.time ),
     369                        get<Statement>().accept1( node->timeout.stmt ),
     370                        get<Expression>().accept1( node->timeout.cond ),
     371                };
     372                stmt->orelse = {
     373                        get<Statement>().accept1( node->orElse.stmt ),
     374                        get<Expression>().accept1( node->orElse.cond ),
     375                };
     376                stmt->location = node->location;
     377                stmt->labels = makeLabelL( stmt, node->labels );
     378                this->node = stmt;
    183379                return nullptr;
    184380        }
    185381
    186382        const ast::Stmt * visit( const ast::WithStmt * node ) override final {
    187                 (void)node;
     383                auto stmt = new WithStmt(
     384                        get<Expression>().acceptL( node->exprs ),
     385                        get<Statement>().accept1( node->stmt )
     386                );
     387                stmt->location = node->location;
     388                stmt->labels = makeLabelL( stmt, node->labels );
     389                this->node = stmt;
    188390                return nullptr;
    189391        }
    190392
    191393        const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
    192                 (void)node;
     394                auto stmt = new NullStmt();
     395                stmt->location = node->location;
     396                stmt->labels = makeLabelL( stmt, node->labels );
     397                this->node = stmt;
    193398                return nullptr;
    194399        }
    195400
    196401        const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
    197                 (void)node;
     402                auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
     403                stmt->location = node->location;
     404                stmt->labels = makeLabelL( stmt, node->labels );
     405                this->node = stmt;
    198406                return nullptr;
    199407        }
     
    514722        std::list< Declaration * > decls;
    515723        for(auto d : translationUnit) {
    516                 d->accept( c );
    517                 decls.emplace_back( c.get<Declaration>() );
     724                decls.emplace_back( c.decl( d ) );
    518725                delete d;
    519726        }
  • src/AST/Stmt.hpp

    r172d9342 rf6964ef  
    1010// Created On       : Wed May  8 13:00:00 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri May 17 10:03:00 2019
    13 // Update Count     : 4
     12// Last Modified On : Fri May 17 12:45:00 2019
     13// Update Count     : 5
    1414//
    1515
     
    175175        : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
    176176
    177         bool isDefault() { return !cond; }
     177        bool isDefault() const { return !cond; }
    178178
    179179        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
Note: See TracChangeset for help on using the changeset viewer.