Skip to content

Commit ef8a404

Browse files
committed
Add C++ instruction builder
1 parent 5314e61 commit ef8a404

25 files changed

Lines changed: 1185 additions & 0 deletions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ target_sources(scratchcpp
1313
cppcodebuilder.cpp
1414
cppcodebuilder.h
1515
)
16+
17+
add_subdirectory(instructions)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
target_sources(scratchcpp
2+
PRIVATE
3+
instructionbuilder.cpp
4+
instructionbuilder.h
5+
instructiongroup.cpp
6+
instructiongroup.h
7+
processresult.h
8+
)
9+
10+
target_sources(scratchcpp
11+
PRIVATE
12+
functions.cpp
13+
functions.h
14+
math.cpp
15+
math.h
16+
comparison.cpp
17+
comparison.h
18+
string.cpp
19+
string.h
20+
logic.cpp
21+
logic.h
22+
control.cpp
23+
control.h
24+
variables.cpp
25+
variables.h
26+
lists.cpp
27+
lists.h
28+
procedures.cpp
29+
procedures.h
30+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "comparison.h"
4+
#include "../cppinstruction.h"
5+
6+
using namespace libscratchcpp;
7+
using namespace libscratchcpp::cppins;
8+
9+
ProcessResult Comparison::process(CppInstruction *ins)
10+
{
11+
ProcessResult ret(true, ins);
12+
13+
switch (ins->type) {
14+
case CppInstruction::Type::CmpEQ:
15+
ret.next = buildCmpEQ(ins);
16+
break;
17+
18+
case CppInstruction::Type::CmpGT:
19+
ret.next = buildCmpGT(ins);
20+
break;
21+
22+
case CppInstruction::Type::CmpLT:
23+
ret.next = buildCmpLT(ins);
24+
break;
25+
26+
case CppInstruction::Type::StrCmpEQCS:
27+
ret.next = buildStrCmpEQCS(ins);
28+
break;
29+
30+
case CppInstruction::Type::StrCmpEQCI:
31+
ret.next = buildStrCmpEQCI(ins);
32+
break;
33+
34+
default:
35+
ret.match = false;
36+
break;
37+
}
38+
39+
return ret;
40+
}
41+
42+
CppInstruction *Comparison::buildCmpEQ(CppInstruction *ins)
43+
{
44+
return ins->getNext();
45+
}
46+
47+
CppInstruction *Comparison::buildCmpGT(CppInstruction *ins)
48+
{
49+
return ins->getNext();
50+
}
51+
52+
CppInstruction *Comparison::buildCmpLT(CppInstruction *ins)
53+
{
54+
return ins->getNext();
55+
}
56+
57+
CppInstruction *Comparison::buildStrCmpEQCS(CppInstruction *ins)
58+
{
59+
return ins->getNext();
60+
}
61+
62+
CppInstruction *Comparison::buildStrCmpEQCI(CppInstruction *ins)
63+
{
64+
return ins->getNext();
65+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "instructiongroup.h"
6+
7+
namespace libscratchcpp::cppins
8+
{
9+
10+
class Comparison : public InstructionGroup
11+
{
12+
public:
13+
using InstructionGroup::InstructionGroup;
14+
15+
ProcessResult process(CppInstruction *ins) override;
16+
17+
private:
18+
CppInstruction *buildCmpEQ(CppInstruction *ins);
19+
CppInstruction *buildCmpGT(CppInstruction *ins);
20+
CppInstruction *buildCmpLT(CppInstruction *ins);
21+
CppInstruction *buildStrCmpEQCS(CppInstruction *ins);
22+
CppInstruction *buildStrCmpEQCI(CppInstruction *ins);
23+
};
24+
25+
} // namespace libscratchcpp::cppins
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "control.h"
4+
#include "../cppinstruction.h"
5+
6+
using namespace libscratchcpp;
7+
using namespace libscratchcpp::cppins;
8+
9+
ProcessResult Control::process(CppInstruction *ins)
10+
{
11+
ProcessResult ret(true, ins);
12+
13+
switch (ins->type) {
14+
case CppInstruction::Type::Select:
15+
ret.next = buildSelect(ins);
16+
break;
17+
18+
case CppInstruction::Type::Yield:
19+
ret.next = buildYield(ins);
20+
break;
21+
22+
case CppInstruction::Type::BeginIf:
23+
ret.next = buildBeginIf(ins);
24+
break;
25+
26+
case CppInstruction::Type::BeginElse:
27+
ret.next = buildBeginElse(ins);
28+
break;
29+
30+
case CppInstruction::Type::EndIf:
31+
ret.next = buildEndIf(ins);
32+
break;
33+
34+
case CppInstruction::Type::BeginRepeatLoop:
35+
ret.next = buildBeginRepeatLoop(ins);
36+
break;
37+
38+
case CppInstruction::Type::LoopIndex:
39+
ret.next = buildLoopIndex(ins);
40+
break;
41+
42+
case CppInstruction::Type::BeginWhileLoop:
43+
ret.next = buildBeginWhileLoop(ins);
44+
break;
45+
46+
case CppInstruction::Type::BeginRepeatUntilLoop:
47+
ret.next = buildBeginRepeatUntilLoop(ins);
48+
break;
49+
50+
case CppInstruction::Type::BeginLoopCondition:
51+
ret.next = buildBeginLoopCondition(ins);
52+
break;
53+
54+
case CppInstruction::Type::EndLoop:
55+
ret.next = buildEndLoop(ins);
56+
break;
57+
58+
case CppInstruction::Type::Stop:
59+
ret.next = buildStop(ins);
60+
break;
61+
62+
case CppInstruction::Type::ThreadStop:
63+
ret.next = buildThreadStop(ins);
64+
break;
65+
66+
case CppInstruction::Type::InvalidateTarget:
67+
ret.next = buildInvalidateTarget(ins);
68+
break;
69+
70+
case CppInstruction::Type::Print:
71+
ret.next = buildPrint(ins);
72+
break;
73+
74+
default:
75+
ret.match = false;
76+
break;
77+
}
78+
79+
return ret;
80+
}
81+
82+
CppInstruction *Control::buildSelect(CppInstruction *ins)
83+
{
84+
return ins->getNext();
85+
}
86+
87+
CppInstruction *Control::buildYield(CppInstruction *ins)
88+
{
89+
return ins->getNext();
90+
}
91+
92+
CppInstruction *Control::buildBeginIf(CppInstruction *ins)
93+
{
94+
return ins->getNext();
95+
}
96+
97+
CppInstruction *Control::buildBeginElse(CppInstruction *ins)
98+
{
99+
return ins->getNext();
100+
}
101+
102+
CppInstruction *Control::buildEndIf(CppInstruction *ins)
103+
{
104+
return ins->getNext();
105+
}
106+
107+
CppInstruction *Control::buildBeginRepeatLoop(CppInstruction *ins)
108+
{
109+
return ins->getNext();
110+
}
111+
112+
CppInstruction *Control::buildLoopIndex(CppInstruction *ins)
113+
{
114+
return ins->getNext();
115+
}
116+
117+
CppInstruction *Control::buildBeginWhileLoop(CppInstruction *ins)
118+
{
119+
return ins->getNext();
120+
}
121+
122+
CppInstruction *Control::buildBeginRepeatUntilLoop(CppInstruction *ins)
123+
{
124+
return ins->getNext();
125+
}
126+
127+
CppInstruction *Control::buildBeginLoopCondition(CppInstruction *ins)
128+
{
129+
return ins->getNext();
130+
}
131+
132+
CppInstruction *Control::buildEndLoop(CppInstruction *ins)
133+
{
134+
return ins->getNext();
135+
}
136+
137+
CppInstruction *Control::buildStop(CppInstruction *ins)
138+
{
139+
return ins->getNext();
140+
}
141+
142+
CppInstruction *Control::buildThreadStop(CppInstruction *ins)
143+
{
144+
return ins->getNext();
145+
}
146+
147+
CppInstruction *Control::buildInvalidateTarget(CppInstruction *ins)
148+
{
149+
return ins->getNext();
150+
}
151+
152+
CppInstruction *Control::buildPrint(CppInstruction *ins)
153+
{
154+
return ins->getNext();
155+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "instructiongroup.h"
6+
7+
namespace libscratchcpp::cppins
8+
{
9+
10+
class Control : public InstructionGroup
11+
{
12+
public:
13+
using InstructionGroup::InstructionGroup;
14+
15+
ProcessResult process(CppInstruction *ins) override;
16+
17+
private:
18+
CppInstruction *buildSelect(CppInstruction *ins);
19+
CppInstruction *buildYield(CppInstruction *ins);
20+
CppInstruction *buildBeginIf(CppInstruction *ins);
21+
CppInstruction *buildBeginElse(CppInstruction *ins);
22+
CppInstruction *buildEndIf(CppInstruction *ins);
23+
CppInstruction *buildBeginRepeatLoop(CppInstruction *ins);
24+
CppInstruction *buildLoopIndex(CppInstruction *ins);
25+
CppInstruction *buildBeginWhileLoop(CppInstruction *ins);
26+
CppInstruction *buildBeginRepeatUntilLoop(CppInstruction *ins);
27+
CppInstruction *buildBeginLoopCondition(CppInstruction *ins);
28+
CppInstruction *buildEndLoop(CppInstruction *ins);
29+
CppInstruction *buildStop(CppInstruction *ins);
30+
CppInstruction *buildThreadStop(CppInstruction *ins);
31+
CppInstruction *buildInvalidateTarget(CppInstruction *ins);
32+
CppInstruction *buildPrint(CppInstruction *ins);
33+
};
34+
35+
} // namespace libscratchcpp::cppins
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "functions.h"
4+
#include "../cppinstruction.h"
5+
6+
using namespace libscratchcpp;
7+
using namespace libscratchcpp::cppins;
8+
9+
ProcessResult Functions::process(CppInstruction *ins)
10+
{
11+
ProcessResult ret(true, ins);
12+
13+
switch (ins->type) {
14+
case CppInstruction::Type::FunctionCall:
15+
ret.next = buildFunctionCall(ins);
16+
break;
17+
18+
default:
19+
ret.match = false;
20+
break;
21+
}
22+
23+
return ret;
24+
}
25+
26+
CppInstruction *Functions::buildFunctionCall(CppInstruction *ins)
27+
{
28+
return ins->getNext();
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "instructiongroup.h"
6+
7+
namespace libscratchcpp::cppins
8+
{
9+
10+
class Functions : public InstructionGroup
11+
{
12+
public:
13+
using InstructionGroup::InstructionGroup;
14+
15+
ProcessResult process(CppInstruction *ins) override;
16+
17+
private:
18+
CppInstruction *buildFunctionCall(CppInstruction *ins);
19+
};
20+
21+
} // namespace libscratchcpp::cppins

0 commit comments

Comments
 (0)