Blockly. Generator
Constructor
Generator
new Generator(name)
Class for a code generator that translates the blocks into a language.
Parameter |
|
---|---|
name |
string Language name of this generator. |
Properties
NAME_TYPE
Category to separate generated function names from variables and procedures.
COMMENT_WRAP
number
Maximum length for a comment before wrapping. Does not account for indenting level.
definitions_
Object
A dictionary of definitions to be printed before the code.
FUNCTION_NAME_PLACEHOLDER_
string
This is used as a placeholder in functions defined using Blockly.Generator.provideFunction_. It must not be legal code that could legitimately appear in a function definition (or comment), and it must not confuse the regular expression parser.
functionNames_
Object
A dictionary mapping desired function names in definitions_ to actual function names (to avoid collisions with user functions).
INDENT
string
The method of indenting. Defaults to two spaces, but language generators may override this to increase indent or change to tabs.
INFINITE_LOOP_TRAP
nullable string
Arbitrary code to inject into locations that risk causing infinite loops. Any instances of '%1' will be replaced by the block ID that failed. E.g. ' checkTimeout(%1);\n'
isInitialized
nullable boolean
Whether the init method has been called. Generators that set this flag to false after creation and true in init will cause blockToCode to emit a warning if the generator has not been initialized. If this flag is untouched, it will have no effect.
ORDER_OVERRIDES
non-null Array of non-null Array of number
List of outer-inner pairings that do NOT require parentheses.
RESERVED_WORDS_
string
Comma-separated list of reserved words.
STATEMENT_PREFIX
nullable string
Arbitrary code to inject before every statement. Any instances of '%1' will be replaced by the block ID of the statement. E.g. 'highlight(%1);\n'
STATEMENT_SUFFIX
nullable string
Arbitrary code to inject after every statement. Any instances of '%1' will be replaced by the block ID of the statement. E.g. 'highlight(%1);\n'
variableDB_
A database of variable names.
Methods
addLoopTrap
addLoopTrap(branch, block) returns string
Add an infinite loop trap to the contents of a loop. Add statement suffix at the start of the loop block (right after the loop statement executes), and a statement prefix to the end of the loop block (right before the loop statement executes).
Parameter |
|
---|---|
branch |
string Code for loop contents. |
block |
Enclosing block. Value must not be null. |
- Returns
-
string
Loop contents, with infinite loop trap added.
addReservedWords
addReservedWords(words)
Add one or more words to the list of reserved words for this language.
Parameter |
|
---|---|
words |
string Comma-separated list of words to add to the list. No spaces. Duplicates are ok. |
allNestedComments
allNestedComments(block) returns string
Recursively spider a tree of blocks, returning all their comments.
Parameter |
|
---|---|
block |
The block from which to start spidering. Value must not be null. |
- Returns
-
string
Concatenated list of comments.
blockToCode
blockToCode(block, opt_thisOnly) returns (string or Array)
Generate code for the specified block (and attached blocks). The generator must be initialized before calling this function.
Parameter |
|
---|---|
block |
The block to generate code for. |
opt_thisOnly |
Optional boolean True to generate code for only this statement. |
- Returns
-
(string or non-null Array)
For statement blocks, the generated code. For value blocks, an array containing the generated code and an operator order value. Returns '' if block is null.
finish
finish(code) returns string
Hook for code to run at end of code generation. Subclasses may override this, e.g. to prepend the generated code with the variable definitions.
Parameter |
|
---|---|
code |
string Generated code. |
- Returns
-
string
Completed code.
init
init(_workspace)
Hook for code to run before code generation starts. Subclasses may override this, e.g. to initialise the database of variable names.
Parameter |
|
---|---|
_workspace |
Workspace to generate code from. Value must not be null. |
injectId
injectId(msg, block) returns string
Inject a block ID into a message to replace '%1'. Used for STATEMENT_PREFIX, STATEMENT_SUFFIX, and INFINITE_LOOP_TRAP.
Parameter |
|
---|---|
msg |
string Code snippet with '%1'. |
block |
Block which has an ID. Value must not be null. |
- Returns
-
string
Code snippet with ID.
prefixLines
prefixLines(text, prefix) returns string
Prepend a common prefix onto each line of code. Intended for indenting code or adding comment markers.
Parameter |
|
---|---|
text |
string The lines of code. |
prefix |
string The common prefix. |
- Returns
-
string
The prefixed lines of code.
provideFunction_
provideFunction_(desiredName, code) returns string
Define a function to be included in the generated code. The first time this is called with a given desiredName, the code is saved and an actual name is generated. Subsequent calls with the same desiredName have no effect but have the same return value.
It is up to the caller to make sure the same desiredName is not used for different code values.
The code gets output when Blockly.Generator.finish() is called.
Parameter |
|
---|---|
desiredName |
string The desired name of the function (e.g., isPrime). |
code |
Array of string A list of statements. Use ' ' for indents. Value must not be null. |
- Returns
-
string
The actual name of the new function. This may differ from desiredName if the former has already been taken by the user.
scrub_
scrub_(_block, code, _opt_thisOnly) returns string
Common tasks for generating code from blocks. This is called from blockToCode and is called on every block, not just top level blocks. Subclasses may override this, e.g. to generate code for statements following the block, or to handle comments for the specified block and any connected value blocks.
Parameter |
|
---|---|
_block |
The current block. Value must not be null. |
code |
string The code created for this block. |
_opt_thisOnly |
Optional boolean True to generate code for only this statement. |
- Returns
-
string
Code with comments and subsequent blocks added.
scrubNakedValue
scrubNakedValue(line) returns string
Naked values are top-level blocks with outputs that aren't plugged into anything. Subclasses may override this, e.g. if their language does not allow naked values.
Parameter |
|
---|---|
line |
string Line of generated code. |
- Returns
-
string
Legal line of code.
statementToCode
statementToCode(block, name) returns string
Generate a code string representing the blocks attached to the named statement input. Indent the code. This is mainly used in generators. When trying to generate code to evaluate look at using workspaceToCode or blockToCode.
Parameter |
|
---|---|
block |
The block containing the input. Value must not be null. |
name |
string The name of the input. |
- Returns
-
string
Generated code or '' if no blocks are connected.
valueToCode
valueToCode(block, name, outerOrder) returns string
Generate code representing the specified value input.
Parameter |
|
---|---|
block |
The block containing the input. Value must not be null. |
name |
string The name of the input. |
outerOrder |
number The maximum binding strength (minimum order value) of any operators adjacent to "block". |
- Returns
-
string
Generated code or '' if no blocks are connected or the specified input does not exist.
workspaceToCode
workspaceToCode(workspace) returns string
Generate code for all blocks in the workspace to the specified language.
Parameter |
|
---|---|
workspace |
Workspace to generate code from. |
- Returns
-
string
Generated code.