Skip to content

Commit 1971123

Browse files
committed
Compiler: Add target and ctx function call methods
1 parent beb0f98 commit 1971123

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class LIBSCRATCHCPP_EXPORT Compiler
4848
std::shared_ptr<ExecutableCode> compile(std::shared_ptr<Block> startBlock);
4949

5050
CompilerValue *addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
51+
CompilerValue *addTargetFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
52+
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
5153
CompilerConstant *addConstValue(const Value &value);
5254
CompilerValue *addVariableValue(Variable *variable);
5355
CompilerValue *addListContents(List *list);

src/dev/engine/compiler.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,34 @@ std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBl
7676

7777
/*!
7878
* Adds a call to the given function.\n
79-
* For example: extern "C" bool some_block(Target *target, double arg1, const char *arg2)
79+
* For example: extern "C" bool some_block(double arg1, const char *arg2)
8080
*/
8181
CompilerValue *Compiler::addFunctionCall(const std::string &functionName, StaticType returnType, const ArgTypes &argTypes, const Args &args)
8282
{
8383
assert(argTypes.size() == args.size());
8484
return impl->builder->addFunctionCall(functionName, returnType, argTypes, args);
8585
}
8686

87+
/*!
88+
* Adds a call to the given function with a target parameter.\n
89+
* For example: extern "C" bool some_block(Target *target, double arg1, const char *arg2)
90+
*/
91+
CompilerValue *Compiler::addTargetFunctionCall(const std::string &functionName, StaticType returnType, const ArgTypes &argTypes, const Args &args)
92+
{
93+
assert(argTypes.size() == args.size());
94+
return impl->builder->addTargetFunctionCall(functionName, returnType, argTypes, args);
95+
}
96+
97+
/*!
98+
* Adds a call to the given function with an execution context parameter.\n
99+
* For example: extern "C" bool some_block(ExecutionContext *ctx, double arg1, const char *arg2)
100+
*/
101+
CompilerValue *Compiler::addFunctionCallWithCtx(const std::string &functionName, StaticType returnType, const ArgTypes &argTypes, const Args &args)
102+
{
103+
assert(argTypes.size() == args.size());
104+
return impl->builder->addFunctionCallWithCtx(functionName, returnType, argTypes, args);
105+
}
106+
87107
/*! Adds the given constant to the compiled code. */
88108
CompilerConstant *Compiler::addConstValue(const Value &value)
89109
{

test/dev/compiler/compiler_test.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,56 @@ TEST_F(CompilerTest, AddFunctionCall)
8989
compile(compiler, block);
9090
}
9191

92+
TEST_F(CompilerTest, AddTargetFunctionCall)
93+
{
94+
Compiler compiler(&m_engine, &m_target);
95+
auto block = std::make_shared<Block>("a", "");
96+
m_compareBlock = block;
97+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
98+
EXPECT_EQ(compiler->block(), m_compareBlock);
99+
CompilerValue arg1(Compiler::StaticType::Unknown);
100+
CompilerValue arg2(Compiler::StaticType::Unknown);
101+
Compiler::ArgTypes argTypes = { Compiler::StaticType::Number, Compiler::StaticType::Bool };
102+
Compiler::Args args = { &arg1, &arg2 };
103+
EXPECT_CALL(*m_builder, addTargetFunctionCall("test1", Compiler::StaticType::Void, argTypes, args));
104+
compiler->addTargetFunctionCall("test1", Compiler::StaticType::Void, argTypes, args);
105+
106+
args = { &arg1 };
107+
argTypes = { Compiler::StaticType::String };
108+
EXPECT_CALL(*m_builder, addTargetFunctionCall("test2", Compiler::StaticType::Bool, argTypes, args));
109+
compiler->addTargetFunctionCall("test2", Compiler::StaticType::Bool, argTypes, args);
110+
111+
return nullptr;
112+
});
113+
114+
compile(compiler, block);
115+
}
116+
117+
TEST_F(CompilerTest, AddFunctionCallWithCtx)
118+
{
119+
Compiler compiler(&m_engine, &m_target);
120+
auto block = std::make_shared<Block>("a", "");
121+
m_compareBlock = block;
122+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
123+
EXPECT_EQ(compiler->block(), m_compareBlock);
124+
CompilerValue arg1(Compiler::StaticType::Unknown);
125+
CompilerValue arg2(Compiler::StaticType::Unknown);
126+
Compiler::ArgTypes argTypes = { Compiler::StaticType::Number, Compiler::StaticType::Bool };
127+
Compiler::Args args = { &arg1, &arg2 };
128+
EXPECT_CALL(*m_builder, addFunctionCallWithCtx("test1", Compiler::StaticType::Void, argTypes, args));
129+
compiler->addFunctionCallWithCtx("test1", Compiler::StaticType::Void, argTypes, args);
130+
131+
args = { &arg1 };
132+
argTypes = { Compiler::StaticType::String };
133+
EXPECT_CALL(*m_builder, addFunctionCallWithCtx("test2", Compiler::StaticType::Bool, argTypes, args));
134+
compiler->addFunctionCallWithCtx("test2", Compiler::StaticType::Bool, argTypes, args);
135+
136+
return nullptr;
137+
});
138+
139+
compile(compiler, block);
140+
}
141+
92142
TEST_F(CompilerTest, AddConstValue)
93143
{
94144
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)