Merge pull request #1563 from mzgoddard/get-param-condition-input

Get param condition input
This commit is contained in:
Karishma Chadha 2018-09-18 15:29:43 -04:00 committed by GitHub
commit 0ad59d3859
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 7 deletions

View file

@ -70,6 +70,8 @@ const parseProcedureArgMap = function (procCode) {
arg.inputOp = 'math_number';
} else if (argType === 's') {
arg.inputOp = 'text';
} else if (argType === 'b') {
arg.inputOp = 'boolean';
}
argMap.push(arg);
}
@ -117,6 +119,7 @@ const flatten = function (blocks) {
* @param {Function} addBroadcastMsg function to update broadcast message name map
* @param {Function} getVariableId function to retreive a variable's ID based on name
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
* @param {ParseState} parseState - info on the state of parsing beyond the current block.
* @param {object<int, Comment>} comments - Comments from sb2 project that need to be attached to blocks.
* They are indexed in this object by the sb2 flattened block list index indicating
* which block they should attach to.
@ -125,14 +128,15 @@ const flatten = function (blocks) {
* @return {Array<Array.<object>|int>} Tuple where first item is the Scratch VM-format block list, and
* second item is the updated comment index
*/
const parseBlockList = function (blockList, addBroadcastMsg, getVariableId, extensions, comments, commentIndex) {
const parseBlockList = function (blockList, addBroadcastMsg, getVariableId, extensions, parseState, comments,
commentIndex) {
const resultingList = [];
let previousBlock = null; // For setting next.
for (let i = 0; i < blockList.length; i++) {
const block = blockList[i];
// eslint-disable-next-line no-use-before-define
const parsedBlockAndComments = parseBlock(block, addBroadcastMsg, getVariableId,
extensions, comments, commentIndex);
extensions, parseState, comments, commentIndex);
const parsedBlock = parsedBlockAndComments[0];
// Update commentIndex
commentIndex = parsedBlockAndComments[1];
@ -168,8 +172,9 @@ const parseScripts = function (scripts, blocks, addBroadcastMsg, getVariableId,
const scriptX = script[0];
const scriptY = script[1];
const blockList = script[2];
const parseState = {};
const [parsedBlockList, newCommentIndex] = parseBlockList(blockList, addBroadcastMsg, getVariableId, extensions,
comments, scriptIndexForComment);
parseState, comments, scriptIndexForComment);
scriptIndexForComment = newCommentIndex;
if (parsedBlockList[0]) {
parsedBlockList[0].x = scriptX * WORKSPACE_X_SCALE;
@ -269,6 +274,7 @@ const parseMonitorObject = (object, runtime, targets, extensions) => {
null, // `addBroadcastMsg`, not needed for monitor blocks.
getVariableId,
extensions,
{},
null, // `comments`, not needed for monitor blocks
null // `commentIndex`, not needed for monitor blocks
);
@ -744,6 +750,7 @@ const specMapBlock = function (block) {
* @param {Function} addBroadcastMsg function to update broadcast message name map
* @param {Function} getVariableId function to retrieve a variable's ID based on name
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
* @param {ParseState} parseState - info on the state of parsing beyond the current block.
* @param {object<int, Comment>} comments - Comments from sb2 project that need to be attached to blocks.
* They are indexed in this object by the sb2 flattened block list index indicating
* which block they should attach to.
@ -752,7 +759,7 @@ const specMapBlock = function (block) {
* @return {Array.<object|int>} Tuple where first item is the Scratch VM-format block (or null if unsupported object),
* and second item is the updated comment index (after this block and its children are parsed)
*/
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions, comments, commentIndex) {
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions, parseState, comments, commentIndex) {
const commentsForParsedBlock = (comments && typeof commentIndex === 'number' && !isNaN(commentIndex)) ?
comments[commentIndex] : null;
const blockMetadata = specMapBlock(sb2block);
@ -805,6 +812,8 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
}
commentIndex++;
const parentExpectedArg = parseState.expectedArg;
// For a procedure call, generate argument map from proc string.
if (oldOpcode === 'call') {
blockMetadata.argMap = parseProcedureArgMap(sb2block[1]);
@ -829,18 +838,20 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
if (typeof providedArg === 'object' && providedArg) {
// Block or block list occupies the input.
let innerBlocks;
parseState.expectedArg = expectedArg;
if (typeof providedArg[0] === 'object' && providedArg[0]) {
// Block list occupies the input.
[innerBlocks, commentIndex] = parseBlockList(providedArg, addBroadcastMsg, getVariableId,
extensions, comments, commentIndex);
extensions, parseState, comments, commentIndex);
} else {
// Single block occupies the input.
const parsedBlockDesc = parseBlock(providedArg, addBroadcastMsg, getVariableId, extensions,
comments, commentIndex);
parseState, comments, commentIndex);
innerBlocks = [parsedBlockDesc[0]];
// Update commentIndex
commentIndex = parsedBlockDesc[1];
}
parseState.expectedArg = parentExpectedArg;
// Check if innerBlocks is an empty list.
// This indicates that all the inner blocks from the sb2 have
// unknown opcodes and have been skipped.
@ -865,6 +876,11 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
}
// Generate a shadow block to occupy the input.
if (!expectedArg.inputOp) {
// Undefined inputOp. inputOp should always be defined for inputs.
log.warn(`Unknown input operation for input ${expectedArg.inputName} of opcode ${activeBlock.opcode}.`);
continue;
}
if (expectedArg.inputOp === 'boolean' || expectedArg.inputOp === 'substack') {
// No editable shadow input; e.g., for a boolean.
continue;
}
@ -1079,8 +1095,15 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
argumentids: JSON.stringify(parseProcedureArgIds(sb2block[1]))
};
} else if (oldOpcode === 'getParam') {
let returnCode = sb2block[2];
// Ensure the returnCode is "b" if used in a boolean input.
if (parentExpectedArg && parentExpectedArg.inputOp === 'boolean' && returnCode !== 'b') {
returnCode = 'b';
}
// Assign correct opcode based on the block shape.
switch (sb2block[2]) {
switch (returnCode) {
case 'r':
activeBlock.opcode = 'argument_reporter_string_number';
break;

View file

@ -803,6 +803,7 @@ const specMap = {
},
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK'
}
]
@ -812,6 +813,7 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK'
}
]
@ -821,10 +823,12 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'boolean',
inputName: 'CONDITION'
},
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK'
}
]
@ -834,14 +838,17 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'boolean',
inputName: 'CONDITION'
},
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK'
},
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK2'
}
]
@ -851,6 +858,7 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'boolean',
inputName: 'CONDITION'
}
]
@ -860,10 +868,12 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'boolean',
inputName: 'CONDITION'
},
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK'
}
]
@ -873,10 +883,12 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'boolean',
inputName: 'CONDITION'
},
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK'
}
]
@ -895,6 +907,7 @@ const specMap = {
},
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK'
}
]
@ -948,6 +961,7 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'substack',
inputName: 'SUBSTACK'
}
]
@ -1255,10 +1269,12 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'boolean',
inputName: 'OPERAND1'
},
{
type: 'input',
inputOp: 'boolean',
inputName: 'OPERAND2'
}
]
@ -1268,10 +1284,12 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'boolean',
inputName: 'OPERAND1'
},
{
type: 'input',
inputOp: 'boolean',
inputName: 'OPERAND2'
}
]
@ -1281,6 +1299,7 @@ const specMap = {
argMap: [
{
type: 'input',
inputOp: 'boolean',
inputName: 'OPERAND'
}
]

Binary file not shown.