Skip to content

Commit d071783

Browse files
committed
Compiler: Add list methods
1 parent b0d2666 commit d071783

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class LIBSCRATCHCPP_EXPORT Compiler
4646
void addConstValue(const Value &value);
4747
void addVariableValue(Variable *variable);
4848
void addListContents(List *list);
49+
void addListItem(List *list);
50+
void addListItemIndex(List *list);
51+
void addListContains(List *list);
52+
void addListSize(List *list);
4953
void addInput(const std::string &name);
5054

5155
void createAdd();
@@ -80,6 +84,12 @@ class LIBSCRATCHCPP_EXPORT Compiler
8084

8185
void createVariableWrite(Variable *variable);
8286

87+
void createListClear(List *list);
88+
void createListRemove(List *list);
89+
void createListAppend(List *list);
90+
void createListInsert(List *list);
91+
void createListReplace(List *list);
92+
8393
void beginIfStatement();
8494
void beginElseBranch();
8595
void endIf();

src/dev/engine/compiler.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ void Compiler::addListContents(List *list)
100100
impl->builder->addListContents(list);
101101
}
102102

103+
/*! Adds the item with index from the last value of the given list to the code. */
104+
void Compiler::addListItem(List *list)
105+
{
106+
impl->builder->addListItem(list);
107+
}
108+
109+
/*! Adds the index of the item from the last value of the given list to the code. */
110+
void Compiler::addListItemIndex(List *list)
111+
{
112+
impl->builder->addListItemIndex(list);
113+
}
114+
115+
/*! Adds the result of a list contains item from the check to the code. */
116+
void Compiler::addListContains(List *list)
117+
{
118+
impl->builder->addListContains(list);
119+
}
120+
121+
/*! Adds the length of the given list to the code. */
122+
void Compiler::addListSize(List *list)
123+
{
124+
impl->builder->addListSize(list);
125+
}
126+
103127
/*! Compiles the given input (resolved by name) and adds it to the compiled code. */
104128
void Compiler::addInput(const std::string &name)
105129
{
@@ -268,6 +292,39 @@ void Compiler::createVariableWrite(Variable *variable)
268292
impl->builder->createVariableWrite(variable);
269293
}
270294

295+
/*! Creates a clear list operation. */
296+
void Compiler::createListClear(List *list)
297+
{
298+
impl->builder->createListClear(list);
299+
}
300+
301+
/*!
302+
* Creates a remove item from list operation.
303+
* \note The index starts with 0 and is cast to number, special strings like "last" are not handled.
304+
*/
305+
void Compiler::createListRemove(List *list)
306+
{
307+
impl->builder->createListRemove(list);
308+
}
309+
310+
/*! Creates a list append operation using the last value. */
311+
void Compiler::createListAppend(List *list)
312+
{
313+
impl->builder->createListAppend(list);
314+
}
315+
316+
/*! Creates a list insert operation using the last 2 values (index, value). */
317+
void Compiler::createListInsert(List *list)
318+
{
319+
impl->builder->createListInsert(list);
320+
}
321+
322+
/*! Creates a list replace operation using the last 2 values (index, value). */
323+
void Compiler::createListReplace(List *list)
324+
{
325+
impl->builder->createListReplace(list);
326+
}
327+
271328
/*!
272329
* Starts a custom if statement.
273330
* \note The if statement must be terminated using endIf() after compiling your block.

test/dev/compiler/compiler_test.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,70 @@ TEST_F(CompilerTest, AddListContents)
132132
compile(compiler, block);
133133
}
134134

135+
TEST_F(CompilerTest, AddListItem)
136+
{
137+
Compiler compiler(&m_engine, &m_target);
138+
auto block = std::make_shared<Block>("a", "");
139+
block->setCompileFunction([](Compiler *compiler) {
140+
List list1("", ""), list2("", "");
141+
EXPECT_CALL(*m_builder, addListItem(&list1));
142+
compiler->addListItem(&list1);
143+
144+
EXPECT_CALL(*m_builder, addListItem(&list2));
145+
compiler->addListItem(&list2);
146+
});
147+
148+
compile(compiler, block);
149+
}
150+
151+
TEST_F(CompilerTest, AddListItemIndex)
152+
{
153+
Compiler compiler(&m_engine, &m_target);
154+
auto block = std::make_shared<Block>("a", "");
155+
block->setCompileFunction([](Compiler *compiler) {
156+
List list1("", ""), list2("", "");
157+
EXPECT_CALL(*m_builder, addListItemIndex(&list1));
158+
compiler->addListItemIndex(&list1);
159+
160+
EXPECT_CALL(*m_builder, addListItemIndex(&list2));
161+
compiler->addListItemIndex(&list2);
162+
});
163+
164+
compile(compiler, block);
165+
}
166+
167+
TEST_F(CompilerTest, AddListSize)
168+
{
169+
Compiler compiler(&m_engine, &m_target);
170+
auto block = std::make_shared<Block>("a", "");
171+
block->setCompileFunction([](Compiler *compiler) {
172+
List list1("", ""), list2("", "");
173+
EXPECT_CALL(*m_builder, addListSize(&list1));
174+
compiler->addListSize(&list1);
175+
176+
EXPECT_CALL(*m_builder, addListSize(&list2));
177+
compiler->addListSize(&list2);
178+
});
179+
180+
compile(compiler, block);
181+
}
182+
183+
TEST_F(CompilerTest, AddListContains)
184+
{
185+
Compiler compiler(&m_engine, &m_target);
186+
auto block = std::make_shared<Block>("a", "");
187+
block->setCompileFunction([](Compiler *compiler) {
188+
List list1("", ""), list2("", "");
189+
EXPECT_CALL(*m_builder, addListContains(&list1));
190+
compiler->addListContains(&list1);
191+
192+
EXPECT_CALL(*m_builder, addListContains(&list2));
193+
compiler->addListContains(&list2);
194+
});
195+
196+
compile(compiler, block);
197+
}
198+
135199
TEST_F(CompilerTest, AddInput)
136200
{
137201
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)