Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit edb28bf

Browse files
committedNov 7, 2024·
Compiler: Add methods for math instructions
1 parent 60b53c4 commit edb28bf

File tree

3 files changed

+321
-0
lines changed

3 files changed

+321
-0
lines changed
 

‎include/scratchcpp/dev/compiler.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ class LIBSCRATCHCPP_EXPORT Compiler
6161
void createOr();
6262
void createNot();
6363

64+
void createMod();
65+
void createRound();
66+
void createAbs();
67+
void createFloor();
68+
void createCeil();
69+
void createSqrt();
70+
void createSin();
71+
void createCos();
72+
void createTan();
73+
void createAsin();
74+
void createAcos();
75+
void createAtan();
76+
void createLn();
77+
void createLog10();
78+
void createExp();
79+
void createExp10();
80+
6481
void moveToIf(std::shared_ptr<Block> substack);
6582
void moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
6683
void moveToRepeatLoop(std::shared_ptr<Block> substack);

‎src/dev/engine/compiler.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,102 @@ void Compiler::createNot()
160160
impl->builder->createNot();
161161
}
162162

163+
/*! Creates a remainder operation using the last 2 values. */
164+
void Compiler::createMod()
165+
{
166+
impl->builder->createMod();
167+
}
168+
169+
/*! Creates a round operation using the last value. */
170+
void Compiler::createRound()
171+
{
172+
impl->builder->createRound();
173+
}
174+
175+
/*! Creates an abs operation using the last value. */
176+
void Compiler::createAbs()
177+
{
178+
impl->builder->createAbs();
179+
}
180+
181+
/*! Creates a floor operation using the last value. */
182+
void Compiler::createFloor()
183+
{
184+
impl->builder->createFloor();
185+
}
186+
187+
/*! Creates a ceiling operation using the last value. */
188+
void Compiler::createCeil()
189+
{
190+
impl->builder->createCeil();
191+
}
192+
193+
/*! Creates a square root operation using the last value. */
194+
void Compiler::createSqrt()
195+
{
196+
impl->builder->createSqrt();
197+
}
198+
199+
/*! Creates a sin operation using the last value. */
200+
void Compiler::createSin()
201+
{
202+
impl->builder->createSin();
203+
}
204+
205+
/*! Creates a cos operation using the last value. */
206+
void Compiler::createCos()
207+
{
208+
impl->builder->createCos();
209+
}
210+
211+
/*! Creates a tan operation using the last value. */
212+
void Compiler::createTan()
213+
{
214+
impl->builder->createTan();
215+
}
216+
217+
/*! Creates an asin operation using the last value. */
218+
void Compiler::createAsin()
219+
{
220+
impl->builder->createAsin();
221+
}
222+
223+
/*! Creates an acos operation using the last value. */
224+
void Compiler::createAcos()
225+
{
226+
impl->builder->createAcos();
227+
}
228+
229+
/*! Creates an atan operation using the last value. */
230+
void Compiler::createAtan()
231+
{
232+
impl->builder->createAtan();
233+
}
234+
235+
/*! Creates an ln operation using the last value. */
236+
void Compiler::createLn()
237+
{
238+
impl->builder->createLn();
239+
}
240+
241+
/*! Creates a log10 operation using the last value. */
242+
void Compiler::createLog10()
243+
{
244+
impl->builder->createLog10();
245+
}
246+
247+
/*! Creates an e^x operation using the last value. */
248+
void Compiler::createExp()
249+
{
250+
impl->builder->createExp();
251+
}
252+
253+
/*! Creates a 10^x operation using the last value. */
254+
void Compiler::createExp10()
255+
{
256+
impl->builder->createExp10();
257+
}
258+
163259
/*! Jumps to the given if substack. */
164260
void Compiler::moveToIf(std::shared_ptr<Block> substack)
165261
{

‎test/dev/compiler/compiler_test.cpp

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,214 @@ TEST_F(CompilerTest, CreateNot)
349349
compile(compiler, block);
350350
}
351351

