@@ -401,6 +401,84 @@ TEST_F(CompilerTest, MoveToIfElse)
401401 compile (compiler, if1);
402402}
403403
404+ TEST_F (CompilerTest, MoveToRepeatLoop)
405+ {
406+ Compiler compiler (&m_engine, &m_target);
407+
408+ auto l1 = std::make_shared<Block>(" " , " loop" );
409+ l1->setCompileFunction ([](Compiler *compiler) {
410+ EXPECT_CALL (*m_builder, beginRepeatLoop ());
411+ EXPECT_CALL (*m_builder, endLoop ());
412+ compiler->moveToRepeatLoop (nullptr );
413+ });
414+
415+ auto l2 = std::make_shared<Block>(" " , " loop" );
416+ l1->setNext (l2);
417+ l2->setParent (l1);
418+ l2->setCompileFunction ([](Compiler *compiler) {
419+ EXPECT_CALL (*m_builder, beginRepeatLoop ());
420+ EXPECT_CALL (*m_builder, addConstValue (Value (2 )));
421+ EXPECT_CALL (*m_builder, endLoop ());
422+ compiler->moveToRepeatLoop (compiler->input (" SUBSTACK" )->valueBlock ());
423+ });
424+
425+ auto substack = std::make_shared<Block>(" " , " substack" );
426+ substack->setParent (l2);
427+ substack->setCompileFunction ([](Compiler *compiler) { compiler->addConstValue (2 ); });
428+
429+ auto input = std::make_shared<Input>(" SUBSTACK" , Input::Type::NoShadow);
430+ input->setValueBlock (substack);
431+ l2->addInput (input);
432+
433+ // Nested
434+ auto l3 = std::make_shared<Block>(" " , " loop" );
435+ l2->setNext (l3);
436+ l3->setParent (l2);
437+ l3->setCompileFunction ([](Compiler *compiler) {
438+ EXPECT_CALL (*m_builder, beginRepeatLoop ()).Times (2 );
439+ EXPECT_CALL (*m_builder, endLoop ()).Times (2 );
440+ EXPECT_CALL (*m_builder, addConstValue (Value (1 )));
441+ compiler->moveToRepeatLoop (compiler->input (" SUBSTACK" )->valueBlock ());
442+ });
443+
444+ // Begin loop
445+ auto loopSubstack = std::make_shared<Block>(" " , " loop" );
446+ loopSubstack->setParent (l3);
447+ loopSubstack->setCompileFunction ([](Compiler *compiler) { compiler->moveToRepeatLoop (compiler->input (" SUBSTACK" )->valueBlock ()); });
448+
449+ substack = std::make_shared<Block>(" " , " substack" );
450+ substack->setParent (loopSubstack);
451+ substack->setCompileFunction ([](Compiler *compiler) { compiler->addConstValue (1 ); });
452+
453+ input = std::make_shared<Input>(" SUBSTACK" , Input::Type::NoShadow);
454+ input->setValueBlock (substack);
455+ loopSubstack->addInput (input);
456+
457+ // End loop
458+ input = std::make_shared<Input>(" SUBSTACK" , Input::Type::NoShadow);
459+ input->setValueBlock (loopSubstack);
460+ l3->addInput (input);
461+
462+ // Empty loop body
463+ auto l4 = std::make_shared<Block>(" " , " loop" );
464+ l3->setNext (l4);
465+ l4->setParent (l3);
466+ l4->setCompileFunction ([](Compiler *compiler) {
467+ EXPECT_CALL (*m_builder, beginRepeatLoop ());
468+ EXPECT_CALL (*m_builder, endLoop ());
469+ compiler->moveToRepeatLoop (nullptr );
470+ });
471+
472+ // Code after the loop
473+ auto block = std::make_shared<Block>(" " , " " );
474+ block->setParent (l4);
475+ l4->setNext (block);
476+ block->setCompileFunction ([](Compiler *compiler) { compiler->addConstValue (" after" ); });
477+
478+ EXPECT_CALL (*m_builder, addConstValue (Value (" after" )));
479+ compile (compiler, l1);
480+ }
481+
404482TEST_F (CompilerTest, Input)
405483{
406484 Compiler compiler (&m_engine, &m_target);
0 commit comments