|
libscratchcpp
A library for C++ based Scratch project players
|
The Compiler class provides API for compiling Scratch scripts. More...
#include <scratchcpp/compiler.h>
Public Types | |
| enum class | StaticType { Void = 0 , Number = 1 << 0 , Bool = 1 << 1 , String = 1 << 2 , Pointer = 1 << 3 , Unknown = Number | Bool | String } |
| enum class | CodeType { Script , Reporter , HatPredicate } |
| using | ArgTypes = std::vector<StaticType> |
| using | Args = std::vector<CompilerValue *> |
Static Public Member Functions | |
| static std::shared_ptr< CompilerContext > | createContext (IEngine *engine, Target *target) |
The Compiler class provides API for compiling Scratch scripts.
| using libscratchcpp::Compiler::Args = std::vector<CompilerValue *> |
| using libscratchcpp::Compiler::ArgTypes = std::vector<StaticType> |
|
strong |
|
strong |
| Compiler::Compiler | ( | CompilerContext * | ctx | ) |
Constructs Compiler using the given context.
Constructs Compiler using a new context for the given target.
|
delete |
| CompilerConstant * Compiler::addConstValue | ( | const Value & | value | ) |
Adds the given constant to the compiled code.
| CompilerValue * Compiler::addFunctionCall | ( | const std::string & | functionName, |
| StaticType | returnType = StaticType::Void, | ||
| const ArgTypes & | argTypes = {}, | ||
| const Args & | args = {} ) |
Adds a call to the given function.
For example: extern "C" bool some_block(double arg1, const StringPtr *arg2)
| CompilerValue * Compiler::addFunctionCallWithCtx | ( | const std::string & | functionName, |
| StaticType | returnType = StaticType::Void, | ||
| const ArgTypes & | argTypes = {}, | ||
| const Args & | args = {} ) |
Adds a call to the given function with an execution context parameter.
For example: extern "C" bool some_block(ExecutionContext *ctx, double arg1, const StringPtr *arg2)
| CompilerValue * Compiler::addInput | ( | const std::string & | name | ) |
Compiles the given input (resolved by name) and adds it to the compiled code.
| CompilerValue * Compiler::addInput | ( | Input * | input | ) |
Compiles the given input and adds it to the compiled code.
| CompilerValue * Compiler::addListContains | ( | List * | list, |
| CompilerValue * | item ) |
Adds the result of a list contains item check to the code.
| CompilerValue * Compiler::addListContents | ( | List * | list | ) |
Adds the string representation of the given list to the code.
| CompilerValue * Compiler::addListItem | ( | List * | list, |
| CompilerValue * | index ) |
Adds the item with index of the given list to the code.
| CompilerValue * Compiler::addListItemIndex | ( | List * | list, |
| CompilerValue * | item ) |
Adds the index of item of the given list to the code.
| CompilerValue * Compiler::addListSize | ( | List * | list | ) |
Adds the length of the given list to the code.
| CompilerValue * Compiler::addLocalVariableValue | ( | CompilerLocalVariable * | variable | ) |
Adds the value of the given local variable to the code.
| CompilerValue * Compiler::addLoopIndex | ( | ) |
Adds the index of the current repeat loop to the compiled code.
| CompilerValue * Compiler::addProcedureArgument | ( | const std::string & | name | ) |
Adds the procedure argument with the given name to the code.
| CompilerValue * Compiler::addStringChar | ( | CompilerValue * | string, |
| CompilerValue * | index ) |
Adds the string character with the given index to the compiled code.
| CompilerValue * Compiler::addStringLength | ( | CompilerValue * | string | ) |
Adds the length of the given string to the compiled code.
| CompilerValue * Compiler::addTargetFunctionCall | ( | const std::string & | functionName, |
| StaticType | returnType = StaticType::Void, | ||
| const ArgTypes & | argTypes = {}, | ||
| const Args & | args = {} ) |
Adds a call to the given function with a target parameter.
For example: extern "C" bool some_block(Target *target, double arg1, const StringPtr *arg2)
| CompilerValue * Compiler::addVariableValue | ( | Variable * | variable | ) |
Adds the value of the given variable to the code.
| void Compiler::beginElseBranch | ( | ) |
Starts the else branch of custom if statement.
| void Compiler::beginIfStatement | ( | CompilerValue * | cond | ) |
Starts a custom if statement.
| void Compiler::beginLoopCondition | ( | ) |
Begins a while/until loop condition.
| void Compiler::beginRepeatUntilLoop | ( | CompilerValue * | cond | ) |
Begins a custom repeat until loop.
| void Compiler::beginWhileLoop | ( | CompilerValue * | cond | ) |
Begins a custom while loop.
| Block * Compiler::block | ( | ) | const |
Returns currently compiled block.
| std::shared_ptr< ExecutableCode > Compiler::compile | ( | Block * | startBlock, |
| CodeType | codeType = CodeType::Script ) |
Compiles the script starting with the given block.
| CompilerValue * Compiler::createAbs | ( | CompilerValue * | num | ) |
Creates an abs operation.
| CompilerValue * Compiler::createAcos | ( | CompilerValue * | num | ) |
Creates an acos operation.
| CompilerValue * Compiler::createAdd | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates an add instruction.
| CompilerValue * Compiler::createAnd | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates an AND operation.
| CompilerValue * Compiler::createAsin | ( | CompilerValue * | num | ) |
Creates an asin operation.
| CompilerValue * Compiler::createAtan | ( | CompilerValue * | num | ) |
Creates an atan operation.
| CompilerValue * Compiler::createCeil | ( | CompilerValue * | num | ) |
Creates a ceiling operation.
| CompilerValue * Compiler::createCmpEQ | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates an equality comparison instruction.
| CompilerValue * Compiler::createCmpGT | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates a greater than comparison instruction.
| CompilerValue * Compiler::createCmpLT | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates a lower than comparison instruction.
|
static |
Creates a compiler context for the given target.
| CompilerValue * Compiler::createCos | ( | CompilerValue * | num | ) |
Creates a cos operation.
| CompilerValue * Compiler::createDiv | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates a divide instruction.
| CompilerValue * Compiler::createExp | ( | CompilerValue * | num | ) |
Creates an e^x operation.
| CompilerValue * Compiler::createExp10 | ( | CompilerValue * | num | ) |
Creates a 10^x operation.
| CompilerValue * Compiler::createFloor | ( | CompilerValue * | num | ) |
Creates a floor operation.
| void Compiler::createListAppend | ( | List * | list, |
| CompilerValue * | item ) |
Creates a list append operation.
| void Compiler::createListClear | ( | List * | list | ) |
Creates a clear list operation.
| void Compiler::createListInsert | ( | List * | list, |
| CompilerValue * | index, | ||
| CompilerValue * | item ) |
Creates a list insert operation.
| void Compiler::createListRemove | ( | List * | list, |
| CompilerValue * | index ) |
Creates a remove item from list operation.
| void Compiler::createListReplace | ( | List * | list, |
| CompilerValue * | index, | ||
| CompilerValue * | item ) |
Creates a list replace operation.
| CompilerValue * Compiler::createLn | ( | CompilerValue * | num | ) |
Creates an ln operation.
| CompilerLocalVariable * Compiler::createLocalVariable | ( | Compiler::StaticType | type | ) |
Creates a local variable with the given type.
| void Compiler::createLocalVariableWrite | ( | CompilerLocalVariable * | variable, |
| CompilerValue * | value ) |
Creates a local variable write operation.
| CompilerValue * Compiler::createLog10 | ( | CompilerValue * | num | ) |
Creates a log10 operation.
| CompilerValue * Compiler::createMod | ( | CompilerValue * | num1, |
| CompilerValue * | num2 ) |
Creates a remainder operation.
| CompilerValue * Compiler::createMul | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates a multiply instruction.
| CompilerValue * Compiler::createNot | ( | CompilerValue * | operand | ) |
Creates a NOT operation.
| CompilerValue * Compiler::createOr | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates an OR operation.
| void Compiler::createProcedureCall | ( | BlockPrototype * | prototype, |
| const Compiler::Args & | args ) |
Creates a call to the procedure with the given prototype.
| CompilerValue * Compiler::createRandom | ( | CompilerValue * | from, |
| CompilerValue * | to ) |
Creates a random instruction (Scratch behavior).
| CompilerValue * Compiler::createRandomInt | ( | CompilerValue * | from, |
| CompilerValue * | to ) |
Creates a random integer instruction.
| CompilerValue * Compiler::createRound | ( | CompilerValue * | num | ) |
Creates a round operation.
| CompilerValue * Compiler::createSelect | ( | CompilerValue * | cond, |
| CompilerValue * | trueValue, | ||
| CompilerValue * | falseValue, | ||
| Compiler::StaticType | valueType ) |
Creates a select instruction (ternary operator).
| CompilerValue * Compiler::createSin | ( | CompilerValue * | num | ) |
Creates a sin operation.
| CompilerValue * Compiler::createSqrt | ( | CompilerValue * | num | ) |
Creates a square root operation.
| void Compiler::createStop | ( | ) |
Creates a stop script instruction.
| void Compiler::createStopWithoutSync | ( | ) |
Creates a stop script without synchronization instruction.
Use this if synchronization is not possible at the stop point.
| CompilerValue * Compiler::createStrCmpEQ | ( | CompilerValue * | string1, |
| CompilerValue * | string2, | ||
| bool | caseSensitive = false ) |
Creates a string equality comparison (explicitly casts operands to string).
| CompilerValue * Compiler::createStringConcat | ( | CompilerValue * | string1, |
| CompilerValue * | string2 ) |
Creates a string concatenation operation.
| CompilerValue * Compiler::createSub | ( | CompilerValue * | operand1, |
| CompilerValue * | operand2 ) |
Creates a subtract instruction.
| CompilerValue * Compiler::createTan | ( | CompilerValue * | num | ) |
Creates a tan operation.
| void Compiler::createVariableWrite | ( | Variable * | variable, |
| CompilerValue * | value ) |
Creates a variable write operation.
| void Compiler::createYield | ( | ) |
Creates a suspend instruction.
| void Compiler::endIf | ( | ) |
Ends custom if statement.
| void Compiler::endLoop | ( | ) |
Ends custom loop.
| IEngine * Compiler::engine | ( | ) | const |
Returns the Engine of the project.
| Field * Compiler::field | ( | const std::string & | name | ) | const |
Convenience method which returns the field with the given name.
| Input * Compiler::input | ( | const std::string & | name | ) | const |
Convenience method which returns the field with the given name.
| void Compiler::moveToIf | ( | CompilerValue * | cond, |
| Block * | substack ) |
Jumps to the given if substack.
| void Compiler::moveToIfElse | ( | CompilerValue * | cond, |
| Block * | substack1, | ||
| Block * | substack2 ) |
Jumps to the given if/else substack. The second substack is used for the else branch.
| void Compiler::moveToRepeatLoop | ( | CompilerValue * | count, |
| Block * | substack ) |
Jumps to the given repeat loop substack.
| void Compiler::moveToRepeatUntilLoop | ( | CompilerValue * | cond, |
| Block * | substack ) |
Jumps to the given until loop substack.
| void Compiler::moveToWhileLoop | ( | CompilerValue * | cond, |
| Block * | substack ) |
Jumps to the given while loop substack.
| void Compiler::preoptimize | ( | ) |
Optimizes all compiled scripts before they're called for the first time.
| const std::unordered_set< std::string > & Compiler::unsupportedBlocks | ( | ) | const |
Returns unsupported block opcodes which were found when compiling.
| void Compiler::warp | ( | ) |
Makes current script run without screen refresh.