libscratchcpp
A library for C++ based Scratch project players
Loading...
Searching...
No Matches
libscratchcpp::Compiler Class Reference

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 *>
 

Public Member Functions

 Compiler (CompilerContext *ctx)
 
 Compiler (IEngine *engine, Target *target)
 
 Compiler (const Compiler &)=delete
 
IEngineengine () const
 
Targettarget () const
 
Blockblock () const
 
std::shared_ptr< ExecutableCodecompile (Block *startBlock, CodeType codeType=CodeType::Script)
 
void preoptimize ()
 
CompilerValueaddFunctionCall (const std::string &functionName, StaticType returnType=StaticType::Void, const ArgTypes &argTypes={}, const Args &args={})
 
CompilerValueaddTargetFunctionCall (const std::string &functionName, StaticType returnType=StaticType::Void, const ArgTypes &argTypes={}, const Args &args={})
 
CompilerValueaddFunctionCallWithCtx (const std::string &functionName, StaticType returnType=StaticType::Void, const ArgTypes &argTypes={}, const Args &args={})
 
CompilerConstantaddConstValue (const Value &value)
 
CompilerValueaddStringChar (CompilerValue *string, CompilerValue *index)
 
CompilerValueaddStringLength (CompilerValue *string)
 
CompilerValueaddLoopIndex ()
 
CompilerValueaddLocalVariableValue (CompilerLocalVariable *variable)
 
CompilerValueaddVariableValue (Variable *variable)
 
CompilerValueaddListContents (List *list)
 
CompilerValueaddListItem (List *list, CompilerValue *index)
 
CompilerValueaddListItemIndex (List *list, CompilerValue *item)
 
CompilerValueaddListContains (List *list, CompilerValue *item)
 
CompilerValueaddListSize (List *list)
 
CompilerValueaddProcedureArgument (const std::string &name)
 
CompilerValueaddInput (const std::string &name)
 
CompilerValueaddInput (Input *input)
 
