Skip to content

Commit bc2ec32

Browse files
committed
Compiler: Add methods for string instructions
1 parent a775a5b commit bc2ec32

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

include/scratchcpp/compiler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class LIBSCRATCHCPP_EXPORT Compiler
5656
CompilerValue *addTargetFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
5757
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
5858
CompilerConstant *addConstValue(const Value &value);
59+
CompilerValue *addStringChar(CompilerValue *string, CompilerValue *index);
60+
CompilerValue *addStringLength(CompilerValue *string);
5961
CompilerValue *addLoopIndex();
6062
CompilerValue *addLocalVariableValue(CompilerLocalVariable *variable);
6163
CompilerValue *addVariableValue(Variable *variable);
@@ -104,6 +106,8 @@ class LIBSCRATCHCPP_EXPORT Compiler
104106
CompilerValue *createExp(CompilerValue *num);
105107
CompilerValue *createExp10(CompilerValue *num);
106108

109+
CompilerValue *createStringConcat(CompilerValue *string1, CompilerValue *string2);
110+
107111
CompilerValue *createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, Compiler::StaticType valueType);
108112

109113
CompilerLocalVariable *createLocalVariable(Compiler::StaticType type);

src/engine/compiler.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ CompilerConstant *Compiler::addConstValue(const Value &value)
151151
return static_cast<CompilerConstant *>(impl->builder->addConstValue(value));
152152
}
153153

154+
/*! Adds the string character with the given index to the compiled code. */
155+
CompilerValue *Compiler::addStringChar(CompilerValue *string, CompilerValue *index)
156+
{
157+
return impl->builder->addStringChar(string, index);
158+
}
159+
160+
/*! Adds the length of the given string to the compiled code. */
161+
CompilerValue *Compiler::addStringLength(CompilerValue *string)
162+
{
163+
return impl->builder->addStringLength(string);
164+
}
165+
154166
/*! Adds the index of the current repeat loop to the compiled code. */
155167
CompilerValue *Compiler::addLoopIndex()
156168
{
@@ -444,6 +456,12 @@ CompilerValue *Compiler::createExp10(CompilerValue *num)
444456
return impl->builder->createExp10(num);
445457
}
446458

459+
/*! Creates a string concatenation operation. */
460+
CompilerValue *Compiler::createStringConcat(CompilerValue *string1, CompilerValue *string2)
461+
{
462+
return impl->builder->createStringConcat(string1, string2);
463+
}
464+
447465
/*! Creates a select instruction (ternary operator). */
448466
CompilerValue *Compiler::createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, StaticType valueType)
449467
{

test/compiler/compiler_test.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,49 @@ TEST_F(CompilerTest, AddConstValue)
173173
compile(m_compiler.get(), block);
174174
}
175175

176+
TEST_F(CompilerTest, AddStringChar)
177+
{
178+
179+
auto block = std::make_shared<Block>("", "");
180+
181+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
182+
CompilerValue arg1(Compiler::StaticType::Unknown);
183+
CompilerValue arg2(Compiler::StaticType::Unknown);
184+
CompilerValue ret(Compiler::StaticType::Unknown);
185+
186+
EXPECT_CALL(*m_builder, addStringChar(&arg1, &arg2)).WillOnce(Return(&ret));
187+
EXPECT_EQ(compiler->addStringChar(&arg1, &arg2), &ret);
188+
189+
EXPECT_CALL(*m_builder, addStringChar(&arg1, &arg2)).WillOnce(Return(nullptr));
190+
EXPECT_EQ(compiler->addStringChar(&arg1, &arg2), nullptr);
191+
192+
return nullptr;
193+
});
194+
195+
compile(m_compiler.get(), block);
196+
}
197+
198+
TEST_F(CompilerTest, AddStringLength)
199+
{
200+
201+
auto block = std::make_shared<Block>("", "");
202+
203+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
204+
CompilerValue arg(Compiler::StaticType::Unknown);
205+
CompilerValue ret(Compiler::StaticType::Unknown);
206+
207+
EXPECT_CALL(*m_builder, addStringLength(&arg)).WillOnce(Return(&ret));
208+
EXPECT_EQ(compiler->addStringLength(&arg), &ret);
209+
210+
EXPECT_CALL(*m_builder, addStringLength(&arg)).WillOnce(Return(nullptr));
211+
EXPECT_EQ(compiler->addStringLength(&arg), nullptr);
212+
213+
return nullptr;
214+
});
215+
216+
compile(m_compiler.get(), block);
217+
}
218+
176219
TEST_F(CompilerTest, AddLoopIndex)
177220
{
178221

@@ -957,6 +1000,24 @@ TEST_F(CompilerTest, CreateExp10)
9571000
compile(m_compiler.get(), block);
9581001
}
9591002

1003+
TEST_F(CompilerTest, CreateStringConcat)
1004+
{
1005+
1006+
auto block = std::make_shared<Block>("", "");
1007+
1008+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
1009+
CompilerValue arg1(Compiler::StaticType::Unknown);
1010+
CompilerValue arg2(Compiler::StaticType::Unknown);
1011+
CompilerValue ret(Compiler::StaticType::Unknown);
1012+
EXPECT_CALL(*m_builder, createStringConcat(&arg1, &arg2)).WillOnce(Return(&ret));
1013+
EXPECT_EQ(compiler->createStringConcat(&arg1, &arg2), &ret);
1014+
1015+
return nullptr;
1016+
});
1017+
1018+
compile(m_compiler.get(), block);
1019+
}
1020+
9601021
TEST_F(CompilerTest, CreateSelect)
9611022
{
9621023

0 commit comments

Comments
 (0)