https://github.com/richorama/ironblock
:arrow_forward: A .net core interpreter for blockly programs
https://github.com/richorama/ironblock
blockly netcore
Last synced: 4 months ago
JSON representation
:arrow_forward: A .net core interpreter for blockly programs
- Host: GitHub
- URL: https://github.com/richorama/ironblock
- Owner: richorama
- License: mit
- Created: 2017-11-22T14:19:13.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-27T10:27:10.000Z (over 1 year ago)
- Last Synced: 2025-03-13T16:46:20.162Z (4 months ago)
- Topics: blockly, netcore
- Language: C#
- Homepage: https://richorama.github.io/IronBlock/
- Size: 19.2 MB
- Stars: 88
- Watchers: 13
- Forks: 24
- Open Issues: 12
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README

# IronBlock
A .net core interpreter for [blockly](https://developers.google.com/blockly), allowing you to execute blockly programs in .NET.
## Installation
Via [nuget](https://www.nuget.org/packages/IronBlock):
```
PM> Install-Package IronBlock
```or
```
> dotnet add package IronBlock
```## Basic Usage
Firstly, in JavaScript save your blockly workspace as an XML file:
```js
var xml = Blockly.Xml.workspaceToDom(workspace);
```The blockly [code demo](https://blockly-demo.appspot.com/static/demos/code/index.html) allows you to view the XML of your workspace.
The XML will look something like this:
```xml
Hello World
```
You'll need to pass this XML to your .NET server using an Ajax call or similar.
You can then parse the XML, and execute the Blockly program in .NET.
```cs
using IronBlock;
using IronBlock.Blocks;// create a parser
var parser = new Parser();// add the standard blocks to the parser
parser.AddStandardBlocks();// parse the xml file to create a workspace
var workspace = parser.Parse(xml);// run the workspace
var output = workspace.Evaluate();// "Hello World"
// you can optionally pass in a dictionary of variables
var args = new Dictionary();
args.Add("message", "Hello!");
workspace.Evaluate(args);// if your program sets any variable values,
// you can read then out of the args dictionary
```## Custom Blocks
Blockly has a [block designer](https://blockly-demo.appspot.com/static/demos/blockfactory/index.html) allowing you to create your own blocks very easily.
Custom blocks can be implemented in C# by inheriting `IBlock`:
```cs
public class MyCustomBlock : IBlock
{
public override object Evaluate(Context context)
{
// read a field
var myField = this.Fields.Get("MY_FIELD");
// evaluate a value
var myValue = this.Values.Evaluate("MY_VALUE", context);
// evaluate a statement
var myStatement = this.Statements.Get("MY_STATEMENT");
myStatement.Evaluate(context); // evaluate your statement// if your block returns a value, simply `return myValue`
// if your block is part of a statment, and another block runs after it, call
base.Evaluate(context);
return null;
}
}
```You can then register your block and run it:
```cs
var parser = new Parser();
parser.AddBlock("my_custom_block");
var workspace = parser.Parse(xml);
workspace.Evaluate();
```## License
MIT