Changeset 525f7ad for src/Parser


Ignore:
Timestamp:
Jun 19, 2024, 3:20:39 PM (18 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
57e43cd
Parents:
1725989
Message:
  1. Add count_e( enum_name ), a pseudo function that return the number of element in an enum; 2. Implementation of enum range loop.
Location:
src/Parser
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cpp

    r1725989 r525f7ad  
    211211        buildMoveList( forctl->init, astinit );
    212212
     213        if ( forctl->range_over ) {
     214                ast::Expr * range_over = maybeMoveBuild( forctl->range_over );
     215                delete forctl;
     216                return new ast::ForStmt( location,
     217                        std::move( astinit ),
     218                        range_over,
     219                        buildMoveSingle( stmt ),
     220                        buildMoveOptional( else_ )
     221                );
     222        }
     223
    213224        ast::Expr * astcond = nullptr;                                          // maybe empty
    214225        astcond = maybeMoveBuild( forctl->condition );
  • src/Parser/StatementNode.hpp

    r1725989 r525f7ad  
    6363struct ForCtrl {
    6464        ForCtrl( StatementNode * stmt, ExpressionNode * condition, ExpressionNode * change ) :
    65                 init( stmt ), condition( condition ), change( change ) {}
     65                init( stmt ), condition( condition ), change( change ), range_over( nullptr ) {}
     66        ForCtrl( StatementNode * decl, ExpressionNode * _range_over) :
     67                init( decl ), condition( nullptr ), change( nullptr ),  range_over( _range_over ) {}
    6668
    6769        StatementNode * init;
    6870        ExpressionNode * condition;
    6971        ExpressionNode * change;
     72        ExpressionNode * range_over;
    7073};
    7174
  • src/Parser/lex.ll

    r1725989 r525f7ad  
    321321__signed__              { KEYWORD_RETURN(SIGNED); }                             // GCC
    322322sizeof                  { KEYWORD_RETURN(SIZEOF); }
     323__count_e__             { KEYWORD_RETURN(COUNT); }                              // GCC
    323324static                  { KEYWORD_RETURN(STATIC); }
    324325_Static_assert  { KEYWORD_RETURN(STATICASSERT); }               // C11
  • src/Parser/parser.yy

    r1725989 r525f7ad  
    284284} // forCtrl
    285285
     286ForCtrl * enumRangeCtrl( ExpressionNode * index_expr, ExpressionNode * range_over_expr ) {
     287        if ( auto identifier = dynamic_cast<ast::NameExpr *>(index_expr->expr.get()) ) {
     288                DeclarationNode * indexDecl =
     289                        DeclarationNode::newName( new std::string(identifier->name) );
     290                assert( range_over_expr );
     291                auto node = new StatementNode( indexDecl ); // <- this cause this error
     292                return new ForCtrl( node, range_over_expr );
     293        } else if (auto commaExpr = dynamic_cast<ast::CommaExpr *>( index_expr->expr.get() )) {
     294                if ( auto identifier = commaExpr->arg1.as<ast::NameExpr>() ) {
     295                        assert( range_over_expr );
     296                        DeclarationNode * indexDecl = distAttr(
     297                                DeclarationNode::newTypeof( range_over_expr, true ),
     298                                DeclarationNode::newName( new std::string( identifier->name) ) );
     299                        return new ForCtrl( new StatementNode( indexDecl ), range_over_expr );
     300                } else {
     301                        SemanticError( yylloc, "syntax error, loop-index name missing. Expression disallowed." ); return nullptr;
     302                } // if
     303        } else {
     304                SemanticError( yylloc, "syntax error, loop-index name missing. Expression disallowed." ); return nullptr;
     305        } // if
     306} // enumRangeCtrl
     307
    286308static void IdentifierBeforeIdentifier( string & identifier1, string & identifier2, const char * kind ) {
    287309        SemanticError( yylloc, "syntax error, adjacent identifiers \"%s\" and \"%s\" are not meaningful in an %s.\n"
     
    368390%token DECIMAL32 DECIMAL64 DECIMAL128                                   // GCC
    369391%token ZERO_T ONE_T                                                                             // CFA
    370 %token SIZEOF TYPEOF VA_LIST VA_ARG AUTO_TYPE                   // GCC
     392%token SIZEOF TYPEOF VA_LIST VA_ARG AUTO_TYPE COUNT             // GCC
    371393%token OFFSETOF BASETYPEOF TYPEID                                               // CFA
    372394%token ENUM STRUCT UNION
     
    968990                        // $$ = new ExpressionNode( build_offsetOf( $3, build_varref( $5 ) ) );
    969991                }
     992        | COUNT '(' type ')'
     993                {
     994                        // SemanticError( yylloc, "Count is currently unimplemented. "); $$ = nullptr;
     995                        $$ = new ExpressionNode( new ast::CountExpr( yylloc, maybeMoveBuildType( $3 ) ) );
     996                }
    970997        ;
    971998
     
    15991626                { SemanticError( yylloc, "syntax error, missing low/high value for up/down-to range so index is uninitialized." ); $$ = nullptr; }
    16001627
    1601         | comma_expression ';' enum_key                                         // CFA, enum type
    1602                 {
    1603                         SemanticError( yylloc, "Type iterator is currently unimplemented." ); $$ = nullptr;
    1604                         //$$ = forCtrl( new ExpressionNode( build_varref( $3 ) ), $1, nullptr, OperKinds::Range, nullptr, nullptr );
    1605                 }
    1606         | comma_expression ';' downupdowneq enum_key            // CFA, enum type, reverse direction
     1628        | comma_expression ';' enum_type                                        // CFA, enum type
     1629                {
     1630                        $$ = enumRangeCtrl( $1, new ExpressionNode( new ast::TypeExpr(yylloc, $3->buildType() ) ) );
     1631                }
     1632        | comma_expression ';' downupdowneq enum_type           // CFA, enum type, reverse direction
    16071633                {
    16081634                        if ( $3 == OperKinds::LEThan || $3 == OperKinds::GEThan ) {
     
    16111637                        SemanticError( yylloc, "Type iterator is currently unimplemented." ); $$ = nullptr;
    16121638                }
    1613         ;
    1614 
    1615 enum_key:
    1616         TYPEDEFname
    1617         | ENUM identifier_or_type_name
    16181639        ;
    16191640
Note: See TracChangeset for help on using the changeset viewer.