How to show "Multidimensional Array" to the users by the generated PHP code?

308 views
Skip to first unread message

Maziarser

unread,
Oct 5, 2017, 11:17:54 AM10/5/17
to Blockly
Hi all,

I have made a block that the user should give it a "row" number and a "column" number and an "object name":









The PHP code which is generated is like below and the output is working fine:
room(18,30,"wall");

But I want to show the generated PHP code like below as it is a "Multidimensional Array":
room[18][30]= $wall;

Thanks for your help and time in advance!
Maziar




Cory Diers

unread,
Oct 5, 2017, 6:45:53 PM10/5/17
to blo...@googlegroups.com
Hi Maziar,

Can you send what your generator looks like for that block? I suspect that's where your issue is.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Cory Diers | Software Engineer | cory...@google.com | 

Maziarser

unread,
Oct 6, 2017, 8:02:07 AM10/6/17
to Blockly
Hi Cory,

The Block definition JS code:
Blockly.Blocks['frontend'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("set");
    this.appendValueInput("row")
        .setCheck("Number")
        .appendField("ROW");
    this.appendValueInput("col")
        .setCheck("Number")
        .appendField("COLUMN");
    this.appendDummyInput()
        .appendField("to")
        .appendField(new Blockly.FieldDropdown([["Wall","\"wall\""], ["Blank","\"blank\""]]), "item");
    this.setInputsInline(false);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(330);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};

and the Generator Stub in PHP:

Blockly.PHP['frontend'] = function(block) {
  var value_row = Blockly.PHP.valueToCode(block, 'row', Blockly.PHP.ORDER_ATOMIC);
  var value_col = Blockly.PHP.valueToCode(block, 'col', Blockly.PHP.ORDER_ATOMIC);
  var dropdown_item = block.getFieldValue('item');
  // TODO: Assemble PHP into code variable.
  var code = 'build(' + value_row + ',' + value_col + ',' + dropdown_item + ');\n'; 
  return code;
};

So, code will return the "build" which is a php function in my build.php file:
function build($rows, $cols, $build_items){
 //more stuff 
} 

As I have mentioned, it is working properly but I want to show the generated code in a way that I mentioned ( generated PHP code like below as it is a "Multidimensional Array").

Thank for your help! 
Maziar


On Friday, October 6, 2017 at 12:45:53 AM UTC+2, Cory Diers wrote:
Hi Maziar,

Can you send what your generator looks like for that block? I suspect that's where your issue is.
On Thu, Oct 5, 2017 at 8:17 AM, Maziarser <mazyar...@gmail.com> wrote:
Hi all,

I have made a block that the user should give it a "row" number and a "column" number and an "object name":









The PHP code which is generated is like below and the output is working fine:
room(18,30,"wall");

But I want to show the generated PHP code like below as it is a "Multidimensional Array":
room[18][30]= $wall;

Thanks for your help and time in advance!
Maziar




--
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.

Cory Diers

unread,
Oct 6, 2017, 1:53:06 PM10/6/17
to blo...@googlegroups.com
The key part of this is this line:

  var code = 'build(' + value_row + ',' + value_col + ',' + dropdown_item + ');\n'; 

That generates build(18,30,"wall");

All you need to change to get the result you're looking for is to do something like this:

  var code = 'room[' + value_row + '][' + value_col + '] = $' + dropdown_item + ';\n'; 

Remember - Blockly is just generating strings of code to be run later. If you want to change the pattern of what it generates, all you have to do is change what the generator returns.

To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Maziarser

unread,
Oct 6, 2017, 2:13:18 PM10/6/17
to Blockly
That's exactly the part that I have the problem.

I can not pass the parameters to the function same as what you write. As I mentioned, my php function is like:

function build($rows, $cols, $build_items){//do stuff}
OR
function room($rows, $cols, $build_items){//do stuff}

I can pass the parameters like:

var code = 'build(' + value_row + ',' + value_col + ',' + dropdown_item + ');\n';

or

var code = 'room(' + value_row + ',' + value_col + ',' + dropdown_item + ');\n';


but as i said i can not pass the function like what you write. it will show what I want as the generated php code. BUT, it is not executing and it shows the error like:

Parse error: syntax error, unexpected '['

Cory Diers

unread,
Oct 6, 2017, 4:39:12 PM10/6/17
to blo...@googlegroups.com
So, there are two potential breakage points here: One is that the code you're trying to run doesn't work the way you want it to, and the second is that Blockly isn't generating the right code. Have you tried writing and running the code that you want want manually? Just write a script by hand that does:

room[18][30]= $wall;

If that's not working, then the issue you're having is a PHP one. You mention that room is a function, but you're trying to access it as a multi-dimensional array. I'm not a PHP expert, but I think that's going to be a problem until you make room a multi-dimensional array instead of a function. Blockly just outputs whatever string you tell it to, if that string isn't valid PHP, then it's going to cause problems.

If, on the other hand, you have the code you want and it works correctly when you write it manually, then we likely have a Blockly issue. If that's the case, what is different about the generated code from the code you wrote by hand? (Or if the syntax error you quoted above is coming from generating code from Blockly, where exactly is the error happening?)

To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Maziarser

unread,
Oct 7, 2017, 8:14:42 AM10/7/17
to Blockly
Ok, here is the story:

Blockly is generating the code which should be generated. That's why it is working correctly like this:
room(18,30,"wall");

If I write the code manually like this:
room[18][30]= $wall;

It will not work. 
why? because I did not pass the parameters to the function "room" in a right way!

On the other hand, I can not define the room as a multi-dimensional array as it should be a function which is executing a bunch of stuff for me, but what i want user to see is a multi-dimensional array (to make the code more interesting and understandable for them).

Thus, what I was thinking when I asked this question, is to twinge the "var code" in generator to pass the variable to the room function in a correct way (room(18,30,"wall");), but show the code in a way to be understandable for my user as a multi-dimensional array (room[18][30]= $wall;).  

Maziar

Cory Diers

unread,
Oct 9, 2017, 1:08:29 PM10/9/17
to blo...@googlegroups.com
Blockly isn't really set up for a use case like that. Since the intent of Blockly is to generate code that executes, "Generating code that doesn't work correctly" is somewhat out of scope. If you really want to do it this way, you're likely going to have to define two different generators - one for the code that gets generated "under the hood," as it were, and one that generates the code for the end user. I'm not totally sure how easy that would be, but I imagine that if you redefine those generators (Blockly.PHP['frontend']) to be the code that runs under the hood or the "fake" version that's the multi-dimensional array, it would work the way you want it to.

To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Andrew n marshall

unread,
Oct 16, 2017, 6:58:26 PM10/16/17
to blo...@googlegroups.com
One approach could be to have two different generators. One for execution, and one for display. In many cases, you would convert the workspace to two different code strings, one for each generator.

It makes when you are trying to add annotations to code, perhaps to highlight or step through code. However, I hesitate to recommend it in a case where you are showing users (and thus teaching them) code that doesn't actually run.
Cory Diers | Software Engineer | corydiers@google.com | 

Reply all
Reply to author
Forward
0 new messages