- Timestamp:
- Mar 10, 2023, 4:05:50 PM (23 months ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 85a95cc
- Parents:
- fed03b3
- Location:
- src/Parser
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ParseNode.h
rfed03b3 r1cdc052 394 394 395 395 struct ForCtrl { 396 ForCtrl( ExpressionNode * expr, ExpressionNode * condition, ExpressionNode * change ) : 397 init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {} 398 ForCtrl( DeclarationNode * decl, ExpressionNode * condition, ExpressionNode * change ) : 399 init( new StatementNode( decl ) ), condition( condition ), change( change ) {} 396 ForCtrl( StatementNode * stmt, ExpressionNode * condition, ExpressionNode * change ) : 397 init( stmt ), condition( condition ), change( change ) {} 400 398 401 399 StatementNode * init; -
src/Parser/parser.yy
rfed03b3 r1cdc052 206 206 #define MISSING_HIGH "Missing high value for down-to range so index is uninitialized." 207 207 208 static ForCtrl * makeForCtrl( 209 DeclarationNode * init, 210 enum OperKinds compop, 211 ExpressionNode * comp, 212 ExpressionNode * inc ) { 213 // Wrap both comp/inc if they are non-null. 214 if ( comp ) comp = new ExpressionNode( build_binary_val( 215 compop, 216 new ExpressionNode( build_varref( new string( *init->name ) ) ), 217 comp ) ); 218 if ( inc ) inc = new ExpressionNode( build_binary_val( 219 // choose += or -= for upto/downto 220 compop == OperKinds::LThan || compop == OperKinds::LEThan ? OperKinds::PlusAssn : OperKinds::MinusAssn, 221 new ExpressionNode( build_varref( new string( *init->name ) ) ), 222 inc ) ); 223 // The StatementNode call frees init->name, it must happen later. 224 return new ForCtrl( new StatementNode( init ), comp, inc ); 225 } 226 208 227 ForCtrl * forCtrl( DeclarationNode * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) { 209 228 if ( index->initializer ) { … … 213 232 SemanticError( yylloc, "Multiple loop indexes disallowed in for-loop declaration." ); 214 233 } // if 215 return new ForCtrl( index->addInitializer( new InitializerNode( start ) ), 216 // NULL comp/inc => leave blank 217 comp ? new ExpressionNode( build_binary_val( compop, new ExpressionNode( build_varref( new string( *index->name ) ) ), comp ) ) : nullptr, 218 inc ? new ExpressionNode( build_binary_val( compop == OperKinds::LThan || compop == OperKinds::LEThan ? // choose += or -= for upto/downto 219 OperKinds::PlusAssn : OperKinds::MinusAssn, new ExpressionNode( build_varref( new string( *index->name ) ) ), inc ) ) : nullptr ); 234 DeclarationNode * initDecl = index->addInitializer( new InitializerNode( start ) ); 235 return makeForCtrl( initDecl, compop, comp, inc ); 220 236 } // forCtrl 221 237 … … 225 241 type = new ExpressionNode( new CastExpr( maybeMoveBuild( type ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) ); 226 242 } // if 227 // type = new ExpressionNode( build_func( new ExpressionNode( build_varref( new string( "__for_control_index_constraints__" ) ) ), type ) ); 228 return new ForCtrl( 229 distAttr( DeclarationNode::newTypeof( type, true ), DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) ) ), 230 // NULL comp/inc => leave blank 231 comp ? new ExpressionNode( build_binary_val( compop, new ExpressionNode( build_varref( new string( *index ) ) ), comp ) ) : nullptr, 232 inc ? new ExpressionNode( build_binary_val( compop == OperKinds::LThan || compop == OperKinds::LEThan ? // choose += or -= for upto/downto 233 OperKinds::PlusAssn : OperKinds::MinusAssn, new ExpressionNode( build_varref( new string( *index ) ) ), inc ) ) : nullptr ); 243 DeclarationNode * initDecl = distAttr( 244 DeclarationNode::newTypeof( type, true ), 245 DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) ) 246 ); 247 return makeForCtrl( initDecl, compop, comp, inc ); 234 248 } // forCtrl 235 249 … … 1298 1312 { $$ = new StatementNode( build_do_while( $5, maybe_build_compound( $2 ), $8 ) ); } 1299 1313 | FOR '(' ')' statement %prec THEN // CFA => for ( ;; ) 1300 { $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) ); }1314 { $$ = new StatementNode( build_for( new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( $4 ) ) ); } 1301 1315 | FOR '(' ')' statement ELSE statement // CFA 1302 1316 { 1303 $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) );1317 $$ = new StatementNode( build_for( new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( $4 ) ) ); 1304 1318 SemanticWarning( yylloc, Warning::SuperfluousElse ); 1305 1319 } … … 1335 1349 for_control_expression: 1336 1350 ';' comma_expression_opt ';' comma_expression_opt 1337 { $$ = new ForCtrl( (ExpressionNode * )nullptr, $2, $4 ); }1351 { $$ = new ForCtrl( nullptr, $2, $4 ); } 1338 1352 | comma_expression ';' comma_expression_opt ';' comma_expression_opt 1339 { $$ = new ForCtrl( $1, $3, $5 ); } 1353 { 1354 StatementNode * init = $1 ? new StatementNode( new ExprStmt( maybeMoveBuild( $1 ) ) ) : nullptr; 1355 $$ = new ForCtrl( init, $3, $5 ); 1356 } 1340 1357 | declaration comma_expression_opt ';' comma_expression_opt // C99, declaration has ';' 1341 { $$ = new ForCtrl( $1, $2, $4 ); }1358 { $$ = new ForCtrl( new StatementNode( $1 ), $2, $4 ); } 1342 1359 1343 1360 | '@' ';' comma_expression // CFA, empty loop-index 1344 { $$ = new ForCtrl( (ExpressionNode *)nullptr, $3, nullptr ); }1361 { $$ = new ForCtrl( nullptr, $3, nullptr ); } 1345 1362 | '@' ';' comma_expression ';' comma_expression // CFA, empty loop-index 1346 { $$ = new ForCtrl( (ExpressionNode *)nullptr, $3, $5 ); }1363 { $$ = new ForCtrl( nullptr, $3, $5 ); } 1347 1364 1348 1365 | comma_expression // CFA, anonymous loop-index
Note: See TracChangeset
for help on using the changeset viewer.