Consider the following assembly of blocks.
alert(2 * 3 + 4);
This is obviously incorrect, since the multiplication operator rips apart the addition, grabbing the '3' for itself. One solution is to wrap the result of every value block in parentheses:
alert(((2) * ((3) + (4)));
This solution works perfectly, but it results in extremely messy code with large numbers of redundant parentheses. For some use cases this is not an issue. If human eyes are never going to see the generated code, then this is acceptable. However, Blockly is often used as an educational tool to introduce programming, a use case which relies on generating human-readable code.
ORDER_ATOMIC added to the start and
ORDER_NONE added to the end.
Applying these orders occurs in two places within each block's generator. The first place is when fetching generated code from a connected value block. In this case we pass the constant which represents the maximum binding strength of any operators adjacent to the sub-block's generated code. For example:
The second place is when returning generated code from a value block. In this case we pass the constant which represents the minimum binding strength of any operators in the block's generated code. For example:
If the order value returned by the sub-block is weaker than or equal to the
order value for the order argument of the parent block, then the
function will automatically wrap the contents of the sub-block's code in
parentheses to prevent it from being ripped apart by the parent block's code.
Below are some more examples. In each case the block has one connected sub-block which is represented as 'X' (the contents of 'X' is unknown and doesn't matter). The second column lists the strongest operator which might split 'X'. The third column lists the weakest operator in the final code for the block.
|Generated Code||Max strength against X||Min strength of block|
Math is hard
Still don't understand? No problem. Just use
ORDER_ATOMIC as the order on
every call to
valueToCode, and use
ORDER_NONE as the order for the final
return statement on every value block. The resulting code will be infested with
needless parentheses, but is guaranteed to be correct.