Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/engine/internal/codegen/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ target_sources(scratchcpp
cppconstant.h
cppcodebuilder.cpp
cppcodebuilder.h
cppcodegenerator.cpp
cppcodegenerator.h
cppcodestream.h
cpptypeinfo.h
cppidentifier.h
cppconstidentifier.h
cppnamedidentifier.h
cppassignment.h
cppfunctioncall.h
cppswitch.h
)

add_subdirectory(instructions)
58 changes: 58 additions & 0 deletions src/engine/internal/codegen/cpp/cppassignment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "cppnamedidentifier.h"
#include "cppcodestream.h"

namespace libscratchcpp
{

class CppAssignment
{
public:
CppAssignment(CppCodeStream &out, const CppNamedIdentifier *dst) :
m_out(out)
{
if (!dst->isMutable) {
throw std::runtime_error("cannot assign to '" + dst->name + "'");
}

m_out.addToLine(dst->name);
m_out.addToLine(" = ");
}

CppAssignment(CppCodeStream &out, const CppNamedIdentifier *dstObj, const std::string &dstMember) :
m_out(out)
{
if (!dstObj->isMutable) {
throw std::runtime_error("cannot assign to member of '" + dstObj->name + "'");
}

m_out.addToLine(dstObj->name);
m_out.addToLine(".");
m_out.addToLine(dstMember);
m_out.addToLine(" = ");
}

~CppAssignment()
{
if (!m_closed)
close();
}

CppAssignment(const CppAssignment &) = delete;
CppAssignment &operator=(const CppAssignment &) = delete;

void close()
{
m_closed = true;
m_out.addToLine(";");
}

private:
CppCodeStream &m_out;
bool m_closed = false;
};

} // namespace libscratchcpp
Loading
Loading