123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- 'use strict';
- goog.provide('Blockly.Python');
- goog.require('Blockly.Generator');
- goog.require('Blockly.StaticTyping');
- Blockly.Python = new Blockly.Generator('Arduino');
- Blockly.Python.StaticTyping = new Blockly.StaticTyping();
- Blockly.Python.addReservedWords(
- 'Blockly,' +
- 'setup,loop,if,else,for,switch,case,while,do,break,continue,return,goto,' +
- 'define,include,HIGH,LOW,INPUT,OUTPUT,INPUT_PULLUP,true,false,integer,' +
- 'constants,floating,point,void,boolean,char,unsigned,byte,int,word,long,' +
- 'float,double,string,String,array,static,volatile,const,sizeof,pinMode,' +
- 'digitalWrite,digitalRead,analogReference,analogRead,analogWrite,tone,' +
- 'noTone,shiftOut,shitIn,pulseIn,millis,micros,delay,delayMicroseconds,' +
- 'min,max,abs,constrain,map,pow,sqrt,sin,cos,tan,randomSeed,random,' +
- 'lowByte,highByte,bitRead,bitWrite,bitSet,bitClear,bit,attachInterrupt,' +
- 'detachInterrupt,interrupts,noInterrupts');
- Blockly.Python.ORDER_ATOMIC = 0;
- Blockly.Python.ORDER_UNARY_POSTFIX = 1;
- Blockly.Python.ORDER_UNARY_PREFIX = 2;
- Blockly.Python.ORDER_MULTIPLICATIVE = 3;
- Blockly.Python.ORDER_ADDITIVE = 4;
- Blockly.Python.ORDER_SHIFT = 5;
- Blockly.Python.ORDER_RELATIONAL = 6;
- Blockly.Python.ORDER_EQUALITY = 7;
- Blockly.Python.ORDER_BITWISE_AND = 8;
- Blockly.Python.ORDER_BITWISE_XOR = 9;
- Blockly.Python.ORDER_BITWISE_OR = 10;
- Blockly.Python.ORDER_LOGICAL_AND = 11;
- Blockly.Python.ORDER_LOGICAL_OR = 12;
- Blockly.Python.ORDER_CONDITIONAL = 13;
- Blockly.Python.ORDER_ASSIGNMENT = 14;
- Blockly.Python.ORDER_NONE = 99;
- Blockly.Python.PinTypes = {
- INPUT: 'INPUT',
- OUTPUT: 'OUTPUT',
- PWM: 'PWM',
- SERVO: 'SERVO',
- STEPPER: 'STEPPER',
- SERIAL: 'SERIAL',
- I2C: 'I2C/TWI',
- SPI: 'SPI',
-
- FASTLED: 'FASTLED',
- DHT: 'DHT'
- };
- Blockly.Python.DEF_FUNC_NAME = Blockly.Python.FUNCTION_NAME_PLACEHOLDER_;
- Blockly.Python.init = function(workspace) {
-
- Blockly.Python.includes_ = Object.create(null);
-
- Blockly.Python.definitions_ = Object.create(null);
-
- Blockly.Python.variables_ = Object.create(null);
-
- Blockly.Python.codeFunctions_ = Object.create(null);
-
- Blockly.Python.userFunctions_ = Object.create(null);
-
-
- Blockly.Python.functionNames_ = Object.create(null);
-
- Blockly.Python.setups_ = Object.create(null);
-
- Blockly.Python.pins_ = Object.create(null);
- if (!Blockly.Python.variableDB_) {
- Blockly.Python.variableDB_ =
- new Blockly.Names(Blockly.Python.RESERVED_WORDS_);
- } else {
- Blockly.Python.variableDB_.reset();
- }
-
- var varsWithTypes = Blockly.Python.StaticTyping.collectVarsWithTypes(workspace);
- Blockly.Python.StaticTyping.setProcedureArgs(workspace, varsWithTypes);
-
- for (var varName in varsWithTypes) {
- if (Blockly.Python.getArduinoType_(varsWithTypes[varName]) != "array") {
-
- Blockly.Python.addVariable(varName,
- Blockly.Python.getArduinoType_(varsWithTypes[varName]) +' ' +
- Blockly.Python.variableDB_.getName(varName, Blockly.Variables.NAME_TYPE) + ';');
- }
- }
- };
- Blockly.Python.finish = function(code) {
-
- var includes = [], definitions = [], variables = [], functions = [];
- for (var name in Blockly.Python.includes_) {
- includes.push(Blockly.Python.includes_[name]);
- }
- if (includes.length) {
- includes.push('\n');
- }
- for (var name in Blockly.Python.variables_) {
- variables.push(Blockly.Python.variables_[name]);
- }
- if (variables.length) {
- variables.push('\n');
- }
- for (var name in Blockly.Python.definitions_) {
- definitions.push(Blockly.Python.definitions_[name]);
- }
- if (definitions.length) {
- definitions.push('\n');
- }
- for (var name in Blockly.Python.codeFunctions_) {
- functions.push(Blockly.Python.codeFunctions_[name]);
- }
- for (var name in Blockly.Python.userFunctions_) {
- functions.push(Blockly.Python.userFunctions_[name]);
- }
- if (functions.length) {
- functions.push('\n');
- }
-
- var setups = [''], userSetupCode= '';
- if (Blockly.Python.setups_['userSetupCode'] !== undefined) {
- userSetupCode = '\n' + Blockly.Python.setups_['userSetupCode'];
- delete Blockly.Python.setups_['userSetupCode'];
- }
- for (var name in Blockly.Python.setups_) {
- setups.push(Blockly.Python.setups_[name]);
- }
- if (userSetupCode) {
- setups.push(userSetupCode);
- }
-
- delete Blockly.Python.includes_;
- delete Blockly.Python.definitions_;
- delete Blockly.Python.codeFunctions_;
- delete Blockly.Python.userFunctions_;
- delete Blockly.Python.functionNames_;
- delete Blockly.Python.setups_;
- delete Blockly.Python.pins_;
- Blockly.Python.variableDB_.reset();
-
-
-
-
-
- var allDefs = includes.join('\n') + variables.join('\n') +
- definitions.join('\n');
- var setup = 'void setup() {' + setups.join('\n ') + '\n}\n\n';
- var loop = 'void loop() {\n ' + code.replace(/\n/g, '\n ') + '\n}\n\n';
- return allDefs + setup + loop + functions.join('\n');
- };
- Blockly.Python.addInclude = function(includeTag, code) {
- if (Blockly.Python.includes_[includeTag] === undefined) {
- Blockly.Python.includes_[includeTag] = code;
- }
- };
- Blockly.Python.addDeclaration = function(declarationTag, code) {
- if (Blockly.Python.definitions_[declarationTag] === undefined) {
- Blockly.Python.definitions_[declarationTag] = code;
- }
- };
- Blockly.Python.addVariable = function(varName, code, overwrite) {
- var overwritten = false;
- if (overwrite || (Blockly.Python.variables_[varName] === undefined)) {
- Blockly.Python.variables_[varName] = code;
- overwritten = true;
- }
- return overwritten;
- };
- Blockly.Python.addSetup = function(setupTag, code, overwrite) {
- var overwritten = false;
- if (overwrite || (Blockly.Python.setups_[setupTag] === undefined)) {
- Blockly.Python.setups_[setupTag] = code;
- overwritten = true;
- }
- return overwritten;
- };
- Blockly.Python.addFunction = function(preferedName, code) {
- if (Blockly.Python.codeFunctions_[preferedName] === undefined) {
- var uniqueName = Blockly.Python.variableDB_.getDistinctName(
- preferedName, Blockly.Generator.NAME_TYPE);
- Blockly.Python.codeFunctions_[preferedName] =
- code.replace(Blockly.Python.DEF_FUNC_NAME, uniqueName);
- Blockly.Python.functionNames_[preferedName] = uniqueName;
- }
- return Blockly.Python.functionNames_[preferedName];
- };
- Blockly.Python.reservePin = function(block, pin, pinType, warningTag) {
- if (Blockly.Python.pins_[pin] !== undefined) {
- if (Blockly.Python.pins_[pin] != pinType) {
- block.setWarningText(Blockly.Msg.ARD_PIN_WARN1.replace('%1', pin)
- .replace('%2', warningTag).replace('%3', pinType)
- .replace('%4', Blockly.Python.pins_[pin]), warningTag);
- } else {
- block.setWarningText(null, warningTag);
- }
- } else {
- Blockly.Python.pins_[pin] = pinType;
- block.setWarningText(null, warningTag);
- }
- };
- Blockly.Python.scrubNakedValue = function(line) {
- return line + ';\n';
- };
- Blockly.Python.quote_ = function(string) {
-
- string = string.replace(/\\/g, '\\\\')
- .replace(/\n/g, '\\\n')
- .replace(/\$/g, '\\$')
- .replace(/'/g, '\\\'');
- return '\"' + string + '\"';
- };
- Blockly.Python.scrub_ = function(block, code) {
- if (code === null) { return ''; }
- var commentCode = '';
-
- if (!block.outputConnection || !block.outputConnection.targetConnection) {
-
- var comment = block.getCommentText();
- if (comment) {
- commentCode += this.prefixLines(comment, '// ') + '\n';
- }
-
-
- for (var x = 0; x < block.inputList.length; x++) {
- if (block.inputList[x].type == Blockly.INPUT_VALUE) {
- var childBlock = block.inputList[x].connection.targetBlock();
- if (childBlock) {
- var comment = this.allNestedComments(childBlock);
- if (comment) {
- commentCode += this.prefixLines(comment, '// ');
- }
- }
- }
- }
- }
- var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
- var nextCode = this.blockToCode(nextBlock);
- return commentCode + code + nextCode;
- };
- Blockly.Python.getArduinoType_ = function(typeBlockly) {
- switch (typeBlockly.typeId) {
- case Blockly.Types.SHORT_NUMBER.typeId:
- return 'short';
- case Blockly.Types.NUMBER.typeId:
- return 'int';
- case Blockly.Types.LARGE_NUMBER.typeId:
- return 'long';
- case Blockly.Types.DECIMAL.typeId:
- return 'float';
- case Blockly.Types.TEXT.typeId:
- return 'String';
- case Blockly.Types.CHARACTER.typeId:
- return 'char';
- case Blockly.Types.BOOLEAN.typeId:
- return 'boolean';
- case Blockly.Types.NULL.typeId:
- return 'void';
- case Blockly.Types.UNDEF.typeId:
- return 'undefined';
- case Blockly.Types.CHILD_BLOCK_MISSING.typeId:
-
-
- return 'int';
- case Blockly.Types.ARRAY.typeId:
- return 'array';
- default:
- return 'Invalid Blockly Type';
- }
- };
- Blockly.Python.noGeneratorCodeInline = function() {
- return ['', Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Python.noGeneratorCodeLine = function() { return ''; };
|