Skip to content

Commit 39d1274

Browse files
committed
for loop is fixed
1 parent 1b19995 commit 39d1274

4 files changed

Lines changed: 90 additions & 8 deletions

File tree

www/blocs&generateurs/blockly_generateurs_cpp.js

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,106 @@ Blockly.Arduino['controls_switch']=function(block){
2121
code += '}\n'
2222
return code;
2323
};
24-
Blockly.Arduino["controls_for"]=function(block){
24+
/*Blockly.Arduino["controls_for"]=function(block){
2525
var variable0 = Blockly.Arduino.variableDB_.getName(block.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE);
2626
var argument0 = Blockly.Arduino.valueToCode(block, "FROM", Blockly.Arduino.ORDER_ASSIGNMENT);
2727
var argument1 = Blockly.Arduino.valueToCode(block, "TO", Blockly.Arduino.ORDER_ASSIGNMENT);
2828
var argument2 = Blockly.Arduino.valueToCode(block, "BY", Blockly.Arduino.ORDER_ASSIGNMENT);
2929
var branch = Blockly.Arduino.statementToCode(block, "DO");
3030
if (Blockly.Arduino.INFINITE_LOOP_TRAP) branch = Blockly.Arduino.INFINITE_LOOP_TRAP.replace(/%1/g, "'" + block.id + "'") + branch;
3131
if( argument0 < argument1){
32-
return "for (" + variable0 + "=" + argument0 + " ; " + variable0 + "<=" + argument1 + " ; " + variable0 + "=" + variable0 + "+" + argument2 + ") {\n" + branch + "}\n"
32+
return "for (" + variable0 + "=" + argument0 + " ; " + variable0 + "<=" + argument1 + " ; " + variable0 + "=" + variable0 + "+" + argument2 + ") {\n" + branch + "}\n"
3333
}
3434
else {
3535
return "for (" + variable0 + "=" + argument0 + " ; " + variable0 + ">=" + argument1 + " ; " + variable0 + "=" + variable0 + "-" + argument2 + ") {\n" + branch + "}\n"
36+
37+
}
38+
}; */
39+
40+
41+
Blockly.Arduino['controls_for'] = function(block) {
42+
var variable0 = Blockly.Arduino.variableDB_.getName(
43+
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
44+
var argument0 = Blockly.Arduino.valueToCode(block, 'FROM',
45+
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
46+
var argument1 = Blockly.Arduino.valueToCode(block, 'TO',
47+
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
48+
var increment = Blockly.Arduino.valueToCode(block, 'BY',
49+
Blockly.Arduino.ORDER_ASSIGNMENT) || '1';
50+
var branch = Blockly.Arduino.statementToCode(block, 'DO');
51+
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
52+
var code;
53+
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
54+
Blockly.isNumber(increment)) {
55+
// All arguments are simple numbers.
56+
var up = parseFloat(argument0) <= parseFloat(argument1);
57+
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
58+
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
59+
variable0;
60+
var step = Math.abs(parseFloat(increment));
61+
if (step == 1) {
62+
code += up ? '++' : '--';
63+
} else {
64+
code += (up ? ' += ' : ' -= ') + step;
65+
}
66+
code += ') {\n' + branch + '}\n';
67+
} else {
68+
code = '';
69+
// Cache non-trivial values to variables to prevent repeated look-ups.
70+
var startVar = argument0;
71+
if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
72+
var startVar = Blockly.Arduino.variableDB_.getDistinctName(
73+
variable0 + '_start', Blockly.Variables.NAME_TYPE);
74+
code += 'int ' + startVar + ' = ' + argument0 + ';\n';
75+
}
76+
var endVar = argument1;
77+
if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
78+
var endVar = Blockly.Arduino.variableDB_.getDistinctName(
79+
variable0 + '_end', Blockly.Variables.NAME_TYPE);
80+
code += 'int ' + endVar + ' = ' + argument1 + ';\n';
81+
}
82+
// Determine loop direction at start, in case one of the bounds
83+
// changes during loop execution.
84+
var incVar = Blockly.Arduino.variableDB_.getDistinctName(
85+
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
86+
code += 'int ' + incVar + ' = ';
87+
if (Blockly.isNumber(increment)) {
88+
code += Math.abs(increment) + ';\n';
89+
} else {
90+
code += 'abs(' + increment + ');\n';
3691
}
92+
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
93+
code += Blockly.Arduino.INDENT + incVar + ' = -' + incVar + ';\n';
94+
code += '}\n';
95+
code += 'for (' + variable0 + ' = ' + startVar + ';\n' +
96+
' ' + incVar + ' >= 0 ? ' +
97+
variable0 + ' <= ' + endVar + ' : ' +
98+
variable0 + ' >= ' + endVar + ';\n' +
99+
' ' + variable0 + ' += ' + incVar + ') {\n' +
100+
branch + '}\n';
101+
}
102+
return code;
37103
};
38104

39105

40106

107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
122+
123+
41124
Blockly.Arduino["controls_if"]=function(block){
42125
var n = 0;
43126
var argument = Blockly.Arduino.valueToCode(block, "IF" + n, Blockly.Arduino.ORDER_NONE);

www/lang/Blockly_en.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Blockly.Msg.CONTROLS_FLOW_STATEMENTS_TOOLTIP_CONTINUE = "Skip the rest of this l
4747
Blockly.Msg.CONTROLS_FLOW_STATEMENTS_WARNING = "Warning: This block must be used in a loop";
4848
Blockly.Msg.CONTROLS_FOREACH_TITLE = "for each item %1 in list %2";
4949
Blockly.Msg.CONTROLS_FOREACH_TOOLTIP = "For each item in a list, assign the value of the item to variable %1, and then execute statements";
50-
Blockly.Msg.CONTROLS_FOR_TITLE = "for %1 ranging from %2 to %3 (<=)in steps of %4";
50+
Blockly.Msg.CONTROLS_FOR_TITLE = "for %1 ranging from %2 to %3 in steps of %4";
5151
Blockly.Msg.CONTROLS_FOR_TITLE2 = "for %1 ranging from %2 to %3 (>=)in steps of - %4";
5252
Blockly.Msg.CONTROLS_FOR_TOOLTIP = "Set variable %1 to values ​​from start number to end number, incrementing by specified step, and execute the specified statements";
5353
Blockly.Msg.CONTROLS_IF_ELSEIF_TOOLTIP = "Add condition";

www/lang/Blockly_es.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Blockly.Msg.CONTROLS_FLOW_STATEMENTS_TOOLTIP_CONTINUE = "Omita el resto de este
4747
Blockly.Msg.CONTROLS_FLOW_STATEMENTS_WARNING = "Advertencia: este bloque debe usarse en un bucle";
4848
Blockly.Msg.CONTROLS_FOREACH_TITLE = "para cada elemento %1 en la lista %2";
4949
Blockly.Msg.CONTROLS_FOREACH_TOOLTIP = "Para cada elemento de una lista, asigne el valor del elemento a la variable %1, y luego ejecute las declaraciones";
50-
Blockly.Msg.CONTROLS_FOR_TITLE = "para %1 comprendido de %2 a %3 (<=)en pasos de %4";
50+
Blockly.Msg.CONTROLS_FOR_TITLE = "para %1 comprendido de %2 a %3 en pasos de %4";
5151
Blockly.Msg.CONTROLS_FOR_TITLE2 = "para %1 comprendido de %2 a %3 (>=)en pasos de - %4";
5252
Blockly.Msg.CONTROLS_FOR_TOOLTIP = "Establezca la variable %1 en valores desde el número inicial hasta el número final, incrementándolo en un paso específico, y ejecute las declaraciones especificadas";
5353
Blockly.Msg.CONTROLS_IF_ELSEIF_TOOLTIP = "Agregar Condición";

www/toolbox/toolbox_arduino_all-mrtnode.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,10 @@
210210
<block type="controls_repeat_ext"> <value name="TIMES"><shadow type="math_number"><field name="NUM">2</field></shadow></value></block>
211211
<block type="controls_whileUntil"></block>
212212
<block type="controls_for">
213-
<value name="FROM"><shadow type="math_number"><field name="NUM">0</field></shadow></value>
214-
<value name="TO"><shadow type="math_number"><field name="NUM">5</field></shadow></value>
215-
<value name="BY"><shadow type="math_number"><field name="NUM">1</field></shadow></value>
213+
<value name="FROM"><shadow type="math_number"><field name="NUM">0</field></shadow></value>
214+
<value name="TO"><shadow type="math_number"><field name="NUM">5</field></shadow></value>
215+
<value name="BY"><shadow type="math_number"><field name="NUM">1</field></shadow></value>
216216
</block>
217-
218217
<block type="controls_switch"></block>
219218
<block type="controls_flow_statements"></block>
220219
<block type="logic_operation"></block>

0 commit comments

Comments
 (0)