Commit 159d80d3 authored by Carlos Pereira Atencio's avatar Carlos Pereira Atencio Committed by carlospamg

Arduino control generator has been depreciated and loops is used instead (if...

Arduino control generator has been depreciated and loops is used instead (if statement is included in logic generator).
Work has been done to port this generator, and it is not yet finished. Needs to be tested properly.
parent e2ebf833
/**
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* http://blockly.googlecode.com/
*
* 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 Arduino for control blocks.
* @author gasolin@gmail.com (Fred Lin)
*/
'use strict';
Blockly.Arduino = Blockly.Generator.get('Arduino');
Blockly.Arduino.controls_if = function() {
// If/elseif/else condition.
var n = 0;
var argument = Blockly.Arduino.valueToCode(this, 'IF' + n,
Blockly.Arduino.ORDER_NONE) || 'false';
var branch = Blockly.Arduino.statementToCode(this, 'DO' + n);
var code = 'if (' + argument + ') {\n' + branch + '\n}';
for (n = 1; n <= this.elseifCount_; n++) {
argument = Blockly.Arduino.valueToCode(this, 'IF' + n,
Blockly.Arduino.ORDER_NONE) || 'false';
branch = Blockly.Arduino.statementToCode(this, 'DO' + n);
code += ' else if (' + argument + ') {\n' + branch + '}';
}
if (this.elseCount_) {
branch = Blockly.Arduino.statementToCode(this, 'ELSE');
code += ' else {\n' + branch + '\n}';
}
return code + '\n';
};
Blockly.Arduino.controls_repeat = function() {
// Repeat n times.
var repeats = Number(this.getTitleValue('TIMES'));
var branch = Blockly.Arduino.statementToCode(this, 'DO');
if (Blockly.Arduino.INFINITE_LOOP_TRAP) {
branch = Blockly.Arduino.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + this.id + '\'') + branch;
}
var loopVar = Blockly.Arduino.variableDB_.getDistinctName(
'count', Blockly.Variables.NAME_TYPE);
var code = 'for (' + loopVar + ' = 0; ' +
loopVar + ' < ' + repeats + '; ' +
loopVar + '++) {\n' +
branch + '}\n';
return code;
};
Blockly.Arduino.controls_whileUntil = function() {
// Do while/until loop.
var argument0 = Blockly.Arduino.valueToCode(this, 'BOOL',
Blockly.Arduino.ORDER_NONE) || 'false';
var branch = Blockly.Arduino.statementToCode(this, 'DO');
if (Blockly.Arduino.INFINITE_LOOP_TRAP) {
branch = Blockly.Arduino.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + this.id + '\'') + branch;
}
if (this.getTitleValue('MODE') == 'UNTIL') {
if (!argument0.match(/^\w+$/)) {
argument0 = '(' + argument0 + ')';
}
argument0 = '!' + argument0;
}
return 'while (' + argument0 + ') {\n' + branch + '}\n';
};
Blockly.Arduino.controls_for = function() {
// For loop.
var variable0 = Blockly.Arduino.variableDB_.getName(
this.getTitleValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.Arduino.valueToCode(this, 'FROM',
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
var argument1 = Blockly.Arduino.valueToCode(this, 'TO',
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
var branch = Blockly.Arduino.statementToCode(this, 'DO');
if (Blockly.Arduino.INFINITE_LOOP_TRAP) {
branch = Blockly.Arduino.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + this.id + '\'') + branch;
}
var code;
if (argument0.match(/^-?\d+(\.\d+)?$/) &&
argument1.match(/^-?\d+(\.\d+)?$/)) {
// Both arguments are simple numbers.
var up = parseFloat(argument0) <= parseFloat(argument1);
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
variable0 + (up ? '++' : '--') + ') {\n' +
branch + '}\n';
} else {
code = '';
// Cache non-trivial values to variables to prevent repeated look-ups.
var startVar = argument0;
if (!argument0.match(/^\w+$/) && !argument0.match(/^-?\d+(\.\d+)?$/)) {
var startVar = Blockly.Arduino.variableDB_.getDistinctName(
variable0 + '_start', Blockly.Variables.NAME_TYPE);
code += 'int ' + startVar + ' = ' + argument0 + ';\n';
}
var endVar = argument1;
if (!argument1.match(/^\w+$/) && !argument1.match(/^-?\d+(\.\d+)?$/)) {
var endVar = Blockly.Arduino.variableDB_.getDistinctName(
variable0 + '_end', Blockly.Variables.NAME_TYPE);
code += 'int ' + endVar + ' = ' + argument1 + ';\n';
}
code += 'for (' + variable0 + ' = ' + startVar + ';\n' +
' (' + startVar + ' <= ' + endVar + ') ? ' +
variable0 + ' <= ' + endVar + ' : ' +
variable0 + ' >= ' + endVar + ';\n' +
' ' + variable0 + ' += (' + startVar + ' <= ' + endVar +
') ? 1 : -1) {\n' +
branch0 + '}\n';
}
return code;
};
Blockly.Arduino.controls_forEach = function() {
// For each loop.
var variable0 = Blockly.Arduino.variableDB_.getName(
this.getTitleValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.Arduino.valueToCode(this, 'LIST',
Blockly.Arduino.ORDER_ASSIGNMENT) || '[]';
var branch = Blockly.Arduino.statementToCode(this, 'DO');
if (Blockly.Arduino.INFINITE_LOOP_TRAP) {
branch = Blockly.Arduino.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + this.id + '\'') + branch;
}
var code = 'for (var ' + variable0 + ' in ' + argument0 + ') {\n' +
branch + '}\n';
return code;
};
Blockly.Arduino.controls_flow_statements = function() {
// Flow statements: continue, break.
switch (this.getTitleValue('FLOW')) {
case 'BREAK':
return 'break;\n';
case 'CONTINUE':
return 'continue;\n';
}
throw 'Unknown flow statement.';
};
/**
* @license
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* http://blockly.googlecode.com/
* https://blockly.googlecode.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -19,59 +20,105 @@
/**
* @fileoverview Generating Arduino for logic blocks.
* @author gasolin@gmail.com (Fred Lin)
*/
'use strict';
Blockly.Arduino = Blockly.Generator.get('Arduino');
goog.provide('Blockly.Arduino.logic');
Blockly.Arduino.logic_compare = function() {
goog.require('Blockly.Arduino');
Blockly.Arduino['controls_if'] = function(block) {
// If/elseif/else condition.
var n = 0;
var argument = Blockly.Arduino.valueToCode(block, 'IF' + n,
Blockly.Arduino.ORDER_NONE) || 'false';
var branch = Blockly.Arduino.statementToCode(block, 'DO' + n);
var code = 'if (' + argument + ') {\n' + branch + '}';
for (n = 1; n <= block.elseifCount_; n++) {
argument = Blockly.Arduino.valueToCode(block, 'IF' + n,
Blockly.Arduino.ORDER_NONE) || 'false';
branch = Blockly.Arduino.statementToCode(block, 'DO' + n);
code += ' else if (' + argument + ') {\n' + branch + '}';
}
if (block.elseCount_) {
branch = Blockly.Arduino.statementToCode(block, 'ELSE');
code += ' else {\n' + branch + '}';
}
return code + '\n';
};
Blockly.Arduino['logic_compare'] = function(block) {
// Comparison operator.
var mode = this.getTitleValue('OP');
var operator = Blockly.Arduino.logic_compare.OPERATORS[mode];
var OPERATORS = {
'EQ': '==',
'NEQ': '!=',
'LT': '<',
'LTE': '<=',
'GT': '>',
'GTE': '>='
};
var operator = OPERATORS[block.getFieldValue('OP')];
var order = (operator == '==' || operator == '!=') ?
Blockly.Arduino.ORDER_EQUALITY : Blockly.Arduino.ORDER_RELATIONAL;
var argument0 = Blockly.Arduino.valueToCode(this, 'A', order) || '0';
var argument1 = Blockly.Arduino.valueToCode(this, 'B', order) || '0';
var argument0 = Blockly.Arduino.valueToCode(block, 'A', order) || '0';
var argument1 = Blockly.Arduino.valueToCode(block, 'B', order) || '0';
var code = argument0 + ' ' + operator + ' ' + argument1;
return [code, order];
};
Blockly.Arduino.logic_compare.OPERATORS = {
EQ: '==',
NEQ: '!=',
LT: '<',
LTE: '<=',
GT: '>',
GTE: '>='
};
Blockly.Arduino.logic_operation = function() {
Blockly.Arduino['logic_operation'] = function(block) {
// Operations 'and', 'or'.
var operator = (this.getTitleValue('OP') == 'AND') ? '&&' : '||';
var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
var order = (operator == '&&') ? Blockly.Arduino.ORDER_LOGICAL_AND :
Blockly.Arduino.ORDER_LOGICAL_OR;
var argument0 = Blockly.Arduino.valueToCode(this, 'A', order) || 'false';
var argument1 = Blockly.Arduino.valueToCode(this, 'B', order) || 'false';
var argument0 = Blockly.Arduino.valueToCode(block, 'A', order) || 'false';
var argument1 = Blockly.Arduino.valueToCode(block, 'B', order) || 'false';
if (!argument0 && !argument1) {
// If there are no arguments, then the return value is false.
argument0 = 'false';
argument1 = 'false';
} else {
// Single missing arguments have no effect on the return value.
var defaultArgument = (operator == '&&') ? 'true' : 'false';
if (!argument0) {
argument0 = defaultArgument;
}
if (!argument1) {
argument1 = defaultArgument;
}
}
var code = argument0 + ' ' + operator + ' ' + argument1;
return [code, order];
};
Blockly.Arduino.logic_negate = function() {
Blockly.Arduino['logic_negate'] = function(block) {
// Negation.
var order = Blockly.Arduino.ORDER_UNARY_PREFIX;
var argument0 = Blockly.Arduino.valueToCode(this, 'BOOL', order) || 'false';
var argument0 = Blockly.Arduino.valueToCode(block, 'BOOL', order) || 'false';
var code = '!' + argument0;
return [code, order];
};
Blockly.Arduino.logic_boolean = function() {
Blockly.Arduino['logic_boolean'] = function(block) {
// Boolean values true and false.
var code = (this.getTitleValue('BOOL') == 'TRUE') ? 'true' : 'false';
var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.logic_null = function() {
Blockly.Arduino['logic_null'] = function(block) {
var code = 'NULL';
return [code ,Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino['logic_ternary'] = function(block) {
// Ternary operator.
var value_if = Blockly.Arduino.valueToCode(block, 'IF',
Blockly.Arduino.ORDER_CONDITIONAL) || 'false';
var value_then = Blockly.Arduino.valueToCode(block, 'THEN',
Blockly.Arduino.ORDER_CONDITIONAL) || 'null';
var value_else = Blockly.Arduino.valueToCode(block, 'ELSE',
Blockly.Arduino.ORDER_CONDITIONAL) || 'null';
var code = value_if + ' ? ' + value_then + ' : ' + value_else
return [code, Blockly.Arduino.ORDER_CONDITIONAL];
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment