Skip to content

Commit e8b45aa

Browse files
authored
Merge pull request #598 from scratchcpp/llvm_variables
LLVM: Implement variables
2 parents be0c951 + 10b55f5 commit e8b45aa

20 files changed

+437
-32
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class LIBSCRATCHCPP_EXPORT Compiler
7878
void createExp();
7979
void createExp10();
8080

81+
void createVariableWrite(Variable *variable);
82+
8183
void moveToIf(std::shared_ptr<Block> substack);
8284
void moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
8385
void moveToRepeatLoop(std::shared_ptr<Block> substack);

include/scratchcpp/target.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace libscratchcpp
1212
{
1313

1414
class Variable;
15+
struct ValueData;
1516
class List;
1617
class Block;
1718
class Comment;
@@ -28,7 +29,7 @@ class LIBSCRATCHCPP_EXPORT Target : public Drawable
2829
public:
2930
Target();
3031
Target(const Target &) = delete;
31-
virtual ~Target() { }
32+
virtual ~Target();
3233

3334
bool isTarget() const override final;
3435

@@ -44,6 +45,8 @@ class LIBSCRATCHCPP_EXPORT Target : public Drawable
4445
int findVariable(const std::string &variableName) const;
4546
int findVariableById(const std::string &id) const;
4647

48+
ValueData **variableData();
49+
4750
const std::vector<std::shared_ptr<List>> &lists() const;
4851
int addList(std::shared_ptr<List> list);
4952
std::shared_ptr<List> listAt(int index) const;

src/dev/engine/compiler.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::shared_ptr<libscratchcpp::Block> Compiler::block() const
3838
/*! Compiles the script starting with the given block. */
3939
std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBlock)
4040
{
41-
impl->builder = impl->builderFactory->create(startBlock->id(), false);
41+
impl->builder = impl->builderFactory->create(impl->target, startBlock->id(), false);
4242
impl->substackTree.clear();
4343
impl->substackHit = false;
4444
impl->warp = false;
@@ -256,6 +256,12 @@ void Compiler::createExp10()
256256
impl->builder->createExp10();
257257
}
258258

259+
/*! Creates a variable write operation using the last value. */
260+
void Compiler::createVariableWrite(Variable *variable)
261+
{
262+
impl->builder->createVariableWrite(variable);
263+
}
264+
259265
/*! Jumps to the given if substack. */
260266
void Compiler::moveToIf(std::shared_ptr<Block> substack)
261267
{

src/dev/engine/internal/codebuilderfactory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ std::shared_ptr<CodeBuilderFactory> CodeBuilderFactory::instance()
1212
return m_instance;
1313
}
1414

15-
std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(const std::string &id, bool warp) const
15+
std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(Target *target, const std::string &id, bool warp) const
1616
{
17-
return std::make_shared<LLVMCodeBuilder>(id, warp);
17+
return std::make_shared<LLVMCodeBuilder>(target, id, warp);
1818
}

src/dev/engine/internal/codebuilderfactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CodeBuilderFactory : public ICodeBuilderFactory
1111
{
1212
public:
1313
static std::shared_ptr<CodeBuilderFactory> instance();
14-
std::shared_ptr<ICodeBuilder> create(const std::string &id, bool warp) const override;
14+
std::shared_ptr<ICodeBuilder> create(Target *target, const std::string &id, bool warp) const override;
1515

1616
private:
1717
static std::shared_ptr<CodeBuilderFactory> m_instance;

src/dev/engine/internal/icodebuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class ICodeBuilder
5454
virtual void createExp() = 0;
5555
virtual void createExp10() = 0;
5656

57+
virtual void createVariableWrite(Variable *variable) = 0;
58+
5759
virtual void beginIfStatement() = 0;
5860
virtual void beginElseBranch() = 0;
5961
virtual void endIf() = 0;

src/dev/engine/internal/icodebuilderfactory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ namespace libscratchcpp
88
{
99

1010
class ICodeBuilder;
11+
class Target;
1112

1213
class ICodeBuilderFactory
1314
{
1415
public:
1516
virtual ~ICodeBuilderFactory() { }
1617

17-
virtual std::shared_ptr<ICodeBuilder> create(const std::string &id, bool warp) const = 0;
18+
virtual std::shared_ptr<ICodeBuilder> create(Target *target, const std::string &id, bool warp) const = 0;
1819
};
1920

2021
} // namespace libscratchcpp

0 commit comments

Comments
 (0)