Skip to content

Commit 267d030

Browse files
committed
CppCodeGenerator: Add finalize() method
1 parent d9c16a3 commit 267d030

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎src/engine/internal/codegen/cpp/cppcodegenerator.cpp‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3-
#include <iostream>
43
#include <optional>
4+
#include <unordered_set>
55

66
#include "cppcodegenerator.h"
77
#include "cppassignment.h"
@@ -43,7 +43,61 @@ CppCodeGenerator::CppCodeGenerator() :
4343

4444
CppCodeGenerator::~CppCodeGenerator()
4545
{
46-
std::cout << m_codeStream.code() << std::endl;
46+
}
47+
48+
std::string CppCodeGenerator::finalize()
49+
{
50+
static unsigned int scriptCount = 0;
51+
52+
const std::string scriptName = "Script" + std::to_string(scriptCount++);
53+
54+
std::ostringstream source;
55+
source << "#include \"script" + std::to_string(scriptCount++) + ".h\"\n";
56+
57+
for (Header header : m_includedHeaders) {
58+
source << "#include " << m_headers.at(header) << "\n";
59+
}
60+
61+
source << "\nnamespace libscratchcpp\n{\n\n";
62+
source << "void " << scriptName << "::runScript(ExecutionContext *ctx, Target *target, ValueData **targetVariables, List **targetLists)\n{\n";
63+
64+
std::unordered_set<const CppIdentifier *> declared;
65+
const auto declare = [&](const CppIdentifier *identifier) {
66+
if (!identifier || !identifier->isNamed() || !declared.insert(identifier).second)
67+
return;
68+
69+
const auto *named = static_cast<const CppNamedIdentifier *>(identifier);
70+
71+
source << " " << identifier->typeString();
72+
source << " " << named->name << ";\n";
73+
};
74+
75+
for (const auto &entry : m_identifiers)
76+
declare(entry.second.get());
77+
78+
for (const auto &identifier : m_internalIdentifiers)
79+
declare(identifier.get());
80+
81+
for (const auto &identifier : m_strings)
82+
declare(identifier.get());
83+
84+
if (!m_strings.empty()) {
85+
for (size_t i = 0; i < m_strings.size(); ++i)
86+
source << " StringPtr " << m_strings[i]->name << "Storage;\n";
87+
88+
source << " StringPtr *" << m_stringArray.name << "[" << m_strings.size() << "] = { ";
89+
90+
for (size_t i = 0; i < m_strings.size(); ++i) {
91+
if (i)
92+
source << ", ";
93+
source << "&" << m_strings[i]->name << "Storage";
94+
}
95+
source << " };\n";
96+
}
97+
98+
source << m_codeStream.code();
99+
source << "}\n\n} // namespace libscratchcpp\n";
100+
return source.str();
47101
}
48102

49103
const CppIdentifier *CppCodeGenerator::castValue(CppValue *value, Compiler::StaticType targetType)

‎src/engine/internal/codegen/cpp/cppcodegenerator.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class CppCodeGenerator
2525

2626
const CppIdentifier *castValue(CppValue *value, Compiler::StaticType targetType);
2727

28+
std::string finalize();
29+
2830
void createCall(const std::string &name, bool ctxArg, bool targetArg, const std::vector<const CppIdentifier *> &args);
2931
void createCall(const std::string &name, CppIdentifier::Type retType, CppValue *retValue, bool ctxArg, bool targetArg, const std::vector<const CppIdentifier *> &args);
3032

@@ -80,7 +82,7 @@ class CppCodeGenerator
8082
std::vector<std::shared_ptr<CppConstIdentifier>> m_constants;
8183

8284
CppNamedIdentifier m_stringArray;
83-
std::vector<std::shared_ptr<CppIdentifier>> m_strings;
85+
std::vector<std::shared_ptr<CppNamedIdentifier>> m_strings;
8486
};
8587

8688
} // namespace libscratchcpp

0 commit comments

Comments
 (0)