Skip to content

Commit 26a8a2b

Browse files
committed
Add base classes for generic codegen classes
1 parent 672ce41 commit 26a8a2b

16 files changed

Lines changed: 282 additions & 221 deletions

‎src/engine/internal/codegen/CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ target_sources(scratchcpp
44
icodebuilderfactory.h
55
codebuilderfactory.cpp
66
codebuilderfactory.h
7+
instructionbase.h
78
instruction.h
9+
instructionlistbase.cpp
10+
instructionlistbase.h
811
instructionlist.h
912
)
1013

‎src/engine/internal/codegen/instruction.h‎

Lines changed: 7 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,109 +2,23 @@
22

33
#pragma once
44

5-
#include <scratchcpp/compiler.h>
5+
#include "instructionbase.h"
66

77
namespace libscratchcpp
88
{
99

10-
class BlockPrototype;
11-
1210
template<typename TInstruction, typename TRegister>
13-
struct Instruction
11+
struct Instruction : public InstructionBase
1412
{
15-
enum class Type
16-
{
17-
FunctionCall,
18-
Add,
19-
Sub,
20-
Mul,
21-
Div,
22-
Random,
23-
RandomInt,
24-
CmpEQ,
25-
CmpGT,
26-
CmpLT,
27-
StrCmpEQCS, // case sensitive
28-
StrCmpEQCI, // case insensitive
29-
And,
30-
Or,
31-
Not,
32-
Mod,
33-
Round,
34-
Abs,
35-
Floor,
36-
Ceil,
37-
Sqrt,
38-
Sin,
39-
Cos,
40-
Tan,
41-
Asin,
42-
Acos,
43-
Atan,
44-
Ln,
45-
Log10,
46-
Exp,
47-
Exp10,
48-
StringConcat,
49-
StringChar,
50-
StringLength,
51-
Select,
52-
CreateLocalVariable,
53-
WriteLocalVariable,
54-
ReadLocalVariable,
55-
WriteVariable,
56-
ReadVariable,
57-
ClearList,
58-
RemoveListItem,
59-
AppendToList,
60-
InsertToList,
61-
ListReplace,
62-
GetListContents,
63-
GetListItem,
64-
GetListSize,
65-
GetListItemIndex,
66-
ListContainsItem,
67-
Yield,
68-
BeginIf,
69-
BeginElse,
70-
EndIf,
71-
BeginRepeatLoop,
72-
LoopIndex,
73-
BeginWhileLoop,
74-
BeginRepeatUntilLoop,
75-
BeginLoopCondition,
76-
EndLoop,
77-
Stop,
78-
ThreadStop,
79-
InvalidateTarget,
80-
CallProcedure,
81-
ProcedureArg,
82-
Print
83-
};
13+
using Type = InstructionBase::Type;
14+
using InstructionBase::InstructionBase; // inherit constructor
15+
16+
inline TInstruction *getPrevious() { return static_cast<TInstruction *>(previous); }
8417

85-
Instruction(Type type, bool loopCondition) :
86-
type(type),
87-
loopCondition(loopCondition)
88-
{
89-
}
18+
inline TInstruction *getNext() { return static_cast<TInstruction *>(next); }
9019

91-
Type type;
92-
std::string functionName;
9320
std::vector<std::pair<Compiler::StaticType, TRegister *>> args; // target type, register
9421
TRegister *functionReturnReg = nullptr;
95-
bool functionTargetArg = false; // whether to add target ptr to function parameters
96-
bool functionCtxArg = false; // whether to add execution context ptr to function parameters
97-
Variable *targetVariable = nullptr; // for variables
98-
List *targetList = nullptr; // for lists
99-
Compiler::StaticType targetType = Compiler::StaticType::Unknown; // variable or list type (before read/write operation)
100-
BlockPrototype *procedurePrototype = nullptr;
101-
size_t procedureArgIndex = 0;
102-
bool loopCondition = false; // whether the instruction is part of a loop condition
103-
104-
// Linked list
105-
TInstruction *previous = nullptr;
106-
TInstruction *next = nullptr;
107-
std::shared_ptr<TInstruction> _next; // ensure items are not deleted
10822
};
10923

11024
} // namespace libscratchcpp
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/compiler.h>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class BlockPrototype;
11+
12+
struct InstructionBase
13+
{
14+
enum class Type
15+
{
16+
FunctionCall,
17+
Add,
18+
Sub,
19+
Mul,
20+
Div,
21+
Random,
22+
RandomInt,
23+
CmpEQ,
24+
CmpGT,
25+
CmpLT,
26+
StrCmpEQCS, // case sensitive
27+
StrCmpEQCI, // case insensitive
28+
And,
29+
Or,
30+
Not,
31+
Mod,
32+
Round,
33+
Abs,
34+
Floor,
35+
Ceil,
36+
Sqrt,
37+
Sin,
38+
Cos,
39+
Tan,
40+
Asin,
41+
Acos,
42+
Atan,
43+
Ln,
44+
Log10,
45+
Exp,
46+
Exp10,
47+
StringConcat,
48+
StringChar,
49+
StringLength,
50+
Select,
51+
CreateLocalVariable,
52+
WriteLocalVariable,
53+
ReadLocalVariable,
54+
WriteVariable,
55+
ReadVariable,
56+
ClearList,
57+
RemoveListItem,
58+
AppendToList,
59+
InsertToList,
60+
ListReplace,
61+
GetListContents,
62+
GetListItem,
63+
GetListSize,
64+
GetListItemIndex,
65+
ListContainsItem,
66+
Yield,
67+
BeginIf,
68+
BeginElse,
69+
EndIf,
70+
BeginRepeatLoop,
71+
LoopIndex,
72+
BeginWhileLoop,
73+
BeginRepeatUntilLoop,
74+
BeginLoopCondition,
75+
EndLoop,
76+
Stop,
77+
ThreadStop,
78+
InvalidateTarget,
79+
CallProcedure,
80+
ProcedureArg,
81+
Print
82+
};
83+
84+
InstructionBase(Type type, bool loopCondition) :
85+
type(type),
86+
loopCondition(loopCondition)
87+
{
88+
}
89+
90+
Type type;
91+
std::string functionName;
92+
bool functionTargetArg = false; // whether to add target ptr to function parameters
93+
bool functionCtxArg = false; // whether to add execution context ptr to function parameters
94+
Variable *targetVariable = nullptr; // for variables
95+
List *targetList = nullptr; // for lists
96+
Compiler::StaticType targetType = Compiler::StaticType::Unknown; // variable or list type (before read/write operation)
97+
BlockPrototype *procedurePrototype = nullptr;
98+
size_t procedureArgIndex = 0;
99+
bool loopCondition = false; // whether the instruction is part of a loop condition
100+
101+
// Linked list
102+
InstructionBase *previous = nullptr;
103+
InstructionBase *next = nullptr;
104+
std::shared_ptr<InstructionBase> _next; // ensure items are not deleted
105+
};
106+
107+
} // namespace libscratchcpp

