forked from google/blockly
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloops.js
165 lines (155 loc) · 5.65 KB
/
loops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @license
* Copyright 2014 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Generating Zig for loop blocks.
*/
'use strict';
goog.module('Blockly.Zig.loops');
const Zig = goog.require('Blockly.Zig');
const stringUtils = goog.require('Blockly.utils.string');
const {NameType} = goog.require('Blockly.Names');
Zig['controls_repeat_ext'] = function(block) {
let repeats;
// Repeat n times.
if (block.getField('TIMES')) {
// Internal number.
repeats = String(Number(block.getFieldValue('TIMES')));
} else {
// External number.
repeats = Zig.valueToCode(block, 'TIMES', Zig.ORDER_ASSIGNMENT) || '0';
}
let branch = Zig.statementToCode(block, 'DO');
branch = Zig.addLoopTrap(branch, block);
let code = '';
const loopVar = Zig.nameDB_.getDistinctName('count', NameType.VARIABLE);
let endVar = repeats;
if (!repeats.match(/^\w+$/) && !stringUtils.isNumber(repeats)) {
endVar = Zig.nameDB_.getDistinctName('repeat_end', NameType.VARIABLE);
code += 'var ' + endVar + ' = ' + repeats + ';\n';
}
code += [
`var ${loopVar}: usize = 0;\n`,
`while (${loopVar} < ${endVar}) : (${loopVar} += 1) {\n`,
branch,
'}\n'
].join('');
return code;
};
Zig['controls_repeat'] = Zig['controls_repeat_ext'];
Zig['controls_whileUntil'] = function(block) {
// Do while/until loop.
const until = block.getFieldValue('MODE') === 'UNTIL';
let argument0 =
Zig.valueToCode(
block, 'BOOL', until ? Zig.ORDER_UNARY_PREFIX : Zig.ORDER_NONE) ||
'false';
let branch = Zig.statementToCode(block, 'DO');
branch = Zig.addLoopTrap(branch, block);
if (until) {
argument0 = '!' + argument0;
}
return 'while (' + argument0 + ') {\n' + branch + '}\n';
};
Zig['controls_for'] = function(block) {
// For loop.
const variable0 =
Zig.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
const argument0 =
Zig.valueToCode(block, 'FROM', Zig.ORDER_ASSIGNMENT) || '0';
const argument1 = Zig.valueToCode(block, 'TO', Zig.ORDER_ASSIGNMENT) || '0';
const increment = Zig.valueToCode(block, 'BY', Zig.ORDER_ASSIGNMENT) || '1';
let branch = Zig.statementToCode(block, 'DO');
branch = Zig.addLoopTrap(branch, block);
let code;
if (stringUtils.isNumber(argument0) && stringUtils.isNumber(argument1) &&
stringUtils.isNumber(increment)) {
// All arguments are simple numbers.
const up = Number(argument0) <= Number(argument1);
code = 'for (' + variable0 + ' = ' + argument0 + '; ' + variable0 +
(up ? ' <= ' : ' >= ') + argument1 + '; ' + variable0;
const step = Math.abs(Number(increment));
if (step === 1) {
code += up ? '++' : '--';
} else {
code += (up ? ' += ' : ' -= ') + step;
}
code += ') {\n' + branch + '}\n';
} else {
code = '';
// Cache non-trivial values to variables to prevent repeated look-ups.
let startVar = argument0;
if (!argument0.match(/^\w+$/) && !stringUtils.isNumber(argument0)) {
startVar =
Zig.nameDB_.getDistinctName(variable0 + '_start', NameType.VARIABLE);
code += 'var ' + startVar + ' = ' + argument0 + ';\n';
}
let endVar = argument1;
if (!argument1.match(/^\w+$/) && !stringUtils.isNumber(argument1)) {
endVar =
Zig.nameDB_.getDistinctName(variable0 + '_end', NameType.VARIABLE);
code += 'var ' + endVar + ' = ' + argument1 + ';\n';
}
// Determine loop direction at start, in case one of the bounds
// changes during loop execution.
const incVar =
Zig.nameDB_.getDistinctName(variable0 + '_inc', NameType.VARIABLE);
code += 'num ' + incVar + ' = ';
if (stringUtils.isNumber(increment)) {
code += Math.abs(increment) + ';\n';
} else {
code += '(' + increment + ').abs();\n';
}
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
code += Zig.INDENT + incVar + ' = -' + incVar + ';\n';
code += '}\n';
code += 'for (' + variable0 + ' = ' + startVar + '; ' + incVar +
' >= 0 ? ' + variable0 + ' <= ' + endVar + ' : ' + variable0 +
' >= ' + endVar + '; ' + variable0 + ' += ' + incVar + ') {\n' +
branch + '}\n';
}
return code;
};
Zig['controls_forEach'] = function(block) {
// For each loop.
const variable0 =
Zig.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
const argument0 =
Zig.valueToCode(block, 'LIST', Zig.ORDER_ASSIGNMENT) || '[]';
let branch = Zig.statementToCode(block, 'DO');
branch = Zig.addLoopTrap(branch, block);
const code =
'for (var ' + variable0 + ' in ' + argument0 + ') {\n' + branch + '}\n';
return code;
};
Zig['controls_flow_statements'] = function(block) {
// Flow statements: continue, break.
let xfix = '';
if (Zig.STATEMENT_PREFIX) {
// Automatic prefix insertion is switched off for this block. Add manually.
xfix += Zig.injectId(Zig.STATEMENT_PREFIX, block);
}
if (Zig.STATEMENT_SUFFIX) {
// Inject any statement suffix here since the regular one at the end
// will not get executed if the break/continue is triggered.
xfix += Zig.injectId(Zig.STATEMENT_SUFFIX, block);
}
if (Zig.STATEMENT_PREFIX) {
const loop = block.getSurroundLoop();
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
// of the loop will not get executed if 'continue' is triggered.
// In the case of 'break', a prefix is needed due to the loop's suffix.
xfix += Zig.injectId(Zig.STATEMENT_PREFIX, loop);
}
}
switch (block.getFieldValue('FLOW')) {
case 'BREAK':
return xfix + 'break;\n';
case 'CONTINUE':
return xfix + 'continue;\n';
}
throw Error('Unknown flow statement.');
};