Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/TranslateEnumRange.cpp

    rd66a43b r5ccc733  
    2626        auto enumDecl = enumInst->base;
    2727
    28         auto init = stmt->inits.front();
     28        auto objInit = stmt->inits.front();
    2929
    30         if (auto declStmt = init.as<ast::DeclStmt>()) {
     30        auto initLocation = objInit->location;
     31        auto rangeLocation = stmt->range_over->location;
     32        assert( stmt->inits.size() == 1 );
     33
     34        if (auto declStmt = objInit.as<ast::DeclStmt>()) {
    3135            auto decl = declStmt->decl;
    3236            if ( auto objDecl = decl.as<ast::ObjectDecl>()) {
     
    5761                if ( !objDecl->init ) {
    5862                    auto location = stmt->location;
    59                     auto newInit = new ast::SingleInit( location, new ast::NameExpr( location, enumDecl->members.front()->name ) );
     63                    ast::SingleInit * newInit;
     64                    newInit = new ast::SingleInit( location,
     65                        new ast::NameExpr( location,
     66                        stmt->is_inc? enumDecl->members.front()->name: enumDecl->members.back()->name ) );
    6067                    auto objDeclWithInit = ast::mutate_field( objDecl, &ast::ObjectDecl::init, newInit );
    6168                    auto declWithInit = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithInit );
     
    7885    auto enumInst = typeExpr->type.strict_as<ast::EnumInstType>();
    7986    auto enumDecl = enumInst->base;
     87   
     88    // Both inc and dec check if the current posn less than the number of enumerator
     89    // for dec, it keeps call pred until it passes 0 (the first enumerator) and underflow,
     90    // it wraps around and become unsigned max
     91    ast::UntypedExpr * condition = ast::UntypedExpr::createCall( location,
     92        "?<=?",
     93        {new ast::CastExpr(location,
     94            new ast::NameExpr( location, indexName ),
     95            new ast::BasicType( ast::BasicKind::UnsignedInt ),
     96            ast::GeneratedFlag::ExplicitCast),
     97        ast::ConstantExpr::from_ulong( location, enumDecl->members.size()-1 ) });
    8098
    81     auto condition = ast::UntypedExpr::createCall( location, "?<=?", {
    82         new ast::NameExpr( location, indexName ),
    83         // ast::ConstantExpr::from_ulong( location, enumDecl->members.size() )
    84         new ast::NameExpr( location, enumDecl->members.back()->name )
    85     });
    86     auto increment = ast::UntypedExpr::createCall( location, "succ", {
    87         new ast::NameExpr( location, indexName )
    88     });
     99    auto increment = ast::UntypedExpr::createCall( location,
     100        stmt->is_inc? "?++": "?--",
     101        { new ast::NameExpr( location, indexName ) });
     102   
    89103    auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment );
    90104    auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition );
Note: See TracChangeset for help on using the changeset viewer.