Skip to content

Commit 8b5f06a

Browse files
committed
Compiler: Add createRandomInt() method
1 parent 408404f commit 8b5f06a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
6666
CompilerValue *createDiv(CompilerValue *operand1, CompilerValue *operand2);
6767

6868
CompilerValue *createRandom(CompilerValue *from, CompilerValue *to);
69+
CompilerValue *createRandomInt(CompilerValue *from, CompilerValue *to);
6970

7071
CompilerValue *createCmpEQ(CompilerValue *operand1, CompilerValue *operand2);
7172
CompilerValue *createCmpGT(CompilerValue *operand1, CompilerValue *operand2);

src/dev/engine/compiler.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ CompilerValue *Compiler::createRandom(CompilerValue *from, CompilerValue *to)
199199
return impl->builder->createRandom(from, to);
200200
}
201201

202+
/*!
203+
* Creates a random integer instruction.
204+
* \note Infinity or NaN results in undefined behavior.
205+
*/
206+
CompilerValue *Compiler::createRandomInt(CompilerValue *from, CompilerValue *to)
207+
{
208+
return impl->builder->createRandomInt(from, to);
209+
}
210+
202211
/*! Creates an equality comparison instruction. */
203212
CompilerValue *Compiler::createCmpEQ(CompilerValue *operand1, CompilerValue *operand2)
204213
{

test/dev/compiler/compiler_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,24 @@ TEST_F(CompilerTest, CreateRandom)
484484
compile(compiler, block);
485485
}
486486

487+
TEST_F(CompilerTest, CreateRandomInt)
488+
{
489+
Compiler compiler(&m_engine, &m_target);
490+
auto block = std::make_shared<Block>("", "");
491+
492+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
493+
CompilerValue arg1(Compiler::StaticType::Unknown);
494+
CompilerValue arg2(Compiler::StaticType::Unknown);
495+
CompilerValue ret(Compiler::StaticType::Unknown);
496+
EXPECT_CALL(*m_builder, createRandomInt(&arg1, &arg2)).WillOnce(Return(&ret));
497+
EXPECT_EQ(compiler->createRandomInt(&arg1, &arg2), &ret);
498+
499+
return nullptr;
500+
});
501+
502+
compile(compiler, block);
503+
}
504+
487505
TEST_F(CompilerTest, CreateCmpEQ)
488506
{
489507
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)