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
|
/**
* @license
* Visual Blocks Language
*
* Copyright 2015 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Generating PHP for procedure blocks.
* @author daarond@gmail.com (Daaron Dwyer)
*/
'use strict';
goog.provide('Blockly.PHP.procedures');
goog.require('Blockly.PHP');
Blockly.PHP['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
// First, add a 'global' statement for every variable that is assigned.
var globals = block.workspace.variableList;
for (var i = globals.length - 1; i >= 0; i--) {
var varName = globals[i];
if (block.arguments_.indexOf(varName) == -1) {
globals[i] = Blockly.PHP.variableDB_.getName(varName,
Blockly.Variables.NAME_TYPE);
} else {
// This variable is actually a parameter name. Do not include it in
// the list of globals, thus allowing it be of local scope.
globals.splice(i, 1);
}
}
globals = globals.length ? ' global ' + globals.join(', ') + ';\n' : '';
var funcName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var branch = Blockly.PHP.statementToCode(block, 'STACK');
if (Blockly.PHP.STATEMENT_PREFIX) {
branch = Blockly.PHP.prefixLines(
Blockly.PHP.STATEMENT_PREFIX.replace(/%1/g,
'\'' + block.id + '\''), Blockly.PHP.INDENT) + branch;
}
if (Blockly.PHP.INFINITE_LOOP_TRAP) {
branch = Blockly.PHP.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + block.id + '\'') + branch;
}
var returnValue = Blockly.PHP.valueToCode(block, 'RETURN',
Blockly.PHP.ORDER_NONE) || '';
if (returnValue) {
returnValue = ' return ' + returnValue + ';\n';
}
var args = [];
for (var i = 0; i < block.arguments_.length; i++) {
args[i] = Blockly.PHP.variableDB_.getName(block.arguments_[i],
Blockly.Variables.NAME_TYPE);
}
var code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
globals + branch + returnValue + '}';
code = Blockly.PHP.scrub_(block, code);
// Add % so as not to collide with helper functions in definitions list.
Blockly.PHP.definitions_['%' + funcName] = code;
return null;
};
// Defining a procedure without a return value uses the same generator as
// a procedure with a return value.
Blockly.PHP['procedures_defnoreturn'] =
Blockly.PHP['procedures_defreturn'];
Blockly.PHP['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
var funcName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var args = [];
for (var i = 0; i < block.arguments_.length; i++) {
args[i] = Blockly.PHP.valueToCode(block, 'ARG' + i,
Blockly.PHP.ORDER_COMMA) || 'null';
}
var code = funcName + '(' + args.join(', ') + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['procedures_callnoreturn'] = function(block) {
// Call a procedure with no return value.
var funcName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var args = [];
for (var i = 0; i < block.arguments_.length; i++) {
args[i] = Blockly.PHP.valueToCode(block, 'ARG' + i,
Blockly.PHP.ORDER_COMMA) || 'null';
}
var code = funcName + '(' + args.join(', ') + ');\n';
return code;
};
Blockly.PHP['procedures_ifreturn'] = function(block) {
// Conditionally return value from a procedure.
var condition = Blockly.PHP.valueToCode(block, 'CONDITION',
Blockly.PHP.ORDER_NONE) || 'false';
var code = 'if (' + condition + ') {\n';
if (block.hasReturnValue_) {
var value = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_NONE) || 'null';
code += ' return ' + value + ';\n';
} else {
code += ' return;\n';
}
code += '}\n';
return code;
};
|