Skip to content

Commit 9e7d3d0

Browse files
committed
Compiler: Add methods for add, sub, mul and div operators
1 parent 9183e2b commit 9e7d3d0

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class LIBSCRATCHCPP_EXPORT Compiler
4747
void addListContents(List *list);
4848
void addInput(const std::string &name);
4949

50+
void createAdd();
51+
void createSub();
52+
void createMul();
53+
void createDiv();
54+
5055
void moveToIf(std::shared_ptr<Block> substack);
5156
void moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
5257
void moveToRepeatLoop(std::shared_ptr<Block> substack);

src/dev/engine/compiler.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ void Compiler::addInput(const std::string &name)
102102
addInput(impl->block->inputAt(impl->block->findInput(name)).get());
103103
}
104104

105+
/*! Creates an add instruction from the last 2 values. */
106+
void Compiler::createAdd()
107+
{
108+
impl->builder->createAdd();
109+
}
110+
111+
/*! Creates a subtract instruction from the last 2 values. */
112+
void Compiler::createSub()
113+
{
114+
impl->builder->createSub();
115+
}
116+
117+
/*! Creates a multiply instruction from the last 2 values. */
118+
void Compiler::createMul()
119+
{
120+
impl->builder->createMul();
121+
}
122+
123+
/*! Creates a divide instruction from the last 2 values. */
124+
void Compiler::createDiv()
125+
{
126+
impl->builder->createDiv();
127+
}
128+
105129
/*! Jumps to the given if substack. */
106130
void Compiler::moveToIf(std::shared_ptr<Block> substack)
107131
{

test/dev/compiler/compiler_test.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,58 @@ TEST_F(CompilerTest, AddInput)
219219
compile(compiler, block);
220220
}
221221

222+
TEST_F(CompilerTest, CreateAdd)
223+
{
224+
Compiler compiler(&m_engine, &m_target);
225+
auto block = std::make_shared<Block>("", "");
226+
227+
block->setCompileFunction([](Compiler *compiler) {
228+
EXPECT_CALL(*m_builder, createAdd);
229+
compiler->createAdd();
230+
});
231+
232+
compile(compiler, block);
233+
}
234+
235+
TEST_F(CompilerTest, CreateSub)
236+
{
237+
Compiler compiler(&m_engine, &m_target);
238+
auto block = std::make_shared<Block>("", "");
239+
240+
block->setCompileFunction([](Compiler *compiler) {
241+
EXPECT_CALL(*m_builder, createSub);
242+
compiler->createSub();
243+
});
244+
245+
compile(compiler, block);
246+
}
247+
248+
TEST_F(CompilerTest, CreateMul)
249+
{
250+
Compiler compiler(&m_engine, &m_target);
251+
auto block = std::make_shared<Block>("", "");
252+
253+
block->setCompileFunction([](Compiler *compiler) {
254+
EXPECT_CALL(*m_builder, createMul);
255+
compiler->createMul();
256+
});
257+
258+
compile(compiler, block);
259+
}
260+
261+
TEST_F(CompilerTest, CreateDiv)
262+
{
263+
Compiler compiler(&m_engine, &m_target);
264+
auto block = std::make_shared<Block>("", "");
265+
266+
block->setCompileFunction([](Compiler *compiler) {
267+
EXPECT_CALL(*m_builder, createDiv);
268+
compiler->createDiv();
269+
});
270+
271+
compile(compiler, block);
272+
}
273+
222274
TEST_F(CompilerTest, MoveToIf)
223275
{
224276
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)