Commit f168f725 authored by Carlos Pereira Atencio's avatar Carlos Pereira Atencio Committed by carlospamg

Updates to Arduino generators for procedure and lists.

  Minor changes to variables, loops, and colour.
  Although there is work left to do, it is at an ok state to push to master, so that the "behind the wire" (fork) can pull from upstream (this).
parent 76c73d8c
......@@ -23,9 +23,9 @@
*/
'use strict';
goog.provide('Blockly.JavaScript.colour');
goog.provide('Blockly.Arduino.colour');
goog.require('Blockly.JavaScript');
goog.require('Blockly.Arduino');
Blockly.Arduino.colour_picker = function() {
// Colour picker.
......
/**
* @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,29 +20,31 @@
/**
* @fileoverview Generating Arduino for list blocks.
* @author gasolin@gmail.com (Fred Lin)
*/
'use strict';
Blockly.Arduino = Blockly.Generator.get('Arduino');
goog.provide('Blockly.Arduino.lists');
Blockly.Arduino.lists_create_empty = function() {
goog.require('Blockly.Arduino');
Blockly.Arduino['lists_create_empty'] = function(block) {
// Create an empty list.
return ['[]', Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.lists_create_with = function() {
Blockly.Arduino['lists_create_with'] = function(block) {
// Create a list with any number of elements of any type.
var code = new Array(this.itemCount_);
for (var n = 0; n < this.itemCount_; n++) {
code[n] = Blockly.Arduino.valueToCode(this, 'ADD' + n,
var code = new Array(block.itemCount_);
for (var n = 0; n < block.itemCount_; n++) {
code[n] = Blockly.Arduino.valueToCode(block, 'ADD' + n,
Blockly.Arduino.ORDER_NONE) || 'null';
}
var code = '[' + code.join(', ') + ']';
code = '[' + code.join(', ') + ']';
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.lists_repeat = function() {
Blockly.Arduino['lists_repeat'] = function(block) {
// Create a list with one element repeated.
if (!Blockly.Arduino.definitions_['lists_repeat']) {
// Function adapted from Closure's goog.array.repeat.
......@@ -58,48 +61,49 @@ Blockly.Arduino.lists_repeat = function() {
func.push('}');
Blockly.Arduino.definitions_['lists_repeat'] = func.join('\n');
}
var argument0 = Blockly.Arduino.valueToCode(this, 'ITEM', true) || 'null';
var argument1 = Blockly.Arduino.valueToCode(this, 'NUM') || '0';
var argument0 = Blockly.Arduino.valueToCode(block, 'ITEM', true) || 'null';
var argument1 = Blockly.Arduino.valueToCode(block, 'NUM') || '0';
var code = Blockly.Arduino.lists_repeat.repeat +
'(' + argument0 + ', ' + argument1 + ')';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
Blockly.Arduino.lists_length = function() {
Blockly.Arduino['lists_length'] = function(block) {
// Testing the length of a list is the same as for a string.
return Blockly.Arduino.text_length.call(this);
return Blockly.Arduino.text_length.call(block);
};
Blockly.Arduino.lists_isEmpty = function() {
Blockly.Arduino['lists_isEmpty'] = function(block) {
// Testing a list for being empty is the same as for a string.
return Blockly.Arduino.text_isEmpty.call(this);
return Blockly.Arduino.text_isEmpty.call(block);
};
Blockly.Arduino.lists_indexOf = function() {
Blockly.Arduino['lists_indexOf'] = function(block) {
// Searching a list for a value is the same as search for a substring.
return Blockly.Arduino.text_indexOf.call(this);
return Blockly.Arduino.text_indexOf.call(block);
};
Blockly.Arduino.lists_getIndex = function() {
Blockly.Arduino['lists_getIndex'] = function(block) {
// Indexing into a list is the same as indexing into a string.
return Blockly.Arduino.text_charAt.call(this);
return Blockly.Arduino.text_charAt.call(block);
};
Blockly.Arduino.lists_setIndex = function() {
Blockly.Arduino['lists_setIndex'] = function(block) {
// Set element at index.
var argument0 = Blockly.Arduino.valueToCode(this, 'AT',
//TODO: Need to add MODE and WHERE inputs.
var at = Blockly.Arduino.valueToCode(block, 'AT',
Blockly.Arduino.ORDER_ADDITIVE) || '1';
var argument1 = Blockly.Arduino.valueToCode(this, 'LIST',
var list = Blockly.Arduino.valueToCode(block, 'LIST',
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '[]';
var argument2 = Blockly.Arduino.valueToCode(this, 'TO',
var value = Blockly.Arduino.valueToCode(block, 'TO',
Blockly.Arduino.ORDER_ASSIGNMENT) || 'null';
// Blockly uses one-based indicies.
if (argument0.match(/^\d+$/)) {
if (at.match(/^\d+$/)) {
// If the index is a naked number, decrement it right now.
argument0 = parseInt(argument0, 10) - 1;
at = parseInt(at, 10) - 1;
} else {
// If the index is dynamic, decrement it in code.
argument0 += ' - 1';
at += ' - 1';
}
return argument1 + '[' + argument0 + '] = ' + argument2 + ';\n';
return list + '[' + at + '] = ' + value + ';\n';
};
......@@ -20,7 +20,6 @@
/**
* @fileoverview Generating Arduino for loop blocks.
* @author gasolin@gmail.com (Fred Lin)
*/
'use strict';
......@@ -128,7 +127,7 @@ Blockly.Arduino['controls_for'] = function(block) {
// changes during loop execution.
var incVar = Blockly.Arduino.variableDB_.getDistinctName(
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
code += 'var ' + incVar + ' = ';
code += 'int ' + incVar + ' = ';
if (Blockly.isNumber(increment)) {
code += Math.abs(increment) + ';\n';
} else {
......
/**
* @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.
......@@ -18,64 +19,66 @@
*/
/**
* @fileoverview Generating Arduino for variable blocks.
* @author gasolin@gmail.com (Fred Lin)
* @fileoverview Generating Arduino for procedure blocks.
*/
'use strict';
Blockly.Arduino = Blockly.Generator.get('Arduino');
goog.provide('Blockly.Arduino.procedures');
Blockly.Arduino.procedures_defreturn = function() {
goog.require('Blockly.Arduino');
Blockly.Arduino['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
var funcName = Blockly.Arduino.variableDB_.getName(this.getTitleValue('NAME'),
var funcName = Blockly.Arduino.variableDB_.getName(block.getTitleValue('NAME'),
Blockly.Procedures.NAME_TYPE);
var branch = Blockly.Arduino.statementToCode(this, 'STACK');
var branch = Blockly.Arduino.statementToCode(block, 'STACK');
if (Blockly.Arduino.INFINITE_LOOP_TRAP) {
branch = Blockly.Arduino.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + this.id + '\'') + branch;
'\'' + block.id + '\'') + branch;
}
var returnValue = Blockly.Arduino.valueToCode(this, 'RETURN',
var returnValue = Blockly.Arduino.valueToCode(block, 'RETURN',
Blockly.Arduino.ORDER_NONE) || '';
if (returnValue) {
returnValue = ' return ' + returnValue + ';\n';
}
var returnType = returnValue ? 'Dynamic' : 'void';
var args = [];
for (var x = 0; x < this.arguments_.length; x++) {
args[x] = Blockly.Arduino.variableDB_.getName(this.arguments_[x],
for (var x = 0; x < block.arguments_.length; x++) {
args[x] = Blockly.Arduino.variableDB_.getName(block.arguments_[x],
Blockly.Variables.NAME_TYPE);
}
var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
branch + returnValue + '}\n';
code = Blockly.Arduino.scrub_(this, code);
code = Blockly.Arduino.scrub_(block, code);
Blockly.Arduino.definitions_[funcName] = code;
return null;
};
// Defining a procedure without a return value uses the same generator as
// a procedure with a return value.
Blockly.Arduino.procedures_defnoreturn = Blockly.Arduino.procedures_defreturn;
Blockly.Arduino['procedures_defnoreturn'] = Blockly.Arduino['procedures_defreturn'];
Blockly.Arduino.procedures_callreturn = function() {
Blockly.Arduino['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
var funcName = Blockly.Arduino.variableDB_.getName(this.getTitleValue('NAME'),
var funcName = Blockly.Arduino.variableDB_.getName(block.getTitleValue('NAME'),
Blockly.Procedures.NAME_TYPE);
var args = [];
for (var x = 0; x < this.arguments_.length; x++) {
args[x] = Blockly.Arduino.valueToCode(this, 'ARG' + x,
for (var x = 0; x < block.arguments_.length; x++) {
args[x] = Blockly.Arduino.valueToCode(block, 'ARG' + x,
Blockly.Arduino.ORDER_NONE) || 'null';
}
var code = funcName + '(' + args.join(', ') + ')';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
Blockly.Arduino.procedures_callnoreturn = function() {
Blockly.Arduino['procedures_callnoreturn'] = function(block) {
// Call a procedure with no return value.
var funcName = Blockly.Arduino.variableDB_.getName(this.getTitleValue('NAME'),
var funcName = Blockly.Arduino.variableDB_.getName(block.getTitleValue('NAME'),
Blockly.Procedures.NAME_TYPE);
var args = [];
for (var x = 0; x < this.arguments_.length; x++) {
args[x] = Blockly.Arduino.valueToCode(this, 'ARG' + x,
for (var x = 0; x < block.arguments_.length; x++) {
args[x] = Blockly.Arduino.valueToCode(block, 'ARG' + x,
Blockly.Arduino.ORDER_NONE) || 'null';
}
var code = funcName + '(' + args.join(', ') + ');\n';
......@@ -84,11 +87,11 @@ Blockly.Arduino.procedures_callnoreturn = function() {
Blockly.Arduino.procedures_ifreturn = function() {
// Conditionally return value from a procedure.
var condition = Blockly.Arduino.valueToCode(this, 'CONDITION',
var condition = Blockly.Arduino.valueToCode(block, 'CONDITION',
Blockly.Arduino.ORDER_NONE) || 'false';
var code = 'if (' + condition + ') {\n';
if (this.hasReturnValue_) {
var value = Blockly.Arduino.valueToCode(this, 'VALUE',
if (block.hasReturnValue_) {
var value = Blockly.Arduino.valueToCode(block, 'VALUE',
Blockly.Arduino.ORDER_NONE) || 'null';
code += ' return ' + value + ';\n';
} else {
......
/**
* @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.
......
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