Changeset 73d0e3f4


Ignore:
Timestamp:
Jun 29, 2024, 5:22:36 PM (3 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
011c29e
Parents:
115ac1ce
Message:

Current enum has bug when use as a reference as in ?++. Change TranslateEnumRange? to use succ and pred instead. Will look back into the reference problem

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/enum.cfa

    r115ac1ce r73d0e3f4  
    2323    int ?>?(E l, E r) { return posn(l) > posn(r); }
    2424    int ?>=?(E l, E r) { return posn(l) >= posn(r); }
     25
     26    E ++?(E& l) {
     27        l = succ(l);
     28        return l;
     29    }
     30   
     31    E ?++(E& l) {
     32        E ret = l;
     33        l = succ(l);
     34        return ret;
     35    }
     36
     37    E --?(E& l) {
     38        l = pred(l);
     39        return l;
     40    }
     41
     42    E ?--(E& l) {
     43        E ret = l;
     44        l = pred(l);
     45        return ret;
     46    }
    2547}
  • libcfa/src/enum.hfa

    r115ac1ce r73d0e3f4  
    4949    int ?>?(E, E);
    5050    int ?>=?(E, E);
     51
     52    int ++?(E&);
     53    int ?++(E&);
     54    int --?(E&);
     55    int ?--(E&);
    5156}
  • src/ControlStruct/TranslateEnumRange.cpp

    r115ac1ce r73d0e3f4  
    2727
    2828        auto objInit = stmt->inits.front();
    29 
    3029        auto initLocation = objInit->location;
    3130        auto rangeLocation = stmt->range_over->location;
     
    6160                if ( !objDecl->init ) {
    6261                    auto location = stmt->location;
    63                     ast::SingleInit * newInit;
    64                     newInit = new ast::SingleInit( location,
     62                    // auto newInit = new ast::SingleInit( location, new ast::NameExpr( location, enumDecl->members.front()->name ) );
     63                    ast::SingleInit * newInit = new ast::SingleInit( location,
    6564                        new ast::NameExpr( location,
    66                         stmt->is_inc? enumDecl->members.front()->name: enumDecl->members.back()->name ) );
     65                           stmt->is_inc? enumDecl->members.front()->name: enumDecl->members.back()->name ) );
    6766                    auto objDeclWithInit = ast::mutate_field( objDecl, &ast::ObjectDecl::init, newInit );
    6867                    auto declWithInit = ast::mutate_field( declStmt, &ast::DeclStmt::decl, objDeclWithInit );
     
    8584    auto enumInst = typeExpr->type.strict_as<ast::EnumInstType>();
    8685    auto enumDecl = enumInst->base;
    87    
     86
    8887    // Both inc and dec check if the current posn less than the number of enumerator
    8988    // for dec, it keeps call pred until it passes 0 (the first enumerator) and underflow,
     
    9392        {new ast::CastExpr(location,
    9493            new ast::NameExpr( location, indexName ),
    95             new ast::BasicType( ast::BasicKind::UnsignedInt ),
    96             ast::GeneratedFlag::ExplicitCast),
     94            new ast::BasicType( ast::BasicKind::UnsignedInt
     95        ),ast::GeneratedFlag::ExplicitCast),
    9796        ast::ConstantExpr::from_ulong( location, enumDecl->members.size()-1 ) });
    98 
    9997    auto increment = ast::UntypedExpr::createCall( location,
    100         stmt->is_inc? "?++": "?--",
    101         { new ast::NameExpr( location, indexName ) });
    102    
     98        stmt->is_inc? "succ": "pred",{
     99        new ast::NameExpr( location, indexName )
     100    });
    103101    auto assig = ast::UntypedExpr::createAssign( location, new ast::NameExpr( location, indexName ), increment );
    104102    auto mut = ast::mutate_field( stmt, &ast::ForStmt::cond, condition );
Note: See TracChangeset for help on using the changeset viewer.