How should I add a new laguage with blockly?

1,470 views
Skip to first unread message

張家瑋

unread,
Jun 26, 2018, 8:20:24 AM6/26/18
to Blockly
Hello

Recently, i tried to make a new language with Blockly
I created some blocks and find many example codes on official tutorial or github.
But it is still hard for me to generate code in new language because there is no example like creating new language.
Is there any tutorial or simple example to generate code in new language ?

Thanks

Rachel Fenichel

unread,
Jun 26, 2018, 8:28:48 PM6/26/18
to Blockly
We don't have a guide for how to build your own generator for a new language.

All generators inherit from Blockly.Generator.  The entry point for code generation is workspaceToCode.  The basic generator class handles walking the tree of blocks and calling blockToCode on all blocks.  You will want to read this class to understand the code.  There are five generators that we provide that inherit from Blockly.Generator.  They are in the generators folder. 

Here are the basic steps to build your own generator:
- Copy one of the existing generators: generators/lua.js, javascript.js, python.js, dart.js, or php.js.  
  - I'll use lua.js for example links below.
- Make sure all references to the old language name are replaced with the new language name. 
- Call Blockly.YourLanguageName.addReservedWords with a list of all reserved words in your language (example)
- Set up order of operation enums (example)
  - See the operator precedence page for more information.
- Implement the basic set of functions:
  - init
  - finish
  - scrubNakedValue
  - scrub_
- Add your generator to the playground and hook up a button so that you can test generating your language
- Define and include generators for a very basic set of blocks (e.g. numbers or strings) and test that they generate correctly, then build from there.

Hope that helps,
Rachel

張家瑋

unread,
Jun 27, 2018, 6:45:27 AM6/27/18
to Blockly
Thanks for your advice.
I'll try them after reading the code.

張家瑋

unread,
Jun 27, 2018, 8:50:47 AM6/27/18
to Blockly
Actually I'm trying to make llvm ARM tablegen by blockly. (ARMInstrInfo.td or maybe other .td files)
Here is what i write after reading generator.js and other files in generator folder. (github)
By the way, I am unfamiliar with javascript and my English isn't very good, please don't mind
If I made a mistake please let me know.

Thanks again!


Scott Yang

unread,
Jul 4, 2020, 12:08:56 AM7/4/20
to Blockly
Hi Zhang,
  So you finally successfully made it?

Ben Shen

unread,
Apr 16, 2021, 7:52:55 PM4/16/21
to Blockly
Hi Rachel,

Thank you for your guide on how to set it up. I'm trying to implement a subset of C. 
I have done what you described about create a new  "geneartors/c.js".

But I have a bit hard time navigate the build system.
Especially about the "goog" part, I'm using a react wrapper https://github.com/nbudin/react-blockly. It seems I can't import the "geneartors/c.js" directly. So I assume I need to somehow generate a "c_comressed.js"? Could you provide some guide/example on how to achieve this?

Thanks,
Ben
Reply all
Reply to author
Forward
0 new messages