Skip to content
Merged
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
10 changes: 10 additions & 0 deletions include/scratchcpp/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ class LIBSCRATCHCPP_EXPORT Compiler
CompilerValue *addTargetFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
CompilerConstant *addConstValue(const Value &value);

CompilerValue *addTargetPointer(const std::string &name);

CompilerValue *addVariablePointer(Variable *variable);

CompilerValue *addListPointer(List *list);

CompilerValue *addTimerPointer();

CompilerValue *addEffectPointer(const std::string &name);
CompilerValue *addStringChar(CompilerValue *string, CompilerValue *index);
CompilerValue *addStringLength(CompilerValue *string);
CompilerValue *addLoopIndex();
Expand Down
6 changes: 3 additions & 3 deletions src/blocks/listblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ CompilerValue *ListBlocks::compileListContents(Compiler *compiler)

// If this block is used in a list monitor, return the list pointer
if (compiler->block()->isMonitorBlock())
return compiler->addConstValue(list.get());
return compiler->addListPointer(static_cast<List *>(list.get()));
else
return compiler->addListContents(static_cast<List *>(list.get()));
}
Expand Down Expand Up @@ -224,7 +224,7 @@ CompilerValue *ListBlocks::compileShowList(Compiler *compiler)
List *list = static_cast<List *>(field->valuePtr().get());
assert(list);

CompilerConstant *listPtr = compiler->addConstValue(list);
CompilerValue *listPtr = compiler->addListPointer(list);
compiler->addTargetFunctionCall("data_showlist", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { listPtr });

return nullptr;
Expand All @@ -237,7 +237,7 @@ CompilerValue *ListBlocks::compileHideList(Compiler *compiler)
List *list = static_cast<List *>(field->valuePtr().get());
assert(list);

CompilerConstant *listPtr = compiler->addConstValue(list);
CompilerValue *listPtr = compiler->addListPointer(list);
compiler->addTargetFunctionCall("data_hidelist", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { listPtr });