CompilerValuecreateAdd (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateSub (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateMul (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateDiv (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateRandom (CompilerValue *from, CompilerValue *to)
 
CompilerValuecreateRandomInt (CompilerValue *from, CompilerValue *to)
 
CompilerValuecreateCmpEQ (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateCmpGT (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateCmpLT (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateStrCmpEQ (CompilerValue *string1, CompilerValue *string2, bool caseSensitive=false)
 
CompilerValuecreateAnd (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateOr (CompilerValue *operand1, CompilerValue *operand2)
 
CompilerValuecreateNot (CompilerValue *operand)
 
CompilerValuecreateMod (CompilerValue *num1, CompilerValue *num2)
 
CompilerValuecreateRound (CompilerValue *num)
 
CompilerValuecreateAbs (CompilerValue *num)
 
CompilerValuecreateFloor (CompilerValue *num)
 
CompilerValuecreateCeil (CompilerValue *num)
 
CompilerValuecreateSqrt (CompilerValue *num)
 
CompilerValuecreateSin (CompilerValue *num)
 
CompilerValuecreateCos (CompilerValue *num)
 
CompilerValuecreateTan (CompilerValue *num)
 
CompilerValuecreateAsin (CompilerValue *num)
 
CompilerValuecreateAcos (CompilerValue *num)
 
CompilerValuecreateAtan (CompilerValue *num)
 
CompilerValuecreateLn (CompilerValue *num)
 
CompilerValuecreateLog10 (CompilerValue *num)
 
CompilerValuecreateExp (CompilerValue *num)
 
CompilerValuecreateExp10 (CompilerValue *num)
 
CompilerValuecreateStringConcat (CompilerValue *string1, CompilerValue *string2)
 
CompilerValuecreateSelect (CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, Compiler::StaticType valueType)
 
CompilerLocalVariablecreateLocalVariable (Compiler::StaticType type)
 
void createLocalVariableWrite (CompilerLocalVariable *variable, CompilerValue *value)
 
void createVariableWrite (Variable *variable, CompilerValue *value)
 
void createListClear (List *list)
 
void createListRemove (List *list, CompilerValue *index)
 
void createListAppend (List *list, CompilerValue *item)
 
void createListInsert (List *list, CompilerValue *index, CompilerValue *item)
 
void createListReplace (List *list, CompilerValue *index, CompilerValue *item)
 
void beginIfStatement (CompilerValue *cond)
 
void beginElseBranch ()
 
void endIf ()
 
void beginWhileLoop (CompilerValue *cond)
 
void beginRepeatUntilLoop (CompilerValue *cond)
 
void beginLoopCondition ()
 
void endLoop ()
 
void moveToIf (CompilerValue *cond, Block *substack)
 
void moveToIfElse (CompilerValue *cond, Block *substack1, Block *substack2)
 
void moveToRepeatLoop (CompilerValue *count, Block *substack)
 
void moveToWhileLoop (CompilerValue *cond, Block *substack)
 
void moveToRepeatUntilLoop (CompilerValue *cond, Block *substack)
 
void warp ()
 
void createYield ()
 
void createStop ()
 
void createStopWithoutSync ()
 
void createProcedureCall (BlockPrototype *prototype, const Compiler::Args &args)
 
Inputinput (const std::string &name) const
 
Fieldfield (const std::string &name) const
 
const std::unordered_set< std::string > & unsupportedBlocks () const
 

Static Public Member Functions

static std::shared_ptr< CompilerContextcreateContext (IEngine *engine, Target *target)
 

Detailed Description

The Compiler class provides API for compiling Scratch scripts.

Member Typedef Documentation

◆ Args

◆ ArgTypes

Member Enumeration Documentation

◆ CodeType

Enumerator
Script 
Reporter 
HatPredicate 

◆ StaticType

Enumerator
Void 
Number 
Bool 
String 
Pointer 
Unknown 

Constructor & Destructor Documentation

◆ Compiler() [1/3]

Compiler::Compiler ( CompilerContext * ctx)

Constructs Compiler using the given context.

◆ Compiler() [2/3]

Compiler::Compiler ( IEngine * engine,
Target * target )

Constructs Compiler using a new context for the given target.

◆ Compiler() [3/3]

libscratchcpp::Compiler::Compiler ( const Compiler & )
delete

Member Function Documentation

◆ addConstValue()

CompilerConstant * Compiler::addConstValue ( const Value & value)

Adds the given constant to the compiled code.

◆ addFunctionCall()

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)

◆ addFunctionCallWithCtx()

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)

◆ addInput() [1/2]

CompilerValue * Compiler::addInput ( const std::string & name)

Compiles the given input (resolved by name) and adds it to the compiled code.

◆ addInput() [2/2]

CompilerValue * Compiler::addInput ( Input * input)

Compiles the given input and adds it to the compiled code.

◆ addListContains()

CompilerValue * Compiler::addListContains ( List * list,
CompilerValue * item )

Adds the result of a list contains item check to the code.

◆ addListContents()

CompilerValue * Compiler::addListContents ( List * list)

Adds the string representation of the given list to the code.

◆ addListItem()

CompilerValue * Compiler::addListItem ( List * list,
CompilerValue * index )

Adds the item with index of the given list to the code.

◆ addListItemIndex()

CompilerValue * Compiler::addListItemIndex ( List * list,
CompilerValue * item )

Adds the index of item of the given list to the code.

◆ addListSize()

CompilerValue * Compiler::addListSize ( List * list)

Adds the length of the given list to the code.

◆ addLocalVariableValue()

CompilerValue * Compiler::addLocalVariableValue ( CompilerLocalVariable * variable)

Adds the value of the given local variable to the code.

◆ addLoopIndex()

CompilerValue * Compiler::addLoopIndex ( )

Adds the index of the current repeat loop to the compiled code.

◆ addProcedureArgument()

CompilerValue * Compiler::addProcedureArgument ( const std::string & name)

Adds the procedure argument with the given name to the code.

◆ addStringChar()

CompilerValue * Compiler::addStringChar ( CompilerValue * string,
CompilerValue * index )

Adds the string character with the given index to the compiled code.

◆ addStringLength()

CompilerValue * Compiler::addStringLength ( CompilerValue * string)

Adds the length of the given string to the compiled code.

◆ addTargetFunctionCall()

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)

◆ addVariableValue()

CompilerValue * Compiler::addVariableValue ( Variable * variable)

Adds the value of the given variable to the code.

◆ beginElseBranch()

void Compiler::beginElseBranch ( )

Starts the else branch of custom if statement.

◆ beginIfStatement()

void Compiler::beginIfStatement ( CompilerValue * cond)

Starts a custom if statement.

Note
The if statement must be terminated using endIf() after compiling your block.

◆ beginLoopCondition()

void Compiler::beginLoopCondition ( )

Begins a while/until loop condition.

◆ beginRepeatUntilLoop()

void Compiler::beginRepeatUntilLoop ( CompilerValue * cond)

Begins a custom repeat until loop.

Note
The loop must be terminated with endLoop() after compiling your block.

◆ beginWhileLoop()

void Compiler::beginWhileLoop ( CompilerValue * cond)

Begins a custom while loop.

Note
The loop must be terminated with endLoop() after compiling your block.

◆ block()

Block * Compiler::block ( ) const

Returns currently compiled block.

◆ compile()

std::shared_ptr< ExecutableCode > Compiler::compile ( Block * startBlock,
CodeType codeType = CodeType::Script )

Compiles the script starting with the given block.

◆ createAbs()

CompilerValue * Compiler::createAbs ( CompilerValue * num)

Creates an abs operation.

◆ createAcos()

CompilerValue * Compiler::createAcos ( CompilerValue * num)

Creates an acos operation.

◆ createAdd()

CompilerValue * Compiler::createAdd ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates an add instruction.

◆ createAnd()

CompilerValue * Compiler::createAnd ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates an AND operation.

◆ createAsin()

CompilerValue * Compiler::createAsin ( CompilerValue * num)

Creates an asin operation.

◆ createAtan()

CompilerValue * Compiler::createAtan ( CompilerValue * num)

Creates an atan operation.

◆ createCeil()

CompilerValue * Compiler::createCeil ( CompilerValue * num)

Creates a ceiling operation.

◆ createCmpEQ()

CompilerValue * Compiler::createCmpEQ ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates an equality comparison instruction.

◆ createCmpGT()

CompilerValue * Compiler::createCmpGT ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates a greater than comparison instruction.

◆ createCmpLT()

CompilerValue * Compiler::createCmpLT ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates a lower than comparison instruction.

◆ createContext()

std::shared_ptr< CompilerContext > Compiler::createContext ( IEngine * engine,
Target * target )
static

Creates a compiler context for the given target.

◆ createCos()

CompilerValue * Compiler::createCos ( CompilerValue * num)

Creates a cos operation.

◆ createDiv()

CompilerValue * Compiler::createDiv ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates a divide instruction.

◆ createExp()

CompilerValue * Compiler::createExp ( CompilerValue * num)

Creates an e^x operation.

◆ createExp10()

CompilerValue * Compiler::createExp10 ( CompilerValue * num)

Creates a 10^x operation.

◆ createFloor()

CompilerValue * Compiler::createFloor ( CompilerValue * num)

Creates a floor operation.

◆ createListAppend()

void Compiler::createListAppend ( List * list,
CompilerValue * item )

Creates a list append operation.

◆ createListClear()

void Compiler::createListClear ( List * list)

Creates a clear list operation.

◆ createListInsert()

void Compiler::createListInsert ( List * list,
CompilerValue * index,
CompilerValue * item )

Creates a list insert operation.

◆ createListRemove()

void Compiler::createListRemove ( List * list,
CompilerValue * index )

Creates a remove item from list operation.

Note
The index starts with 0 and is cast to number, special strings like "last" are not handled.

◆ createListReplace()

void Compiler::createListReplace ( List * list,
CompilerValue * index,
CompilerValue * item )

Creates a list replace operation.

◆ createLn()

CompilerValue * Compiler::createLn ( CompilerValue * num)

Creates an ln operation.

◆ createLocalVariable()

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

Creates a local variable with the given type.

◆ createLocalVariableWrite()

void Compiler::createLocalVariableWrite ( CompilerLocalVariable * variable,
CompilerValue * value )

Creates a local variable write operation.

◆ createLog10()

CompilerValue * Compiler::createLog10 ( CompilerValue * num)

Creates a log10 operation.

◆ createMod()

CompilerValue * Compiler::createMod ( CompilerValue * num1,
CompilerValue * num2 )

Creates a remainder operation.

◆ createMul()

CompilerValue * Compiler::createMul ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates a multiply instruction.

◆ createNot()

CompilerValue * Compiler::createNot ( CompilerValue * operand)

Creates a NOT operation.

◆ createOr()

CompilerValue * Compiler::createOr ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates an OR operation.

◆ createProcedureCall()

void Compiler::createProcedureCall ( BlockPrototype * prototype,
const Compiler::Args & args )

Creates a call to the procedure with the given prototype.

◆ createRandom()

CompilerValue * Compiler::createRandom ( CompilerValue * from,
CompilerValue * to )

Creates a random instruction (Scratch behavior).

◆ createRandomInt()

CompilerValue * Compiler::createRandomInt ( CompilerValue * from,
CompilerValue * to )

Creates a random integer instruction.

Note
Infinity or NaN results in undefined behavior.

◆ createRound()

CompilerValue * Compiler::createRound ( CompilerValue * num)

Creates a round operation.

◆ createSelect()

CompilerValue * Compiler::createSelect ( CompilerValue * cond,
CompilerValue * trueValue,
CompilerValue * falseValue,
Compiler::StaticType valueType )

Creates a select instruction (ternary operator).

◆ createSin()

CompilerValue * Compiler::createSin ( CompilerValue * num)

Creates a sin operation.

◆ createSqrt()

CompilerValue * Compiler::createSqrt ( CompilerValue * num)

Creates a square root operation.

◆ createStop()

void Compiler::createStop ( )

Creates a stop script instruction.

◆ createStopWithoutSync()

void Compiler::createStopWithoutSync ( )

Creates a stop script without synchronization instruction.
Use this if synchronization is not possible at the stop point.

Note
Only use this when everything is synchronized, e. g. after a function call.

◆ createStrCmpEQ()

CompilerValue * Compiler::createStrCmpEQ ( CompilerValue * string1,
CompilerValue * string2,
bool caseSensitive = false )

Creates a string equality comparison (explicitly casts operands to string).

◆ createStringConcat()

CompilerValue * Compiler::createStringConcat ( CompilerValue * string1,
CompilerValue * string2 )

Creates a string concatenation operation.

◆ createSub()

CompilerValue * Compiler::createSub ( CompilerValue * operand1,
CompilerValue * operand2 )

Creates a subtract instruction.

◆ createTan()

CompilerValue * Compiler::createTan ( CompilerValue * num)

Creates a tan operation.

◆ createVariableWrite()

void Compiler::createVariableWrite ( Variable * variable,
CompilerValue * value )

Creates a variable write operation.

◆ createYield()

void Compiler::createYield ( )

Creates a suspend instruction.

◆ endIf()

void Compiler::endIf ( )

Ends custom if statement.

◆ endLoop()

void Compiler::endLoop ( )

Ends custom loop.

◆ engine()

IEngine * Compiler::engine ( ) const

Returns the Engine of the project.

◆ field()

Field * Compiler::field ( const std::string & name) const

Convenience method which returns the field with the given name.

◆ input()

Input * Compiler::input ( const std::string & name) const

Convenience method which returns the field with the given name.

◆ moveToIf()

void Compiler::moveToIf ( CompilerValue * cond,
Block * substack )

Jumps to the given if substack.

◆ moveToIfElse()

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.

◆ moveToRepeatLoop()

void Compiler::moveToRepeatLoop ( CompilerValue * count,
Block * substack )

Jumps to the given repeat loop substack.

◆ moveToRepeatUntilLoop()

void Compiler::moveToRepeatUntilLoop ( CompilerValue * cond,
Block * substack )

Jumps to the given until loop substack.

◆ moveToWhileLoop()

void Compiler::moveToWhileLoop ( CompilerValue * cond,
Block * substack )

Jumps to the given while loop substack.

◆ preoptimize()

void Compiler::preoptimize ( )

Optimizes all compiled scripts before they're called for the first time.

Note
Call this only after compiling all scripts.

◆ target()

Target * Compiler::target ( ) const

Returns the Target of this compiler.

◆ unsupportedBlocks()

const std::unordered_set< std::string > & Compiler::unsupportedBlocks ( ) const

Returns unsupported block opcodes which were found when compiling.

◆ warp()

void Compiler::warp ( )

Makes current script run without screen refresh.


The documentation for this class was generated from the following files: