Skip to content

Commit e6edce7

Browse files
committed
Compiler: Add methods for comparison operators
1 parent 379a12e commit e6edce7

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class LIBSCRATCHCPP_EXPORT Compiler
5353
void createMul();
5454
void createDiv();
5555

56+
void createCmpEQ();
57+
void createCmpGT();
58+
void createCmpLT();
59+
5660
void moveToIf(std::shared_ptr<Block> substack);
5761
void moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
5862
void moveToRepeatLoop(std::shared_ptr<Block> substack);

src/dev/engine/compiler.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ void Compiler::createDiv()
126126
impl->builder->createDiv();
127127
}
128128

129+
/*! Creates an equality comparison instruction using the last 2 values. */
130+
void Compiler::createCmpEQ()
131+
{
132+
impl->builder->createCmpEQ();
133+
}
134+
135+
/*! Creates a greater than comparison instruction using the last 2 values. */
136+
void Compiler::createCmpGT()
137+
{
138+
impl->builder->createCmpGT();
139+
}
140+
141+
/*! Creates a lower than comparison instruction using the last 2 values. */
142+
void Compiler::createCmpLT()
143+
{
144+
impl->builder->createCmpLT();
145+
}
146+
129147
/*! Jumps to the given if substack. */
130148
void Compiler::moveToIf(std::shared_ptr<Block> substack)
131149
{

test/dev/compiler/compiler_test.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,45 @@ TEST_F(CompilerTest, CreateDiv)
271271
compile(compiler, block);
272272
}
273273

274+
TEST_F(CompilerTest, CreateCmpEQ)
275+
{
276+
Compiler compiler(&m_engine, &m_target);
277+
auto block = std::make_shared<Block>("", "");
278+
279+
block->setCompileFunction([](Compiler *compiler) {
280+
EXPECT_CALL(*m_builder, createCmpEQ);
281+
compiler->createCmpEQ();
282+
});
283+
284+
compile(compiler, block);
285+
}
286+
287+
TEST_F(CompilerTest, CreateCmpGT)
288+
{
289+
Compiler compiler(&m_engine, &m_target);
290+
auto block = std::make_shared<Block>("", "");
291+
292+
block->setCompileFunction([](Compiler *compiler) {
293+
EXPECT_CALL(*m_builder, createCmpGT);
294+
compiler->createCmpGT();
295+
});
296+
297+
compile(compiler, block);
298+
}
299+
300+
TEST_F(CompilerTest, CreateCmpLT)
301+
{
302+
Compiler compiler(&m_engine, &m_target);
303+
auto block = std::make_shared<Block>("", "");
304+
305+
block->setCompileFunction([](Compiler *compiler) {
306+
EXPECT_CALL(*m_builder, createCmpLT);
307+
compiler->createCmpLT();
308+
});
309+
310+
compile(compiler, block);
311+
}
312+
274313
TEST_F(CompilerTest, MoveToIf)
275314
{
276315
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)