This project provides a Blockly integration for the Hexput programming language. It allows visual programming through custom blocks that generate Hexput code, making it easier to create Hexput programs without writing code directly.
To install the module, use npm:
npm install hexput-blocklyTo use Hexput Blockly in your project, import it and initialize the Blockly environment:
importBlocklyfrom'blockly';import{initBlockly}from'hexput-blockly';// Initialize Blockly with your container ID and optional toolboxinitBlockly(Blockly,'blocklyDiv',yourToolboxDefinition);After creating blocks in the Blockly workspace, you can generate Hexput code:
importBlocklyfrom'blockly';// Get the workspaceconstworkspace=Blockly.getMainWorkspace();// Generate Hexput codeconstcode=Blockly.Hexput.workspaceToCode(workspace);console.log(code);Hexput Blockly provides many custom blocks for the Hexput language:
- Variable declaration (
vl varName = value) - Number, string, and boolean literals
- Variable references
- Array creation with items
- Object creation with properties
- Array and object property access (dot and bracket notation)
- Mathematical operations (addition, subtraction, multiplication, division)
- Comparison operators (equals, not equals, greater than, less than, etc.)
- Logical operators (and, or, not)
- If statements
- Loop statements
- Function calls with parameters
Here's an example of using blocks to define a variable and perform operations:
// In Blockly this would be visual blocks generating:vlmyNumber=42;vlmyString="Hello, world!";vlmyResult=myNumber+10;ifmyResult>50{vl message =myString+" The result is greater than 50!";}loopitemin[1,2,3]{vl doubled =item*2;}To define new blocks for Hexput Blockly, you can extend the module with your own block definitions:
importBlocklyfrom'blockly';import{initBlockly,generateHexputBlockly}from'hexput-blockly';// Define your custom blockBlockly.Blocks['my_custom_block']={init: function(){// Block definition logic}};// Define the generator for your custom blockconsthexputGenerator=generateHexputBlockly(Blockly);hexputGenerator.forBlock['my_custom_block']=function(block){// Code generation logicreturn'my custom hexput code';};Contributions are welcome! Please feel free to submit a pull request or open an issue for any suggestions or improvements.
This project is licensed under the MIT License. See the LICENSE file for more details.