Completed Java code generator for Blockly

6,651 views
Skip to first unread message

toe...@extremenetworks.com

unread,
Jul 30, 2015, 12:30:22 PM7/30/15
to Blockly
We have completed the Java code generator for all of the blockly blocks such that it runs all of the unit test cases properly now that the mutators have been fixed for the dropdowns.  You can see it https://github.com/toebes-extreme/blockly

However before making a pull request, it is important to point out a couple of things that had to be done to make this work:
  1. We added a getVarsTypes: function to blocks which referenced variables so that the code generator could determine the type of the variable.
  2. We added Blockly.Variables.allVariablesTypes which calls the function and then determines the optimal type for the variable.  If the blockly code shows conflicting types (such as treating x as both a String and a Number as the unit test cases do) then the function declares the variable to be of type Var.
  3. Because of the need for types, we changed arguments_ in procedures.js to be an array of hash of 'name' and 'type' instead of just being an array.  This change was also made in the code generators.  We looked at several options and realized that one thing that would be very useful to have is the ability for a user to specify the types of parameters in their blockly code so that they can take advantage of the same type checking for the core blockly blocks with their own procedures.  For this we would need to know the type.  Technically this will require changing the UI to allow the user to specify the type, but for now we wanted to get the code generator working without that change, but allow for the possibility of it working in the future.
  4. In variables.js we added an onchange: function to determine if a variable is actually a local parameter to a subroutine and not a global variable (as demonstrated by the variable func a in function1 of the procedures.xml test case.  One nice side effect of this change is that local parameter references automatically change their color to match the color of the procedure definition block as you can see in the attached screenshot.
  5. The Java code generator in generators/java.js overrides the workspaceToCode method in order to wrap the generated code with the proper imports/package/class definition and extended classes.  This allows generating completely stand alone code with the exception of the standard JRE.
  6. Because Blockly assumes a typeless language, we created a new Java Var class which does comparisons and mutation of types consistent with the typeless environment when necessary.  However the generate code is what a normal Java programmer would expect to see.
  7. When generating a function or procedure, the code generator generates different types for the parameters.  However, it still generates a global variable of the same name even if it isn't referenced outside of the procedure.
  8. To determine the type of a parameter for a procedure, the code generator looks at both the usage of the local variable (if any) as well as all of the callers to see what type of parameter is passed in.  Likewise the same algorithm is applied to looking at the return type from a function.

Some nice features of the generated Java code to be aware of

  • We have carefully constructed the code to be what a Java programmer would write in their normal IDE including proper comments and indentation.
  • The system optimizes the import of external Java classes to generate the minimum compiled code (i.e. no import something.*).
  • Each generated file is put into a separate class and the generator can also be told of any base class to extend from by calling Blockly.Java.setBaseClass
With this amount of change - specifically with the types, I wanted to get some feedback on the best approach to proceed with a Pull request so that we can add Java to the list of standard languages.  The current code base also includes the mutator and typeblock changes which would take some unmixing...

-- John
Screen Shot 2015-07-30 at 8.12.37 AM.png

Blake

unread,
Jul 30, 2015, 1:01:27 PM7/30/15
to Blockly
John,

Great work! I'm really excited to use a more complete Blockly-Java implementation in my projects.

Is there a live demo somewhere? I'd love to give it a spin.

Thanks,
Blake

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

toe...@extremenetworks.com

unread,
Jul 30, 2015, 1:19:45 PM7/30/15
to Blockly, techplex...@gmail.com
You know.. I started to try to figure out how to put up on Google appspot but never managed to get it done... I will see if that can be done.  That being said, if you download the project from my github, you can run the generators test cases to see the generated Java code.

toe...@extremenetworks.com

unread,
Jul 30, 2015, 3:32:05 PM7/30/15
to Blockly, techplex...@gmail.com, toe...@extremenetworks.com
Ok I've finally figured out how the hosting works...
You can see the current state of the project at: https://pure-object-102213.appspot.com

I've updated the demos to output Java (as well as fixed the PHP output on the code demo).
 https://pure-object-102213.appspot.com/static/demos/code/index.html

If you want to see the range of code generation, just go to:
http://pure-object-102213.appspot.com/static/tests/generators/index.html
and load in the various test cases and click on the Java button to see the code.

Comments and criticisms are very much welcome.
   -- John

Carlos Pereira

unread,
Jul 31, 2015, 12:26:57 AM7/31/15
to Blockly, techplex...@gmail.com, toe...@extremenetworks.com
Cool, I've been looking forward to check this out. First questions so far:

Are all numbers always doubles? is there a way to create an integer?

When a variable is set to a number (block "set item" connected to "number" block) and then do the same for text right after it (same block "set item" connected to "text" block) then it goes crazy and outputs this: https://gist.github.com/carlosperate/5e18129e06f62bfcaf6f

Carlos Pereira

unread,
Jul 31, 2015, 12:32:47 AM7/31/15
to Blockly, techplex...@gmail.com, toe...@extremenetworks.com, carlos...@gmail.com

Also, this block generates

protected Boolean item = false;
item = Var.valueOf(0).equals(0);


What is Var?

toe...@extremenetworks.com

unread,
Jul 31, 2015, 11:40:38 AM7/31/15
to Blockly, techplex...@gmail.com, toe...@extremenetworks.com, carlos...@gmail.com
Var is a custom class that we created to make the Blockly assumptions work (such as being able to treat a variable as a string, number or array without concern).  It also handles the comparison between items such that a string "42" will compare to the integer 42 and the float 42.000 as several of the unit test cases try.

We automatically generate the code for the Var class inline at the bottom of the Java code instead of counting on a separate Jar.  Note that we do depend on other standard Java JRE classes.

toe...@extremenetworks.com

unread,
Jul 31, 2015, 12:28:58 PM7/31/15
to Blockly, techplex...@gmail.com, toe...@extremenetworks.com, carlos...@gmail.com
Ok there are two minor issues here..

  1. The simple cases misses doing a import java.util.List; I will get that added.
  2. The simple case doesn't generate a main function to wrapper the blocks out in the open.  Most of the examples have a clear main function (such as come from the unit test cases). So it isn't clear what we want to do in that case.  I will get all of the open unconnected blocks put into a main() function, but what do we do when there are several unconnected blocks laying around?  In what order do we want to execute them?

Brice Copy

unread,
Mar 20, 2017, 10:57:45 PM3/20/17
to Blockly, techplex...@gmail.com, toe...@extremenetworks.com, carlos...@gmail.com
Hello,

I'm only discovering this thread now and your github repository. I noticed the commits are somewhat behind the official blockly repo, is there a particular reason why this effort stopped ? I was looking for something similar for my project, and seeing you have a finished functional implementation is very encouraging.


Henrik Östman

unread,
Dec 10, 2018, 9:18:28 PM12/10/18
to Blockly
I agree, too many nice project just stalls without any explanations.  It would be nice if this could be merged into Googles own codebase. They only have support for Javascript and some other obscure languages. :-)

Beka Westberg

unread,
Dec 11, 2018, 3:20:36 PM12/11/18
to Blockly
Java would be awesome, I would have loved to create my own minecraft mods (visually) when I was younger!
Reply all
Reply to author
Forward
0 new messages