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

static

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.

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'

ORDER_OVERRIDES

non-null Array of non-null Array of number

List of outer-inner pairings that do NOT require parentheses.

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'

Methods

addLoopTrap

addLoopTrap(branch, id) returns string

Add an infinite loop trap to the contents of a loop. If loop is empty, add a statment prefix for the loop block.

Parameter

branch

string

Code for loop contents.

id

string

ID of enclosing block.

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

Blockly.Block

The block from which to start spidering.

Value must not be null.

Returns

string Concatenated list of comments.

blockToCode

blockToCode(block) returns (string or Array)

Generate code for the specified block (and attached blocks).

Parameter

block

Blockly.Block

The block to generate code for.

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

Blockly.Workspace

Workspace to generate code from.

Value must not be null.

prefixLines

prefixLines(text, prefix) returns string

Prepend a common prefix onto each line of code.

Parameter

text

string

The lines of code.

prefix

string

The common prefix.

Returns

string The prefixed lines of code.

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 code representing the statement. Indent the code.

Parameter

block

Blockly.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

Blockly.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

Blockly.Workspace

Workspace to generate code from.

Returns

string Generated code.