Changeset 401e61f
- Timestamp:
- Jun 5, 2018, 10:16:46 AM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
- Children:
- 2248dc7, dafdbe7
- Parents:
- 6e3eaa57
- Location:
- src
- Files:
-
- 3 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ParseNode.h
r6e3eaa57 r401e61f 10 10 // Created On : Sat May 16 13:28:16 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Apr 30 09:19:17201813 // Update Count : 83 112 // Last Modified On : Mon Jun 4 22:21:04 2018 13 // Update Count : 832 14 14 // 15 15 … … 408 408 Statement * build_case( ExpressionNode * ctl ); 409 409 Statement * build_default(); 410 Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind = false ); 410 Statement * build_while( IfCtl * ctl, StatementNode * stmt ); 411 Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt ); 411 412 Statement * build_for( ForCtl * forctl, StatementNode * stmt ); 412 413 Statement * build_branch( BranchStmt::Type kind ); -
src/Parser/StatementNode.cc
r6e3eaa57 r401e61f 10 10 // Created On : Sat May 16 14:59:41 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Apr 30 09:21:16201813 // Update Count : 3 5412 // Last Modified On : Tue Jun 5 08:58:34 2018 13 // Update Count : 362 14 14 // 15 15 … … 69 69 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts ); 70 70 return this; 71 } 71 } // StatementNode::append_last_case 72 72 73 73 Statement * build_expr( ExpressionNode * ctl ) { 74 74 Expression * e = maybeMoveBuild< Expression >( ctl ); 75 75 76 if ( e ) 77 return new ExprStmt( e ); 78 else 79 return new NullStmt(); 80 } 76 if ( e ) return new ExprStmt( e ); 77 else return new NullStmt(); 78 } // build_expr 81 79 82 80 Expression * build_if_control( IfCtl * ctl, std::list< Statement * > & init ) { … … 100 98 delete ctl; 101 99 return cond; 102 } 100 } // build_if_control 103 101 104 102 Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) { 105 Statement * thenb, * elseb = 0;103 Statement * thenb, * elseb = nullptr; 106 104 std::list< Statement * > branches; 107 105 buildMoveList< Statement, StatementNode >( then_stmt, branches ); … … 119 117 Expression * cond = build_if_control( ctl, init ); 120 118 return new IfStmt( cond, thenb, elseb, init ); 121 } 119 } // build_if 122 120 123 121 Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) { … … 135 133 // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements 136 134 return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches ); 137 } 135 } // build_switch 136 138 137 Statement * build_case( ExpressionNode * ctl ) { 139 138 std::list< Statement * > branches; 140 139 return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches ); 141 } 140 } // build_case 141 142 142 Statement * build_default() { 143 143 std::list< Statement * > branches; 144 144 return new CaseStmt( nullptr, branches, true ); 145 } 146 147 Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind) {145 } // build_default 146 147 Statement * build_while( IfCtl * ctl, StatementNode * stmt ) { 148 148 std::list< Statement * > branches; 149 149 buildMoveList< Statement, StatementNode >( stmt, branches ); … … 151 151 152 152 std::list< Statement * > init; 153 return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), init, kind ); 154 } 153 Expression * cond = build_if_control( ctl, init ); 154 return new WhileStmt( cond, branches.front(), init, false ); 155 } // build_while 156 157 Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt ) { 158 std::list< Statement * > branches; 159 buildMoveList< Statement, StatementNode >( stmt, branches ); 160 assert( branches.size() == 1 ); 161 162 std::list< Statement * > init; 163 return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), init, true ); 164 } // build_do_while 155 165 156 166 Statement * build_for( ForCtl * forctl, StatementNode * stmt ) { … … 174 184 delete forctl; 175 185 return new ForStmt( init, cond, incr, branches.front() ); 176 } 186 } // build_for 177 187 178 188 Statement * build_branch( BranchStmt::Type kind ) { 179 189 Statement * ret = new BranchStmt( "", kind ); 180 190 return ret; 181 } 191 } // build_branch 192 182 193 Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) { 183 194 Statement * ret = new BranchStmt( * identifier, kind ); 184 195 delete identifier; // allocated by lexer 185 196 return ret; 186 } 197 } // build_branch 198 187 199 Statement * build_computedgoto( ExpressionNode * ctl ) { 188 200 return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto ); 189 } 201 } // build_computedgoto 190 202 191 203 Statement * build_return( ExpressionNode * ctl ) { … … 193 205 buildMoveList( ctl, exps ); 194 206 return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr ); 195 } 207 } // build_return 196 208 197 209 Statement * build_throw( ExpressionNode * ctl ) { … … 200 212 assertf( exps.size() < 2, "This means we are leaking memory"); 201 213 return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr ); 202 } 214 } // build_throw 203 215 204 216 Statement * build_resume( ExpressionNode * ctl ) { … … 207 219 assertf( exps.size() < 2, "This means we are leaking memory"); 208 220 return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr ); 209 } 221 } // build_resume 210 222 211 223 Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) { … … 213 225 (void)target; 214 226 assertf( false, "resume at (non-local throw) is not yet supported," ); 215 } 227 } // build_resume_at 216 228 217 229 Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) { … … 221 233 FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) ); 222 234 return new TryStmt( tryBlock, branches, finallyBlock ); 223 } 235 } // build_try 236 224 237 Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) { 225 238 std::list< Statement * > branches; … … 227 240 assert( branches.size() == 1 ); 228 241 return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() ); 229 } 242 } // build_catch 243 230 244 Statement * build_finally( StatementNode * stmt ) { 231 245 std::list< Statement * > branches; … … 233 247 assert( branches.size() == 1 ); 234 248 return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) ); 235 } 249 } // build_finally 236 250 237 251 WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) { … … 254 268 255 269 return node; 256 } 270 } // build_waitfor 257 271 258 272 WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) { … … 273 287 274 288 return node; 275 } 289 } // build_waitfor 276 290 277 291 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) { … … 282 296 node->timeout.statement = maybeMoveBuild<Statement >( stmt ); 283 297 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); 284 } 285 else { 298 } else { 286 299 node->orelse.statement = maybeMoveBuild<Statement >( stmt ); 287 300 node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); 288 } 301 } // if 289 302 290 303 return node; 291 } 304 } // build_waitfor_timeout 292 305 293 306 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) { … … 302 315 303 316 return node; 304 } 317 } // build_waitfor_timeout 305 318 306 319 WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) { … … 309 322 Statement * s = maybeMoveBuild<Statement>( stmt ); 310 323 return new WithStmt( e, s ); 311 } 324 } // build_with 312 325 313 326 Statement * build_compound( StatementNode * first ) { … … 315 328 buildMoveList( first, cs->get_kids() ); 316 329 return cs; 317 } 330 } // build_compound 318 331 319 332 Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) { … … 325 338 buildMoveList( clobber, clob ); 326 339 return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels ); 327 } 340 } // build_asm 328 341 329 342 Statement * build_directive( string * directive ) { 330 343 return new DirectiveStmt( *directive ); 331 } 344 } // build_directive 332 345 333 346 // Local Variables: // -
src/Parser/parser.yy
r6e3eaa57 r401e61f 10 10 // Created On : Sat Sep 1 20:22:55 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jun 4 2 1:24:46201813 // Update Count : 34 8812 // Last Modified On : Mon Jun 4 22:22:04 2018 13 // Update Count : 3492 14 14 // 15 15 … … 1050 1050 1051 1051 iteration_statement: 1052 WHILE '(' comma_expression ')' statement1053 { $$ = new StatementNode( build_while( $ 3, $5) ); }1052 WHILE '(' push if_control_expression ')' statement pop 1053 { $$ = new StatementNode( build_while( $4, $6 ) ); } 1054 1054 | DO statement WHILE '(' comma_expression ')' ';' 1055 { $$ = new StatementNode( build_ while( $5, $2, true) ); }1055 { $$ = new StatementNode( build_do_while( $5, $2 ) ); } 1056 1056 | FOR '(' push for_control_expression ')' statement pop 1057 1057 { $$ = new StatementNode( build_for( $4, $6 ) ); } -
src/tests/.expect/ifwhileCtl.txt
r6e3eaa57 r401e61f 3 3 x == y correct 4 4 s.i < 4 correct 5 x != 0 correct 6 x == y correct 7 s.i < 4 correct -
src/tests/ifwhileCtl.c
r6e3eaa57 r401e61f 10 10 // Created On : Sat Aug 26 10:13:11 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Jun 2 09:55:02201813 // Update Count : 1 512 // Last Modified On : Mon Jun 4 22:46:48 2018 13 // Update Count : 19 14 14 // 15 15 … … 42 42 43 43 if ( struct S { int i; } s = { 3 }; s.i < 4 ) { 44 S s1; 44 45 sout | "s.i < 4 correct" | endl; 45 46 } else { 47 S s1; 46 48 sout | "s.i >= 4 incorrect" | endl; 47 49 } // if 50 51 while ( int x = 1 ) { 52 sout | "x != 0 correct" | endl; 53 break; 54 } // while 55 56 while ( int x = 4, y = 0 ) { 57 sout | "x != 0 && y != 0 incorrect" | endl; 58 } // while 59 60 while ( int x = 5, y = f( x ); x == y ) { 61 sout | "x == y correct" | endl; 62 break; 63 } // while 64 65 while ( struct S { int i; } s = { 3 }; s.i < 4 ) { 66 S s1; 67 sout | "s.i < 4 correct" | endl; 68 break; 69 } // while 48 70 } // main 49 71
Note: See TracChangeset
for help on using the changeset viewer.