Skip to content

Add BlockType.INLINE support #2 #217

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

Closed
wants to merge 4 commits into from
Closed
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
34 changes: 32 additions & 2 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,29 @@ class JSGenerator {
return new TypedInput(`p${node.index}`, TYPE_UNKNOWN);

case 'compat':
if (node.blockType === BlockType.INLINE) {
Copy link
Member Author

@yuri-kiss yuri-kiss Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently util.startBranch with the loop parameter as true does not actually make it a loop, I dont know how to fix this while still allowing a return value.

And honestly I don't see a practical reason for the block to be a loop anyways but I did attempt to support it with no avail, the return value was able to be grabbed but without using globalState.blockUtility._startedBranch[0] I cant get the branch from what I know.

If this does not matter I can resolve this, I just wanted to point it out to you. (garbomuffin)

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);

case 'constant':
return this.safeConstantInput(node.value);

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 @@ -1192,6 +1212,16 @@ class JSGenerator {
this.variableInputs = {};
}

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;
}

descendStack (nodes, frame) {
// Entering a stack -- all bets are off.
// TODO: allow if/else to inherit values
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;