Commit 02f230db authored by carlosperate's avatar carlosperate

Improve block iteration for static typing.

Fixes some of the current issues with assigning types.
parent 441d2913
...@@ -43,7 +43,7 @@ Blockly.StaticTyping.BlocklyType = { ...@@ -43,7 +43,7 @@ Blockly.StaticTyping.BlocklyType = {
}; };
/** /**
* Navigates through the top level blocks collecting all variables and getting * Navigates through all the blocks, collecting all variables and getting
* their type into an associative array with the variable names as the keys and * their type into an associative array with the variable names as the keys and
* the type as the values. * the type as the values.
* @param {Blockly.Workspace} workspace Blockly Workspace to collect variables. * @param {Blockly.Workspace} workspace Blockly Workspace to collect variables.
...@@ -51,39 +51,81 @@ Blockly.StaticTyping.BlocklyType = { ...@@ -51,39 +51,81 @@ Blockly.StaticTyping.BlocklyType = {
* and the type as the values. * and the type as the values.
*/ */
Blockly.StaticTyping.getAllVarsWithTypes = function(workspace) { Blockly.StaticTyping.getAllVarsWithTypes = function(workspace) {
if (workspace.getTopBlocks) { var blocks = Blockly.StaticTyping.getAllStatementsOrdered(workspace);
var blocks = workspace.getTopBlocks(true);
} else {
throw 'Not a valid workspace: ' + workspace;
}
var varsWithTypes = Object.create(null); var varsWithTypes = Object.create(null);
var varUndefBlockList = Object.create(null); var varUndefBlockList = Object.create(null);
for (var x = 0, xlength_ = blocks.length; x < xlength_; x++) { for (var x = 0, xlength_ = blocks.length; x < xlength_; x++) {
var block = blocks[x]; blocks[x].select(); // for step debugging, highlights block in workspace
// Each statement block iterates through its input children collecting vars
var blockVarsWithTypes = Blockly.StaticTyping.getBlockVars(blocks[x]);
for (var z = 0; z < blockVarsWithTypes.length; z++) {
var varName = blockVarsWithTypes[z][0];
var varType = blockVarsWithTypes[z][1];
Blockly.StaticTyping.manageVarsWithTypes(
blocks[x], varName, varType, varsWithTypes, varUndefBlockList);
}
}
return varsWithTypes;
};
/**
* Navigates through each top level block in the workspace to collect all
* statement blocks, in order from top left.
* @param {Blockly.Workspace} workspace Blockly Workspace to collect blocks.
* @return {Array<Blockly.Block>} Array containing all workspace statement
* blocks.
*/
Blockly.StaticTyping.getAllStatementsOrdered = function(workspace) {
if (!workspace.getTopBlocks) {
throw 'Not a valid workspace: ' + workspace;
}
/**
* Navigates through each continous block to collect all statement blocks.
* Function required to use recursion for block input statements.
* @param {Blockly.Block} startBlock Block to start iterating from..
* @return {Array<Blockly.Block>} Array containing all continuous statement
* blocks.
*/
var getAllContinuousStatements = function(startBlock) {
var block = startBlock;
var nextBlock = null;
var connections = null;
var blockNextConnection = null;
var blocks = [];
do { do {
block.select(); // for step debugging, highlights block in workspace block.select(); // for step debugging, highlights block in workspace
var blockVarsWithTypes = Blockly.StaticTyping.getBlockVars(block); blocks.push(block);
for (var z = 0; z < blockVarsWithTypes.length; z++) { blockNextConnection = block.nextConnection;
var varName = blockVarsWithTypes[z][0]; connections = block.getConnections_();
var varType = blockVarsWithTypes[z][1]; block = null;
Blockly.StaticTyping.manageVarsWithTypes( for (var j = 0; j < connections.length; j++) {
block, varName, varType, varsWithTypes, varUndefBlockList); if (connections[j].type == Blockly.NEXT_STATEMENT) {
} // end loop for block variables nextBlock = connections[j].targetBlock();
var childBlocks = null; if (nextBlock) {
if (block.nextConnection) { // If it is the next connection select it and move to the next block
block = block.nextConnection.targetBlock(); if (connections[j] === blockNextConnection) {
} else if (block.getProcedureDef && block = nextBlock;
(childBlocks = block.getChildren()).length > 0) { } else {
// If the block is a function there is no connection, but a child block // Recursion as block children can have their own input statements
block = childBlocks[0]; blocks = blocks.concat(getAllContinuousStatements(nextBlock));
} else { }
block = null; }
}
} }
} while (block); } while (block);
} // end loop for all top blocks
return varsWithTypes; return blocks;
};
var allStatementBlocks = [];
var topBlocks = workspace.getTopBlocks(true);
for (var i = 0; i < topBlocks.length; i++) {
allStatementBlocks = allStatementBlocks.concat(
getAllContinuousStatements(topBlocks[i]));
}
return allStatementBlocks;
}; };
/** /**
...@@ -191,7 +233,8 @@ Blockly.StaticTyping.manageTypeWarning = function(block, bType, vName, vType) { ...@@ -191,7 +233,8 @@ Blockly.StaticTyping.manageTypeWarning = function(block, bType, vName, vType) {
}; };
/** /**
* Iterates through the list of top level blocks and * Iterates through the list of top level blocks and sets the function arguments
* types.
* @param {Blockly.Workspace} workspace Blockly Workspace to collect variables. * @param {Blockly.Workspace} workspace Blockly Workspace to collect variables.
* @param {Array<string>} Associative array with the variable names as the keys * @param {Array<string>} Associative array with the variable names as the keys
* and the type as the values. * and the type as the values.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment