libscratchcpp
A library for C++ based Scratch project players
Loading...
Searching...
No Matches
compiler.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include <unordered_set>
6#include <vector>
7
8#include "global.h"
9#include "enum_bitmask.h"
10#include "spimpl.h"
11
12#define BLOCK_EXPORT extern "C" LIBSCRATCHCPP_EXPORT
13
14namespace libscratchcpp
15{
16
17class CompilerContext;
18class IEngine;
19class Target;
20class ExecutableCode;
21class CompilerValue;
22class CompilerConstant;
23class CompilerLocalVariable;
24class Variable;
25class List;
26class Input;
27class Field;
28class BlockPrototype;
29class CompilerPrivate;
30
33{
34 public:
35 enum class StaticType
36 {
37 Void = 0,
38 Number = 1 << 0,
39 Bool = 1 << 1,
40 String = 1 << 2,
41 Pointer = 1 << 3,
43 };
44
51
52 using ArgTypes = std::vector<StaticType>;
53 using Args = std::vector<CompilerValue *>;
54
57 Compiler(const Compiler &) = delete;
58
59 IEngine *engine() const;
60 Target *target() const;
61 Block *block() const;
62
63 std::shared_ptr<ExecutableCode> compile(Block *startBlock, CodeType codeType = CodeType::Script);
64 void preoptimize();
65
66 CompilerValue *addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
67 CompilerValue *addTargetFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
68 CompilerValue *addFunctionCallWithCtx(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
69 CompilerConstant *addConstValue(const Value &value);
70 CompilerValue *addStringChar(CompilerValue *string, CompilerValue *index);
71 CompilerValue *addStringLength(CompilerValue *string);
72 CompilerValue *addLoopIndex();
73 CompilerValue *addLocalVariableValue(CompilerLocalVariable *variable);
74 CompilerValue *addVariableValue(Variable *variable);
75 CompilerValue *addListContents(List *list);
76 CompilerValue *addListItem(List *list, CompilerValue *index);
77 CompilerValue *addListItemIndex(List *list, CompilerValue *item);
78 CompilerValue *addListContains(List *list, CompilerValue *item);
79 CompilerValue *addListSize(List *list);
80 CompilerValue *addProcedureArgument(const std::string &name);
81
82 CompilerValue *addInput(const std::string &name);
83 CompilerValue *addInput(Input *input);
84
85 CompilerValue *createAdd(CompilerValue *operand1, CompilerValue *operand2);
86 CompilerValue *createSub(CompilerValue *operand1, CompilerValue *operand2);
87 CompilerValue *createMul(CompilerValue *operand1, CompilerValue *operand2);
88 CompilerValue *createDiv(CompilerValue *operand1, CompilerValue *operand2);
89
90 CompilerValue *createRandom(CompilerValue *from, CompilerValue *to);
91 CompilerValue *createRandomInt(CompilerValue *from, CompilerValue *to);
92
93 CompilerValue *createCmpEQ(CompilerValue *operand1, CompilerValue *operand2);
94 CompilerValue *createCmpGT(CompilerValue *operand1, CompilerValue *operand2);
95 CompilerValue *createCmpLT(CompilerValue *operand1, CompilerValue *operand2);
96
97 CompilerValue *createStrCmpEQ(CompilerValue *string1, CompilerValue *string2, bool caseSensitive = false);
98
99 CompilerValue *createAnd(CompilerValue *operand1, CompilerValue *operand2);
100 CompilerValue *createOr(CompilerValue *operand1, CompilerValue *operand2);
101 CompilerValue *createNot(CompilerValue *operand);
102
103 CompilerValue *createMod(CompilerValue *num1, CompilerValue *num2);
104 CompilerValue *createRound(CompilerValue *num);
105 CompilerValue *createAbs(CompilerValue *num);
106 CompilerValue *createFloor(CompilerValue *num);
107 CompilerValue *createCeil(CompilerValue *num);
108 CompilerValue *createSqrt(CompilerValue *num);
109 CompilerValue *createSin(CompilerValue *num);
110 CompilerValue *createCos(CompilerValue *num);
111 CompilerValue *createTan(CompilerValue *num);
112 CompilerValue *createAsin(CompilerValue *num);
113 CompilerValue *createAcos(CompilerValue *num);
114 CompilerValue *createAtan(CompilerValue *num);
115 CompilerValue *createLn(CompilerValue *num);
116 CompilerValue *createLog10(CompilerValue *num);
117 CompilerValue *createExp(CompilerValue *num);
118 CompilerValue *createExp10(CompilerValue *num);
119
120 CompilerValue *createStringConcat(CompilerValue *string1, CompilerValue *string2);
121
122 CompilerValue *createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, Compiler::StaticType valueType);
123
124 CompilerLocalVariable *createLocalVariable(Compiler::StaticType type);
125 void createLocalVariableWrite(CompilerLocalVariable *variable, CompilerValue *value);
126
127 void createVariableWrite(Variable *variable, CompilerValue *value);
128
129 void createListClear(List *list);
130 void createListRemove(List *list, CompilerValue *index);
131 void createListAppend(List *list, CompilerValue *item);
132 void createListInsert(List *list, CompilerValue *index, CompilerValue *item);
133 void createListReplace(List *list, CompilerValue *index, CompilerValue *item);
134
135 void beginIfStatement(CompilerValue *cond);
136 void beginElseBranch();
137 void endIf();
138
139 void beginWhileLoop(CompilerValue *cond);
140 void beginRepeatUntilLoop(CompilerValue *cond);
141 void beginLoopCondition();
142 void endLoop();
143
144 void moveToIf(CompilerValue *cond, Block *substack);
145 void moveToIfElse(CompilerValue *cond, Block *substack1, Block *substack2);
146 void moveToRepeatLoop(CompilerValue *count, Block *substack);
147 void moveToWhileLoop(CompilerValue *cond, Block *substack);
148 void moveToRepeatUntilLoop(CompilerValue *cond, Block *substack);
149 void warp();
150
151 void createYield();
152 void createStop();
153 void createStopWithoutSync();
154
155 void createProcedureCall(BlockPrototype *prototype, const Compiler::Args &args);
156
157 Input *input(const std::string &name) const;
158 Field *field(const std::string &name) const;
159
160 const std::unordered_set<std::string> &unsupportedBlocks() const;
161
162 static std::shared_ptr<CompilerContext> createContext(IEngine *engine, Target *target);
163
164 private:
165 spimpl::unique_impl_ptr<CompilerPrivate> impl;
166};
167
169
170} // namespace libscratchcpp
The Block class represents a Scratch block.
Definition block.h:24
The CompilerContext represents a context for a specific target which is used with the Compiler class.
Definition compilercontext.h:17
The CompilerValue class represents a local value in compiled code.
Definition compilervalue.h:14
Target * target() const
Definition compiler.cpp:35
Compiler(CompilerContext *ctx)
Definition compiler.cpp:17
CodeType
Definition compiler.h:46
@ HatPredicate
Definition compiler.h:49
@ Reporter
Definition compiler.h:48
@ Script
Definition compiler.h:47
std::vector< CompilerValue * > Args
Definition compiler.h:53
CompilerValue * addFunctionCall(const std::string &functionName, StaticType returnType=StaticType::Void, const ArgTypes &argTypes={}, const Args &args={})
Definition compiler.cpp:141
std::shared_ptr< ExecutableCode > compile(Block *startBlock, CodeType codeType=CodeType::Script)
Definition compiler.cpp:47
Block * block() const
Definition compiler.cpp:41
std::vector< StaticType > ArgTypes
Definition compiler.h:52
StaticType
Definition compiler.h:36
@ String
Definition compiler.h:40
@ Pointer
Definition compiler.h:41
@ Void
Definition compiler.h:37
@ Unknown
Definition compiler.h:42
@ Number
Definition compiler.h:38
@ Bool
Definition compiler.h:39
void preoptimize()
Definition compiler.cpp:132
Compiler(const Compiler &)=delete
IEngine * engine() const
Definition compiler.cpp:29
The IEngine interface provides an API for running Scratch projects.
Definition iengine.h:41
The Target class is the Stage or a Sprite.
Definition target.h:28
#define LIBSCRATCHCPP_EXPORT
Definition global.h:17
The main namespace of the library.
Definition asset.h:10
constexpr bool enable_enum_bitmask(Compiler::StaticType)