Generate code before the current one

94 views
Skip to first unread message

daniel net

unread,
May 31, 2024, 4:03:27 PMMay 31
to Blockly
May 31, 2024
Hi !
I am developing Blockly for Basic (Small Basic: https://smallbasic-publicwebsite.azurewebsites.net/)
This Basic is poor and does not include functions, however it can be executed in an internet browser.
I would like to support functions only in the case of variable assignment:
-----
a = func(5)
-----
If we defined: func(x), then, to generate correct code with Blockly, we would have to write:
-----
x = 5
func()
a = func_Return_Value
-----
NB: By convention, "func_Return_Value" will contain the return value.
But there you have it, when generating with Blockly, the generator, and in particular the procedures.ts package containing the procedures_callreturn function is called just after generating "a=".
I wrongly generate the code:
-----
a = func_Return_Value
'***Move this Instruction Block before the previous one!
x = 5
func()
'***End of Block
-----
Question: How to generate the function call code BEFORE assigning the variable "a"?
Thanks for your help !
Daniel

Mark Friedman

unread,
May 31, 2024, 6:38:52 PMMay 31
to blo...@googlegroups.com
Daniel,

  As I understand it, the Small Basic language only supports functions (they call them subroutines) that take no arguments and that don't return values (see, for example, this doc).  I think that what you are asking for is a way to generate Small Basic code for Blockly's standard built-in function definition and function call blocks, which allow arguments and return values.  Are you sure that you want to do that?  It might be clearer to create your own simpler function blocks which match the ways that Small Basic's subroutines work.  

  If you decide that you'd like to create your own, custom, blocks, a good place to start in the Blockly documentation is here.  And we'll be happy to help you here in this forum to help if you need any questions answered or run into issues.

  Hope this helps.

-Mark


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/e7e60bfd-dbb1-4bc2-954a-5b0ba1d3c780n%40googlegroups.com.

daniel net

unread,
Jun 3, 2024, 11:40:33 AMJun 3
to Blockly
Hello Mark!
Thank you for your reply !
I would like to add that I will train people new to programming.
Blockly is a good approach, but subsequently, JavaScript is a bit complex for beginners.
This is why I am currently developing the move from Blockly to Basic.
Note that there is another Small Basic (https://smallbasic.github.io/) which is much more complete,
however it does not offer the possibility of running its Basic in a web browser.

I don't want to show a regression of Small Basic compared to Blockly.
On the contrary, I even want to show that with a poorly evolved language, we can do what other languages ​​do, with a few small efforts.
I don't know if generating code before the current statement is feasible in the Blockly environment, but it would be a great help because it would allow me to also resolve the conditional statement (<cond> ? <when true>: <when ​​false>;)

Thanks for your help !
Daniel

daniel net

unread,
Jun 5, 2024, 3:07:33 PMJun 5
to Blockly
Hello Blockly team
Finally, with tenacity, I managed to generate some code before an assignment was generated.
For this, there are 3 updates :
1) Add the declaration in core/block.ts:
 prelude_code: string = '';
 
2) In generators/<language>/variables.ts
 Replace the "variables_set" function by:
 export function variables_set(block: Block, generator: BasicGenerator) {
 // Setter variable.
 block.prelude_code = '';
 const argument0 = generator.valueToCode(block, 'VALUE', Order.ASSIGNMENT) || '0';
 const varName = generator.getVariableName(block.getFieldValue('VAR'));
 return block.prelude_code + varName + ' = ' + argument0 + '\n';}
 
3) In the function that wants to generate code BEFORE the assignment do :
 (for example in math.ramdom :)
 let code = '';
 let prelude_code = '';
 let block_Parent = block.getParent();
 let block_Parent_type = (block_Parent == null) ? 'Null': block_Parent.type;
  // This prelude_code must be generated ONLY if block_Parent_type == 'variables_set'
  prelude_code += "''***Assign Parameters and Call the Function:\n";
  prelude_code += 'a = ' + argument0 + '\n' +
                  'b = ' + argument1 + '\n' +
                  functionName + "() '(" + argument0 + ', ' + argument1 + ")'\n" +
                  "''***End Function Call\n";
  if (block_Parent_type == 'variables_set')
 {
 if (block_Parent != null)
  {block_Parent.prelude_code += prelude_code;
  code = functionName + "_Return_Value";
  }
  else
  {code = '***Generation Error! block.parent is null\n';}
 }
 else // (block_Parent_type != 'variables_set')
 {
 code = functionName + "_Return_Value\n" + '***Generation Error: block_Parent_type=' + block_Parent_type + '\n' + prelude_code;
 }
==> And that’s it!

Rather than constraining this generation BEFORE an assignment, it would have been better to search for the start of the main instruction.
Thus, we would no longer be limited to:
                                       block_Parent_type == 'variables_set'
but rather to:
                                       block_Parent_... Parent_type == <main instruction type>
But I didn't go that far !
If you could help me, it would expand my constraint.
Thank you for your help !
Daniel

Beka Westberg

unread,
Jun 6, 2024, 12:59:12 PMJun 6
to Blockly
Hello,

Could you post the code for your procedure block-code generator? I think there might be a simpler way to solve this but I want to verify my understanding of what you're currently doing first!

Best wishes,
--Beka

daniel net

unread,
Jun 8, 2024, 10:29:42 AMJun 8
to Blockly
Hello Beka
First, thank you for your interest in my question.
Here are the 3 procedures in question :

1) core/block.ts: see line 222

2) generators/basic/variables.ts: see lines 28 and 32

3) generators/basic/math.ts: see lines from 560


Thank you for your help
Daniel

Reply all
Reply to author
Forward
0 new messages