@@ -8,24 +8,15 @@ using namespace libscratchcpp;
88ListBlocks::ListBlocks ()
99{
1010 // Blocks
11- addCompileFunction (" data_addtolist" , &ListBlocks::compileAddToList);
12- addCompileFunction (" data_deleteoflist" , &ListBlocks::compileDeleteFromList);
13- addCompileFunction (" data_deletealloflist" , &ListBlocks::compileDeleteAllOfList);
14- addCompileFunction (" data_insertatlist" , &ListBlocks::compileInsertToList);
15- addCompileFunction (" data_replaceitemoflist" , &ListBlocks::compileReplaceItemOfList);
16- addCompileFunction (" data_itemoflist" , &ListBlocks::compileItemOfList);
17- addCompileFunction (" data_itemnumoflist" , &ListBlocks::compileItemNumberInList);
18- addCompileFunction (" data_lengthoflist" , &ListBlocks::compileLengthOfList);
19- addCompileFunction (" data_listcontainsitem" , &ListBlocks::compileListContainsItem);
20- addBlock (" data_addtolist" , &ListBlocks::addToList);
21- addBlock (" data_deleteoflist" , &ListBlocks::deleteFromList);
22- addBlock (" data_deletealloflist" , &ListBlocks::deleteAllOfList);
23- addBlock (" data_insertatlist" , &ListBlocks::insertToList);
24- addBlock (" data_replaceitemoflist" , &ListBlocks::replaceItemOfList);
25- addBlock (" data_itemoflist" , &ListBlocks::itemOfList);
26- addBlock (" data_itemnumoflist" , &ListBlocks::itemNumberInList);
27- addBlock (" data_lengthoflist" , &ListBlocks::lengthOfList);
28- addBlock (" data_listcontainsitem" , &ListBlocks::listContainsItem);
11+ addCompileFunction (" data_addtolist" , &compileAddToList);
12+ addCompileFunction (" data_deleteoflist" , &compileDeleteFromList);
13+ addCompileFunction (" data_deletealloflist" , &compileDeleteAllOfList);
14+ addCompileFunction (" data_insertatlist" , &compileInsertToList);
15+ addCompileFunction (" data_replaceitemoflist" , &compileReplaceItemOfList);
16+ addCompileFunction (" data_itemoflist" , &compileItemOfList);
17+ addCompileFunction (" data_itemnumoflist" , &compileItemNumberInList);
18+ addCompileFunction (" data_lengthoflist" , &compileLengthOfList);
19+ addCompileFunction (" data_listcontainsitem" , &compileListContainsItem);
2920
3021 // Inputs
3122 addInput (" ITEM" , ITEM);
@@ -98,133 +89,3 @@ void ListBlocks::compileListContainsItem(Compiler *compiler)
9889 compiler->addInput (ITEM);
9990 compiler->addInstruction (vm::OP_LIST_LENGTH, { compiler->listIndex (compiler->field (LIST)->valuePtr ()) });
10091}
101-
102- Value ListBlocks::addToList (const BlockArgs &args)
103- {
104- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
105- list->push_back (args.input (ITEM)->value ());
106- return Value ();
107- }
108-
109- Value ListBlocks::deleteFromList (const BlockArgs &args)
110- {
111- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
112- Value value = args.input (INDEX)->value ();
113- size_t index;
114- if (value.isString ()) {
115- std::string str = value.toString ();
116- if (str == " last" )
117- index = list->size ();
118- else if (str == " all" ) {
119- list->clear ();
120- return Value ();
121- } else if (str == " random" ) {
122- index = list->size () == 0 ? 0 : randint<size_t >(1 , list->size ());
123- } else
124- return Value ();
125- } else
126- index = validateIndex (value.toInt (), list->size ());
127- if (index == 0 )
128- return Value ();
129- list->removeAt (index - 1 );
130- return Value ();
131- }
132-
133- Value ListBlocks::deleteAllOfList (const BlockArgs &args)
134- {
135- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
136- list->clear ();
137- return Value ();
138- }
139-
140- Value ListBlocks::insertToList (const BlockArgs &args)
141- {
142- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
143- Value value = args.input (INDEX)->value ();
144- int index;
145- if (value.isString ()) {
146- std::string str = value.toString ();
147- if (str == " last" ) {
148- list->push_back (args.input (ITEM)->value ());
149- return Value ();
150- } else if (str == " random" ) {
151- index = list->size () == 0 ? 1 : randint<size_t >(1 , list->size ());
152- } else
153- return Value ();
154- } else {
155- index = validateIndex (value.toInt (), list->size ());
156- if (index == 0 )
157- return Value ();
158- }
159- list->insert (index - 1 , args.input (ITEM)->value ());
160- return Value ();
161- }
162-
163- Value ListBlocks::replaceItemOfList (const BlockArgs &args)
164- {
165- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
166- Value value = args.input (INDEX)->value ();
167- int index;
168- if (value.isString ()) {
169- std::string str = value.toString ();
170- if (str == " last" )
171- index = list->size ();
172- else if (str == " random" ) {
173- index = list->size () == 0 ? 0 : randint<size_t >(1 , list->size ());
174- } else
175- return Value ();
176- } else
177- index = validateIndex (value.toInt (), list->size ());
178- if (index == 0 )
179- return Value ();
180- list->replace (index - 1 , args.input (ITEM)->value ());
181- return Value ();
182- }
183-
184- Value ListBlocks::itemOfList (const BlockArgs &args)
185- {
186- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
187- Value value = args.input (INDEX)->value ();
188- int index;
189- if (value.isString ()) {
190- std::string str = value.toString ();
191- if (str == " last" )
192- index = list->size ();
193- else if (str == " random" ) {
194- index = list->size () == 0 ? 0 : randint<size_t >(1 , list->size ());
195- } else
196- return " " ;
197- } else
198- index = validateIndex (value.toInt (), list->size ());
199- if (index == 0 )
200- return " " ;
201- return list->at (index - 1 );
202- }
203-
204- Value ListBlocks::itemNumberInList (const BlockArgs &args)
205- {
206- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
207- return list->indexOf (args.input (ITEM)->value ()) + 1 ;
208- }
209-
210- Value ListBlocks::lengthOfList (const BlockArgs &args)
211- {
212- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
213- return list->size ();
214- }
215-
216- Value ListBlocks::listContainsItem (const BlockArgs &args)
217- {
218- auto list = std::static_pointer_cast<List>(args.field (LIST)->valuePtr ());
219- return list->contains (args.input (ITEM)->value ());
220- }
221-
222- int ListBlocks::validateIndex (size_t index, size_t listLength)
223- {
224- if (listLength == 0 ) {
225- if (index != 1 )
226- return 0 ;
227- } else if ((index < 1 ) || (index > listLength))
228- return 0 ;
229- return index;
230- }
0 commit comments