‎src/engine/internal/codegen/instructionlist.h‎

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,27 @@
22

33
#pragma once
44

5-
#include <memory>
6-
#include <functional>
7-
8-
#include "test_export.h"
5+
#include "instructionlistbase.h"
96

107
namespace libscratchcpp
118
{
129

1310
template<typename TInstruction>
14-
class LIBSCRATCHCPP_TEST_EXPORT InstructionList
11+
class InstructionList : public InstructionListBase
1512
{
1613
public:
17-
InstructionList() = default;
18-
InstructionList(const InstructionList &) = delete;
19-
20-
inline TInstruction *first() const { return m_first.get(); }
21-
22-
inline TInstruction *last() const { return m_last.get(); }
23-
24-
inline bool empty() const { return !first(); }
25-
26-
inline bool containsInstruction(TInstruction *ins) const
27-
{
28-
TInstruction *ptr = m_first.get();
29-
30-
while (ptr) {
31-
if (ptr == ins)
32-
return true;
33-
34-
ptr = ptr->next;
35-
}
14+
inline TInstruction *first() const { return static_cast<TInstruction *>(InstructionListBase::first()); }
3615

37-
return false;
38-
}
39-
40-
inline bool containsInstruction(std::function<bool(const TInstruction *)> func) const
41-
{
42-
TInstruction *ptr = m_first.get();
43-
44-
while (ptr) {
45-
if (func(ptr))
46-
return true;
16+
inline TInstruction *last() const { return static_cast<TInstruction *>(InstructionListBase::last()); }
4717

48-
ptr = ptr->next;
49-
}
18+
bool containsInstruction(InstructionBase *ins) const { return InstructionListBase::containsInstruction(ins); }
5019

51-
return false;
52-
}
53-
54-
inline void addInstruction(std::shared_ptr<TInstruction> ins)
20+
bool containsInstruction(std::function<bool(const TInstruction *)> func) const
5521
{
56-
if (!(m_first && m_last)) {
57-
assert(!m_first && !m_last);
58-
59-
m_first = ins;
60-
m_last = ins;
61-
62-
ins->previous = nullptr;
63-
ins->next = nullptr;
64-
ins->_next = nullptr;
65-
} else {
66-
m_last->next = ins.get();
67-
m_last->_next = ins;
68-
ins->previous = m_last.get();
69-
m_last = ins;
70-
}
22+
return InstructionListBase::containsInstruction([&](const InstructionBase *i) { return func(static_cast<const TInstruction *>(i)); });
7123
}
7224

73-
private:
74-
std::shared_ptr<TInstruction> m_first;
75-
std::shared_ptr<TInstruction> m_last;
25+
void addInstruction(std::shared_ptr<TInstruction> ins) { return InstructionListBase::addInstruction(std::move(ins)); }
7626
};
7727

7828
} // namespace libscratchcpp
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "instructionlistbase.h"
2+
3+
using namespace libscratchcpp;
4+
5+
bool InstructionListBase::containsInstruction(InstructionBase *ins) const
6+
{
7+
InstructionBase *ptr = m_first.get();
8+
9+
while (ptr) {
10+
if (ptr == ins)
11+
return true;
12+
13+
ptr = ptr->next;
14+
}
15+
16+
return false;
17+
}
18+
19+
bool InstructionListBase::containsInstruction(std::function<bool(const InstructionBase *)> func) const
20+
{
21+
InstructionBase *ptr = m_first.get();
22+
23+
while (ptr) {
24+
if (func(ptr))
25+
return true;
26+
27+
ptr = ptr->next;
28+
}
29+
30+
return false;
31+
}
32+
33+
void InstructionListBase::addInstruction(std::shared_ptr<InstructionBase> ins)
34+
{
35+
if (!(m_first && m_last)) {
36+
assert(!m_first && !m_last);
37+
38+
m_first = ins;
39+
m_last = ins;
40+
41+
ins->previous = nullptr;
42+
ins->next = nullptr;
43+
ins->_next = nullptr;
44+
} else {
45+
m_last->next = ins.get();
46+
m_last->_next = ins;
47+
ins->previous = m_last.get();
48+
m_last = ins;
49+
}
50+
}

0 commit comments

Comments
 (0)