Skip to content

Commit

Permalink
feat: initial blockly update
Browse files Browse the repository at this point in the history
  • Loading branch information
felixerdy committed Nov 25, 2024
1 parent c1ff1d4 commit 9661d9d
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 114 deletions.
17 changes: 3 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/actions/workspaceActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export const onChangeCode = () => (dispatch, getState) => {
const workspace = Blockly.getMainWorkspace();
var code = getState().workspace.code;
code.arduino = Blockly.Generator.Arduino.workspaceToCode(workspace);
code.arduino = Blockly.Arduino.workspaceToCode(workspace);
code.simulator = Blockly.Simulator.workspaceToCode(workspace);
code.simulator = Blockly.Generator.Simulator.workspaceToCode(workspace);
var xmlDom = Blockly.Xml.workspaceToDom(workspace);
var board = getState().board.board;
xmlDom.setAttribute("board", board);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Blockly from "blockly";
import * as Blockly from "blockly";
import { selectedBoard } from "../../helpers/board";

/**
Expand Down
65 changes: 37 additions & 28 deletions src/components/Blockly/generator/simulator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { javascriptGenerator } from "blockly/javascript";
* Arduino code generator.
* @type !Blockly.Generator
*/
Blockly["Simulator"] = javascriptGenerator;
Blockly.Generator.Simulator = javascriptGenerator;

/**
*
Expand All @@ -44,50 +44,52 @@ Blockly["Simulator"] = javascriptGenerator;
* Initialise the database of variable names.
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
*/
Blockly["Simulator"].init = function (workspace) {
Blockly.Generator.Simulator.init = function (workspace) {
// creates a dictionary of modules / components that are used in the code
Blockly["Simulator"].modules_ = Object.create(null);
Blockly.Generator.Simulator.modules_ = Object.create(null);

// creates a list of code to be setup before the setup block
Blockly["Simulator"].setupCode_ = Object.create(null);
Blockly.Generator.Simulator.setupCode_ = Object.create(null);

// creates a list of code for the loop to be runned once
Blockly["Simulator"].codeFunctions_ = Object.create(null);
Blockly.Generator.Simulator.codeFunctions_ = Object.create(null);

// creates a list of code variables
Blockly["Simulator"].variables_ = Object.create(null);
Blockly.Generator.Simulator.variables_ = Object.create(null);

// Create a dictionary mapping desired function names in definitions_
// to actual function names (to avoid collisions with user functions).
Blockly["Simulator"].functionNames_ = Object.create(null);
Blockly.Generator.Simulator.functionNames_ = Object.create(null);

Blockly["Simulator"].variablesInitCode_ = "";
Blockly.Generator.Simulator.variablesInitCode_ = "";

if (!Blockly["Simulator"].nameDB_) {
Blockly["Simulator"].nameDB_ = new Blockly.Names(
Blockly["Simulator"].RESERVED_WORDS_,
if (!Blockly.Generator.Simulator.nameDB_) {
Blockly.Generator.Simulator.nameDB_ = new Blockly.Names(
Blockly.Generator.Simulator.RESERVED_WORDS_,
);
} else {
Blockly["Simulator"].nameDB_.reset();
Blockly.Generator.Simulator.nameDB_.reset();
}

Blockly["Simulator"].nameDB_.setVariableMap(workspace.getVariableMap());
Blockly.Generator.Simulator.nameDB_.setVariableMap(
workspace.getVariableMap(),
);
};

/**
* Prepend the generated code with the variable definitions.
* @param {string} code Generated code.
* @return {string} Completed code.
*/
Blockly["Simulator"].finish = function (code) {
Blockly.Generator.Simulator.finish = function (code) {
const commentCode =
"// Simulator code generated by senseBox Blockly on " + new Date();

const setupCode =
Object.values(Blockly["Simulator"].setupCode_).join("\n") || "";
Object.values(Blockly.Generator.Simulator.setupCode_).join("\n") || "";

const modules =
Object.values(Blockly["Simulator"]?.modules_).join(", ") || "";
Object.values(Blockly.Generator.Simulator?.modules_).join(", ") || "";

const loopCode = `while (true) {
${code}}`;
Expand All @@ -97,9 +99,9 @@ ${code}}`;
${setupCode}
${loopCode}`;

console.log(Blockly["Simulator"].formatCode(simCode));
console.log(Blockly.Generator.Simulator.formatCode(simCode));

return Blockly["Simulator"].formatCode(simCode);
return Blockly.Generator.Simulator.formatCode(simCode);
};

/**
Expand All @@ -108,7 +110,7 @@ ${loopCode}`;
* @param {string} line Line of generated code.
* @return {string} Legal line of code.
*/
Blockly["Simulator"].scrubNakedValue = function (line) {
Blockly.Generator.Simulator.scrubNakedValue = function (line) {
return line + ";\n";
};

Expand All @@ -118,7 +120,7 @@ Blockly["Simulator"].scrubNakedValue = function (line) {
* @param {string} code The Arduino code to format.
* @return {string} Formatted Arduino code.
*/
Blockly["Simulator"].formatCode = function (code) {
Blockly.Generator.Simulator.formatCode = function (code) {
let formattedCode = "";
let indentLevel = 0;
const indentSize = 2; // Number of spaces per indentation level
Expand Down Expand Up @@ -167,7 +169,7 @@ Blockly["Simulator"].formatCode = function (code) {
* @return {string} Arduino string.
* @private
*/
Blockly["Simulator"].quote_ = function (string) {
Blockly.Generator.Simulator.quote_ = function (string) {
// Can't use goog.string.quote since Google's style guide recommends
// JS string literals use single quotes.
string = string
Expand All @@ -187,7 +189,7 @@ Blockly["Simulator"].quote_ = function (string) {
* @return {string} Arduino code with comments and subsequent blocks added.
* @private
*/
Blockly["Simulator"].scrub_ = function (block, code) {
Blockly.Generator.Simulator.scrub_ = function (block, code) {
let commentCode = "";
// Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) {
Expand All @@ -197,18 +199,21 @@ Blockly["Simulator"].scrub_ = function (block, code) {
comment = comment
? Blockly.utils.string.wrap(
comment,
Blockly["Simulator"].COMMENT_WRAP - 3,
Blockly.Generator.Simulator.COMMENT_WRAP - 3,
)
: null;
if (comment) {
if (block.getProcedureDef) {
// Use a comment block for function comments.
commentCode +=
"/**\n" +
Blockly["Simulator"].prefixLines(comment + "\n", " * ") +
Blockly.Generator.Simulator.prefixLines(comment + "\n", " * ") +
" */\n";
} else {
commentCode += Blockly["Simulator"].prefixLines(comment + "\n", "// ");
commentCode += Blockly.Generator.Simulator.prefixLines(
comment + "\n",
"// ",
);
}
}
// Collect comments for all value arguments.
Expand All @@ -217,15 +222,19 @@ Blockly["Simulator"].scrub_ = function (block, code) {
if (block.inputList[i].type === Blockly.INPUT_VALUE) {
const childBlock = block.inputList[i].connection.targetBlock();
if (childBlock) {
const comment = Blockly["Simulator"].allNestedComments(childBlock);
const comment =
Blockly.Generator.Simulator.allNestedComments(childBlock);
if (comment) {
commentCode += Blockly["Simulator"].prefixLines(comment, "// ");
commentCode += Blockly.Generator.Simulator.prefixLines(
comment,
"// ",
);
}
}
}
}
}
const nextBlock = block.nextConnection && block.nextConnection.targetBlock();
const nextCode = Blockly["Simulator"].blockToCode(nextBlock);
const nextCode = Blockly.Generator.Simulator.blockToCode(nextBlock);
return commentCode + code + nextCode;
};
71 changes: 45 additions & 26 deletions src/components/Blockly/generator/simulator/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import * as Blockly from "blockly/core";
* @return {string} Completed code.
*/

Blockly.Simulator["arduino_functions"] = function (block) {
Blockly.Generator.Simulator.forBlock["arduino_functions"] = function (
block,
generator,
) {
var board = window.sessionStorage.getItem("board");

if (board === "mcu" || board === "mini") {
Blockly.Simulator.libraries_["library_senseBoxIO"] =
Blockly.Generator.Simulator.libraries_["library_senseBoxIO"] =
"#include <senseBoxIO.h>";
}
// Edited version of Blockly.Generator.prototype.statementToCode
function statementToCodeNoTab(block, name) {
var targetBlock = block.getInputTargetBlock(name);
var code = Blockly.Simulator.blockToCode(targetBlock);
var code = Blockly.Generator.Simulator.blockToCode(targetBlock);
if (typeof code != "string") {
throw new Error(
'Expecting code from statement block "' + targetBlock.type + '".',
Expand All @@ -26,31 +29,41 @@ Blockly.Simulator["arduino_functions"] = function (block) {
return code;
}

var setupBranch = Blockly.Simulator.statementToCode(block, "SETUP_FUNC");
// //var setupCode = Blockly.Simulator.scrub_(block, setupBranch); No comment block
var setupBranch = Blockly.Generator.Simulator.statementToCode(
block,
"SETUP_FUNC",
);
// //var setupCode = Blockly.Generator.Simulator.scrub_(block, setupBranch); No comment block
if (setupBranch) {
Blockly.Simulator.setupCode_["mainsetup"] = setupBranch;
Blockly.Generator.Simulator.setupCode_["mainsetup"] = setupBranch;
}

var loopBranch = statementToCodeNoTab(block, "LOOP_FUNC");
//var loopcode = Blockly.Simulator.scrub_(block, loopBranch); No comment block
//var loopcode = Blockly.Generator.Simulator.scrub_(block, loopBranch); No comment block
return loopBranch;
};

Blockly.Simulator["procedures_defreturn"] = function (block) {
Blockly.Generator.Simulator.forBlock["procedures_defreturn"] = function (
block,
generator,
) {
// Define a procedure with a return value.
const funcName = Blockly.Simulator.nameDB_.getName(
const funcName = Blockly.Generator.Simulator.nameDB_.getName(
block.getFieldValue("NAME"),
Blockly.Procedures.NAME_TYPE,
);
const branch = Blockly.Simulator.statementToCode(block, "STACK");
const branch = Blockly.Generator.Simulator.statementToCode(block, "STACK");
const returnType = block.getFieldValue("RETURN TYPE") || "void";

let returnValue =
Blockly.Simulator.valueToCode(block, "RETURN", Blockly.Simulator.ORDER_NONE) ||
"";
Blockly.Generator.Simulator.valueToCode(
block,
"RETURN",
Blockly.Generator.Simulator.ORDER_NONE,
) || "";
if (returnValue) {
returnValue = Blockly.Simulator.INDENT + "return " + returnValue + ";\n";
returnValue =
Blockly.Generator.Simulator.INDENT + "return " + returnValue + ";\n";
}
const args = [];
for (let i = 0; i < block.argumentVarModels_.length; i++) {
Expand All @@ -69,9 +82,9 @@ Blockly.Simulator["procedures_defreturn"] = function (block) {
branch +
returnValue +
"}";
code = Blockly.Simulator.scrub_(block, code);
code = Blockly.Generator.Simulator.scrub_(block, code);
// Add % so as not to collide with helper functions in definitions list.
Blockly.Simulator.functionNames_["%" + funcName] = code;
Blockly.Generator.Simulator.functionNames_["%" + funcName] = code;
return null;
};

Expand All @@ -92,41 +105,47 @@ function translateType(type) {
}
}

Blockly.Simulator["procedures_defnoreturn"] =
Blockly.Simulator["procedures_defreturn"];
Blockly.Generator.Simulator["procedures_defnoreturn"] =
Blockly.Generator.Simulator["procedures_defreturn"];

Blockly.Simulator["procedures_callreturn"] = function (block) {
Blockly.Generator.Simulator.forBlock["procedures_callreturn"] = function (
block,
generator,
) {
// Call a procedure with a return value.
const funcName = Blockly.Simulator.nameDB_.getName(
const funcName = Blockly.Generator.Simulator.nameDB_.getName(
block.getFieldValue("NAME"),
Blockly.Procedures.NAME_TYPE,
);
const args = [];
for (let i = 0; i < block.arguments_.length; i++) {
args[i] =
Blockly.Simulator.valueToCode(
Blockly.Generator.Simulator.valueToCode(
block,
"ARG" + i,
Blockly.Simulator.ORDER_COMMA,
Blockly.Generator.Simulator.ORDER_COMMA,
) || "null";
}
const code = funcName + "(" + args.join(", ") + ")";
return [code, Blockly.Simulator.ORDER_ATOMIC];
return [code, Blockly.Generator.Simulator.ORDER_ATOMIC];
};

Blockly.Simulator["procedures_callnoreturn"] = function (block) {
Blockly.Generator.Simulator.forBlock["procedures_callnoreturn"] = function (
block,
generator,
) {
// Call a procedure with no return value.
const funcName = Blockly.Simulator.nameDB_.getName(
const funcName = Blockly.Generator.Simulator.nameDB_.getName(
block.getFieldValue("NAME"),
Blockly.Procedures.NAME_TYPE,
);
const args = [];
for (let i = 0; i < block.arguments_.length; i++) {
args[i] =
Blockly.Simulator.valueToCode(
Blockly.Generator.Simulator.valueToCode(
block,
"ARG" + i,
Blockly.Simulator.ORDER_COMMA,
Blockly.Generator.Simulator.ORDER_COMMA,
) || "null";
}

Expand Down
Loading

0 comments on commit 9661d9d

Please sign in to comment.