return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/looksblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void LooksBlocks::compileSetOrChangeEffect(Compiler *compiler, const std::string
IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect(effectName);

if (effect) {
CompilerValue *effectPtr = compiler->addConstValue(effect);
CompilerValue *effectPtr = compiler->addEffectPointer(effectName);
compiler->addTargetFunctionCall(function, Compiler::StaticType::Void, { Compiler::StaticType::Pointer, Compiler::StaticType::Number }, { effectPtr, arg });
} else
std::cout << "warning: graphic effect '" << effectName << "' is not registered" << std::endl;
Expand Down
10 changes: 6 additions & 4 deletions src/blocks/motionblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CompilerValue *MotionBlocks::compilePointTowards(Compiler *compiler)
Target *anotherTarget = compiler->engine()->targetAt(index);

if (anotherTarget && !anotherTarget->isStage())
compiler->addTargetFunctionCall("motion_point_towards_sprite", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { compiler->addConstValue(anotherTarget) });
compiler->addTargetFunctionCall("motion_point_towards_sprite", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { compiler->addTargetPointer(anotherTarget->name()) });
}
} else {
CompilerValue *towards = compiler->addInput(input);
Expand Down Expand Up @@ -156,7 +156,7 @@ CompilerValue *MotionBlocks::compileGoTo(Compiler *compiler)
Target *anotherTarget = compiler->engine()->targetAt(index);

if (anotherTarget && !anotherTarget->isStage())
compiler->addTargetFunctionCall("motion_go_to_sprite", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { compiler->addConstValue(anotherTarget) });
compiler->addTargetFunctionCall("motion_go_to_sprite", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { compiler->addTargetPointer(anotherTarget->name()) });
}
} else {
CompilerValue *to = compiler->addInput(input);
Expand Down Expand Up @@ -218,8 +218,10 @@ CompilerValue *MotionBlocks::compileGlideTo(Compiler *compiler)

if (anotherTarget && !anotherTarget->isStage()) {
if (!target->isStage()) {
endX = compiler->addFunctionCallWithCtx("motion_get_sprite_x", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { compiler->addConstValue(anotherTarget) });
endY = compiler->addFunctionCallWithCtx("motion_get_sprite_y", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { compiler->addConstValue(anotherTarget) });
endX =
compiler->addFunctionCallWithCtx("motion_get_sprite_x", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { compiler->addTargetPointer(anotherTarget->name()) });
endY =
compiler->addFunctionCallWithCtx("motion_get_sprite_y", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { compiler->addTargetPointer(anotherTarget->name()) });
}
} else
return nullptr;
Expand Down
14 changes: 6 additions & 8 deletions src/blocks/sensingblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ CompilerValue *SensingBlocks::compileTouchingObject(Compiler *compiler)
Target *target = engine->targetAt(engine->findTarget(value));

if (target) {
CompilerValue *targetPtr = compiler->addConstValue(target);
CompilerValue *targetPtr = compiler->addTargetPointer(value);
return compiler->addTargetFunctionCall("sensing_touching_sprite", Compiler::StaticType::Bool, { Compiler::StaticType::Pointer }, { targetPtr });
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ CompilerValue *SensingBlocks::compileDistanceTo(Compiler *compiler)
Target *target = engine->targetAt(engine->findTarget(value));

if (target) {
CompilerValue *targetPtr = compiler->addConstValue(target);
CompilerValue *targetPtr = compiler->addTargetPointer(value);
return compiler->addTargetFunctionCall("sensing_distance_to_sprite", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { targetPtr });
}
}
Expand Down Expand Up @@ -237,15 +237,13 @@ CompilerValue *SensingBlocks::compileLoud(Compiler *compiler)

CompilerValue *SensingBlocks::compileTimer(Compiler *compiler)
{
ITimer *timer = compiler->engine()->timer();
CompilerValue *timerPtr = compiler->addConstValue(timer);
CompilerValue *timerPtr = compiler->addTimerPointer();
return compiler->addFunctionCall("sensing_timer", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { timerPtr });
}

CompilerValue *SensingBlocks::compileResetTimer(Compiler *compiler)
{
ITimer *timer = compiler->engine()->timer();
CompilerValue *timerPtr = compiler->addConstValue(timer);
CompilerValue *timerPtr = compiler->addTimerPointer();
compiler->addFunctionCall("sensing_resettimer", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { timerPtr });
return nullptr;
}
Expand All @@ -269,7 +267,7 @@ CompilerValue *SensingBlocks::compileOf(Compiler *compiler)
if (value == "_stage_") {
// Stage properties
target = engine->stage();
targetPtr = compiler->addConstValue(target);
targetPtr = compiler->addTargetPointer("_stage_");

if (property == "backdrop #")
return compiler->addFunctionCall("sensing_costume_number_of_target", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { targetPtr });
Expand All @@ -280,7 +278,7 @@ CompilerValue *SensingBlocks::compileOf(Compiler *compiler)
target = engine->targetAt(engine->findTarget(value));

if (target) {
targetPtr = compiler->addConstValue(target);
targetPtr = compiler->addTargetPointer(value);

if (property == "x position")
return compiler->addFunctionCall("sensing_x_position_of_sprite", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { targetPtr });
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/variableblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ CompilerValue *VariableBlocks::compileShowVariable(Compiler *compiler)
Variable *var = static_cast<Variable *>(field->valuePtr().get());
assert(var);

CompilerConstant *varPtr = compiler->addConstValue(var);
CompilerValue *varPtr = compiler->addVariablePointer(var);
compiler->addTargetFunctionCall("data_showvariable", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { varPtr });

return nullptr;
Expand All @@ -103,7 +103,7 @@ CompilerValue *VariableBlocks::compileHideVariable(Compiler *compiler)
Variable *var = static_cast<Variable *>(field->valuePtr().get());
assert(var);

CompilerConstant *varPtr = compiler->addConstValue(var);
CompilerValue *varPtr = compiler->addVariablePointer(var);
compiler->addTargetFunctionCall("data_hidevariable", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { varPtr });

return nullptr;
Expand Down
36 changes: 36 additions & 0 deletions src/engine/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,45 @@ CompilerValue *Compiler::addFunctionCallWithCtx(const std::string &functionName,
/*! Adds the given constant to the compiled code. */
CompilerConstant *Compiler::addConstValue(const Value &value)
{
if (value.isPointer()) {
std::cerr << "error: constant pointers are not supported" << std::endl;
assert(false);
return nullptr;
}

return static_cast<CompilerConstant *>(impl->builder->addConstValue(value));
}

/*! Adds a pointer to a target that is identified by its name. */
CompilerValue *Compiler::addTargetPointer(const std::string &name)
{
return impl->builder->addTargetPointer(name);
}

/*! Adds a pointer to a variable that is identified by its ID. */
CompilerValue *Compiler::addVariablePointer(Variable *variable)
{
return impl->builder->addVariablePointer(variable);
}

/*! Adds a pointer to a list that is identified by its ID. */
CompilerValue *Compiler::addListPointer(List *list)
{
return impl->builder->addListPointer(list);
}

/*! Adds a pointer to the engine's timer. */
CompilerValue *Compiler::addTimerPointer()
{
return impl->builder->addTimerPointer();
}

/*! Adds a pointer to the graphics effect that is identified by its name. */
CompilerValue *Compiler::addEffectPointer(const std::string &name)
{
return impl->builder->addEffectPointer(name);
}

/*! Adds the string character with the given index to the compiled code. */
CompilerValue *Compiler::addStringChar(CompilerValue *string, CompilerValue *index)
{
Expand Down
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