Skip to content

Commit d9e0e11

Browse files
committed
Compiler: Add methods for 'and', 'or', 'not' gates
1 parent 1d1867f commit d9e0e11

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

include/scratchcpp/dev/compiler.h

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class LIBSCRATCHCPP_EXPORT Compiler
5757
void createCmpGT();
5858
void createCmpLT();
5959

60+
void createAnd();
61+
void createOr();
62+
void createNot();
63+
6064
void moveToIf(std::shared_ptr<Block> substack);
6165
void moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
6266
void moveToRepeatLoop(std::shared_ptr<Block> substack);

src/dev/engine/compiler.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ void Compiler::createCmpLT()
142142
impl->builder->createCmpLT();
143143
}
144144

145+
/*! Creates an AND operation using the last 2 values. */
146+
void Compiler::createAnd()
147+
{
148+
impl->builder->createAnd();
149+
}
150+
151+
/*! Creates an OR operation using the last 2 values. */
152+
void Compiler::createOr()
153+
{
154+
impl->builder->createOr();
155+
}
156+
157+
/*! Creates a NOT operation using the last value. */
158+
void Compiler::createNot()
159+
{
160+
impl->builder->createNot();
161+
}
162+
145163
/*! Jumps to the given if substack. */
146164
void Compiler::moveToIf(std::shared_ptr<Block> substack)
147165
{

test/dev/compiler/compiler_test.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,45 @@ TEST_F(CompilerTest, CreateCmpLT)
310310
compile(compiler, block);
311311
}
312312

313+
TEST_F(CompilerTest, CreateAnd)
314+
{
315+
Compiler compiler(&m_engine, &m_target);
316+
auto block = std::make_shared<Block>("", "");
317+
318+
block->setCompileFunction([](Compiler *compiler) {
319+
EXPECT_CALL(*m_builder, createAnd);
320+
compiler->createAnd();
321+
});
322+
323+
compile(compiler, block);
324+
}
325+
326+
TEST_F(CompilerTest, CreateOr)
327+
{
328+
Compiler compiler(&m_engine, &m_target);
329+
auto block = std::make_shared<Block>("", "");
330+
331+
block->setCompileFunction([](Compiler *compiler) {
332+
EXPECT_CALL(*m_builder, createOr);
333+
compiler->createOr();
334+
});
335+
336+
compile(compiler, block);
337+
}
338+
339+
TEST_F(CompilerTest, CreateNot)
340+
{
341+
Compiler compiler(&m_engine, &m_target);
342+
auto block = std::make_shared<Block>("", "");
343+
344+
block->setCompileFunction([](Compiler *compiler) {
345+
EXPECT_CALL(*m_builder, createNot);
346+
compiler->createNot();
347+
});
348+
349+
compile(compiler, block);
350+
}
351+
313352
TEST_F(CompilerTest, MoveToIf)
314353
{
315354
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)