Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BlockType.INLINE support #2 #220

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/irgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ class ScriptTreeGenerator {
const blockInfo = this.getBlockInfo(block.opcode);
if (blockInfo) {
const type = blockInfo.info.blockType;
if (type === BlockType.REPORTER || type === BlockType.BOOLEAN) {
if (type === BlockType.REPORTER || type === BlockType.BOOLEAN || type === BlockType.INLINE) {
return this.descendCompatLayer(block);
}
}
Expand Down Expand Up @@ -1416,7 +1416,7 @@ class ScriptTreeGenerator {
const blockInfo = this.getBlockInfo(block.opcode);
const blockType = (blockInfo && blockInfo.info && blockInfo.info.blockType) || BlockType.COMMAND;
const substacks = {};
if (blockType === BlockType.CONDITIONAL || blockType === BlockType.LOOP) {
if (blockType === BlockType.CONDITIONAL || blockType === BlockType.LOOP || blockType === BlockType.INLINE) {
for (const inputName in block.inputs) {
if (!inputName.startsWith('SUBSTACK')) continue;
const branchNum = inputName === 'SUBSTACK' ? 1 : +inputName.substring('SUBSTACK'.length);
Expand Down
32 changes: 31 additions & 1 deletion src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,26 @@ class JSGenerator {
return new TypedInput(`p${node.index}`, TYPE_UNKNOWN);

case 'compat':
if (node.blockType === BlockType.INLINE) {
const branchVariable = this.localVariables.next();
const returnVariable = this.localVariables.next();
let source = '(yield* (function*() {\n';
source += `let ${returnVariable} = undefined;\n`;
source += `const ${branchVariable} = createBranchInfo(false);\n`;
source += `${returnVariable} = (${this.generateCompatibilityLayerCall(node, false, branchVariable)});\n`;
source += `${branchVariable}.branch = globalState.blockUtility._startedBranch[0];\n`;
source += `switch (${branchVariable}.branch) {\n`;
for (const index in node.substacks) {
source += `case ${+index}: {\n`;
source += this.descendStackForSource(node.substacks[index], new Frame(false));
source += `break;\n`;
source += `}\n`; // close case
}
source += '}\n'; // close switch
source += `return ${returnVariable};\n`;
source += '})())'; // close function and yield
return new TypedInput(source, TYPE_UNKNOWN);
}
// Compatibility layer inputs never use flags.
return new TypedInput(`(${this.generateCompatibilityLayerCall(node, false)})`, TYPE_UNKNOWN);

Expand Down Expand Up @@ -768,7 +788,7 @@ class JSGenerator {
const blockType = node.blockType;
if (blockType === BlockType.COMMAND || blockType === BlockType.HAT) {
this.source += `${this.generateCompatibilityLayerCall(node, isLastInLoop)};\n`;
} else if (blockType === BlockType.CONDITIONAL || blockType === BlockType.LOOP) {
} else if (blockType === BlockType.CONDITIONAL || blockType === BlockType.LOOP || blockType === BlockType.INLINE) {
const branchVariable = this.localVariables.next();
this.source += `const ${branchVariable} = createBranchInfo(${blockType === BlockType.LOOP});\n`;
this.source += `while (${branchVariable}.branch = +(${this.generateCompatibilityLayerCall(node, false, branchVariable)})) {\n`;
Expand Down Expand Up @@ -1209,6 +1229,16 @@ class JSGenerator {
this.popFrame();
}

descendStackForSource (nodes, frame) {
// Wrapper for descendStack to get the source
const oldSource = this.source;
this.source = '';
this.descendStack(nodes, frame);
const stackSource = this.source;
this.source = oldSource;
return stackSource;
}

descendVariable (variable) {
if (Object.prototype.hasOwnProperty.call(this.variableInputs, variable.id)) {
return this.variableInputs[variable.id];
Expand Down
6 changes: 6 additions & 0 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,12 @@ class Runtime extends EventEmitter {
blockJSON.nextStatement = null; // null = available connection; undefined = terminal
}
break;
case BlockType.INLINE:
blockJSON.output = null;
blockInfo.disableMonitor = true;
blockInfo.branchCount = blockInfo.branchCount || 1;
blockJSON.outputShape = ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE;
break;
}

const blockText = Array.isArray(blockInfo.text) ? blockInfo.text : [blockInfo.text];
Expand Down
8 changes: 7 additions & 1 deletion src/extension-support/block-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ const BlockType = {
/**
* Arbitrary scratch-blocks XML.
*/
XML: 'xml'
XML: 'xml',

/**
* Specialized reporter block that allows for the insertion and evaluation
* of a substack.
*/
INLINE: 'inline'
};

module.exports = BlockType;