352+
TEST_F(CompilerTest, CreateMod)
353+
{
354+
Compiler compiler(&m_engine, &m_target);
355+
auto block = std::make_shared<Block>("", "");
356+
357+
block->setCompileFunction([](Compiler *compiler) {
358+
EXPECT_CALL(*m_builder, createMod);
359+
compiler->createMod();
360+
});
361+
362+
compile(compiler, block);
363+
}
364+
365+
TEST_F(CompilerTest, CreateRound)
366+
{
367+
Compiler compiler(&m_engine, &m_target);
368+
auto block = std::make_shared<Block>("", "");
369+
370+
block->setCompileFunction([](Compiler *compiler) {
371+
EXPECT_CALL(*m_builder, createRound);
372+
compiler->createRound();
373+
});
374+
375+
compile(compiler, block);
376+
}
377+
378+
TEST_F(CompilerTest, CreateAbs)
379+
{
380+
Compiler compiler(&m_engine, &m_target);
381+
auto block = std::make_shared<Block>("", "");
382+
383+
block->setCompileFunction([](Compiler *compiler) {
384+
EXPECT_CALL(*m_builder, createAbs);
385+
compiler->createAbs();
386+
});
387+
388+
compile(compiler, block);
389+
}
390+
391+
TEST_F(CompilerTest, CreateFloor)
392+
{
393+
Compiler compiler(&m_engine, &m_target);
394+
auto block = std::make_shared<Block>("", "");
395+
396+
block->setCompileFunction([](Compiler *compiler) {
397+
EXPECT_CALL(*m_builder, createFloor);
398+
compiler->createFloor();
399+
});
400+
401+
compile(compiler, block);
402+
}
403+
404+
TEST_F(CompilerTest, CreateCeil)
405+
{
406+
Compiler compiler(&m_engine, &m_target);
407+
auto block = std::make_shared<Block>("", "");
408+
409+
block->setCompileFunction([](Compiler *compiler) {
410+
EXPECT_CALL(*m_builder, createCeil);
411+
compiler->createCeil();
412+
});
413+
414+
compile(compiler, block);
415+
}
416+
417+
TEST_F(CompilerTest, CreateSqrt)
418+
{
419+
Compiler compiler(&m_engine, &m_target);
420+
auto block = std::make_shared<Block>("", "");
421+
422+
block->setCompileFunction([](Compiler *compiler) {
423+
EXPECT_CALL(*m_builder, createSqrt);
424+
compiler->createSqrt();
425+
});
426+
427+
compile(compiler, block);
428+
}
429+
430+
TEST_F(CompilerTest, CreateSin)
431+
{
432+
Compiler compiler(&m_engine, &m_target);
433+
auto block = std::make_shared<Block>("", "");
434+
435+
block->setCompileFunction([](Compiler *compiler) {
436+
EXPECT_CALL(*m_builder, createSin);
437+
compiler->createSin();
438+
});
439+
440+
compile(compiler, block);
441+
}
442+
443+
TEST_F(CompilerTest, CreateCos)
444+
{
445+
Compiler compiler(&m_engine, &m_target);
446+
auto block = std::make_shared<Block>("", "");
447+
448+
block->setCompileFunction([](Compiler *compiler) {
449+
EXPECT_CALL(*m_builder, createCos);
450+
compiler->createCos();
451+
});
452+
453+
compile(compiler, block);
454+
}
455+
456+
TEST_F(CompilerTest, CreateTan)
457+
{
458+
Compiler compiler(&m_engine, &m_target);
459+
auto block = std::make_shared<Block>("", "");
460+
461+
block->setCompileFunction([](Compiler *compiler) {
462+
EXPECT_CALL(*m_builder, createTan);
463+
compiler->createTan();
464+
});
465+
466+
compile(compiler, block);
467+
}
468+
469+
TEST_F(CompilerTest, CreateAsin)
470+
{
471+
Compiler compiler(&m_engine, &m_target);
472+
auto block = std::make_shared<Block>("", "");
473+
474+
block->setCompileFunction([](Compiler *compiler) {
475+
EXPECT_CALL(*m_builder, createAsin);
476+
compiler->createAsin();
477+
});
478+
479+
compile(compiler, block);
480+
}
481+
482+
TEST_F(CompilerTest, CreateAcos)
483+
{
484+
Compiler compiler(&m_engine, &m_target);
485+
auto block = std::make_shared<Block>("", "");
486+
487+
block->setCompileFunction([](Compiler *compiler) {
488+
EXPECT_CALL(*m_builder, createAcos);
489+
compiler->createAcos();
490+
});
491+
492+
compile(compiler, block);
493+
}
494+
495+
TEST_F(CompilerTest, CreateAtan)
496+
{
497+
Compiler compiler(&m_engine, &m_target);
498+
auto block = std::make_shared<Block>("", "");
499+
500+
block->setCompileFunction([](Compiler *compiler) {
501+
EXPECT_CALL(*m_builder, createAtan);
502+
compiler->createAtan();
503+
});
504+
505+
compile(compiler, block);
506+
}
507+
508+
TEST_F(CompilerTest, CreateLn)
509+
{
510+
Compiler compiler(&m_engine, &m_target);
511+
auto block = std::make_shared<Block>("", "");
512+
513+
block->setCompileFunction([](Compiler *compiler) {
514+
EXPECT_CALL(*m_builder, createLn);
515+
compiler->createLn();
516+
});
517+
518+
compile(compiler, block);
519+
}
520+
521+
TEST_F(CompilerTest, CreateLog10)
522+
{
523+
Compiler compiler(&m_engine, &m_target);
524+
auto block = std::make_shared<Block>("", "");
525+
526+
block->setCompileFunction([](Compiler *compiler) {
527+
EXPECT_CALL(*m_builder, createLog10);
528+
compiler->createLog10();
529+
});
530+
531+
compile(compiler, block);
532+
}
533+
534+
TEST_F(CompilerTest, CreateExp)
535+
{
536+
Compiler compiler(&m_engine, &m_target);
537+
auto block = std::make_shared<Block>("", "");
538+
539+
block->setCompileFunction([](Compiler *compiler) {
540+
EXPECT_CALL(*m_builder, createExp);
541+
compiler->createExp();
542+
});
543+
544+
compile(compiler, block);
545+
}
546+
547+
TEST_F(CompilerTest, CreateExp10)
548+
{
549+
Compiler compiler(&m_engine, &m_target);
550+
auto block = std::make_shared<Block>("", "");
551+
552+
block->setCompileFunction([](Compiler *compiler) {
553+
EXPECT_CALL(*m_builder, createExp10);
554+
compiler->createExp10();
555+
});
556+
557+
compile(compiler, block);
558+
}
559+
352560
TEST_F(CompilerTest, MoveToIf)
353561
{
354562
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)
Please sign in to comment.