123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- 'use strict';
- goog.provide('Blockly.Python');
- goog.require('Blockly.Generator');
- Blockly.Python.addReservedWords(
-
-
-
- 'and,as,assert,break,class,continue,def,del,elif,else,except,exec,' +
- 'finally,for,from,global,if,import,in,is,lambda,not,or,pass,print,raise,' +
- 'return,try,while,with,yield,' +
-
- 'True,False,None,NotImplemented,Ellipsis,__debug__,quit,exit,copyright,license,credits,' +
-
-
-
- ''
-
- );
- Blockly.Python.ORDER_ATOMIC = 0;
- Blockly.Python.ORDER_COLLECTION = 1;
- Blockly.Python.ORDER_STRING_CONVERSION = 1;
- Blockly.Python.ORDER_MEMBER = 2.1;
- Blockly.Python.ORDER_FUNCTION_CALL = 2.2;
- Blockly.Python.ORDER_EXPONENTIATION = 3;
- Blockly.Python.ORDER_UNARY_SIGN = 4;
- Blockly.Python.ORDER_BITWISE_NOT = 4;
- Blockly.Python.ORDER_MULTIPLICATIVE = 5;
- Blockly.Python.ORDER_ADDITIVE = 6;
- Blockly.Python.ORDER_BITWISE_SHIFT = 7;
- Blockly.Python.ORDER_BITWISE_AND = 8;
- Blockly.Python.ORDER_BITWISE_XOR = 9;
- Blockly.Python.ORDER_BITWISE_OR = 10;
- Blockly.Python.ORDER_RELATIONAL = 11;
- Blockly.Python.ORDER_LOGICAL_NOT = 12;
- Blockly.Python.ORDER_LOGICAL_AND = 13;
- Blockly.Python.ORDER_LOGICAL_OR = 14;
- Blockly.Python.ORDER_CONDITIONAL = 15;
- Blockly.Python.ORDER_LAMBDA = 16;
- Blockly.Python.ORDER_NONE = 99;
- Blockly.Python.PASS = ' pass\n';
- Blockly.Python.ONE_BASED_INDEXING = false;
- Blockly.Python.ORDER_OVERRIDES = [
-
-
- [Blockly.Python.ORDER_FUNCTION_CALL, Blockly.Python.ORDER_MEMBER],
-
- [Blockly.Python.ORDER_FUNCTION_CALL, Blockly.Python.ORDER_FUNCTION_CALL],
-
-
-
-
- [Blockly.Python.ORDER_MEMBER, Blockly.Python.ORDER_MEMBER],
-
-
- [Blockly.Python.ORDER_MEMBER, Blockly.Python.ORDER_FUNCTION_CALL],
-
- [Blockly.Python.ORDER_LOGICAL_NOT, Blockly.Python.ORDER_LOGICAL_NOT],
-
- [Blockly.Python.ORDER_LOGICAL_AND, Blockly.Python.ORDER_LOGICAL_AND],
-
- [Blockly.Python.ORDER_LOGICAL_OR, Blockly.Python.ORDER_LOGICAL_OR]
- ];
- Blockly.Python.init = function (workspace) {
-
- Blockly.Python.PASS = this.INDENT + 'pass\n';
-
- Blockly.Python.definitions_ = Object.create(null);
-
-
- Blockly.Python.functionNames_ = Object.create(null);
- if (!Blockly.Python.variableDB_) {
- Blockly.Python.variableDB_ =
- new Blockly.Names(Blockly.Python.RESERVED_WORDS_);
- } else {
- Blockly.Python.variableDB_.reset();
- }
-
-
- };
- Blockly.Python.finish = function (code) {
-
- var imports = [];
- var definitions = [];
- for (var name in Blockly.Python.definitions_) {
- var def = Blockly.Python.definitions_[name];
- if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) {
- imports.push(def);
- } else {
- definitions.push(def);
- }
- }
-
- delete Blockly.Python.definitions_;
- delete Blockly.Python.functionNames_;
- Blockly.Python.variableDB_.reset();
- var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n');
- return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code;
- };
- Blockly.Python.scrubNakedValue = function (line) {
- return line + '\n';
- };
- Blockly.Python.quote_ = function (string) {
-
- string = string.replace(/\\/g, '\\\\')
- .replace(/\n/g, '\\\n');
- if (string.indexOf('"') > -1 && string.indexOf('"') == -1) {
- return '\'' + string + '\'';
- } else if (string.indexOf('"') == -1 && string.indexOf('"') > -1) {
- return '"' + string + '"';
- } else {
- string = string.replace(/"/g, '\\\"');
- return '"' + string + '"';
- }
- };
- Blockly.Python.scrub_ = function (block, code) {
- var commentCode = '';
-
- if (!block.outputConnection || !block.outputConnection.targetConnection) {
-
- var comment = block.getCommentText();
- comment = Blockly.utils.wrap(comment, Blockly.Python.COMMENT_WRAP - 3);
- if (comment) {
- if (block.getProcedureDef) {
-
-
-
- } else {
- commentCode += Blockly.Python.prefixLines(comment + '\n', '# ');
- }
- }
-
-
- for (var i = 0; i < block.inputList.length; i++) {
- if (block.inputList[i].type == Blockly.INPUT_VALUE) {
- var childBlock = block.inputList[i].connection.targetBlock();
- if (childBlock) {
- var comment = Blockly.Python.allNestedComments(childBlock);
- if (comment) {
- commentCode += Blockly.Python.prefixLines(comment, '# ');
- }
- }
- }
- }
- }
- var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
- var nextCode = Blockly.Python.blockToCode(nextBlock);
- return commentCode + code + nextCode;
- };
- Blockly.Python.getAdjustedInt = function (block, atId, opt_delta, opt_negate) {
- var delta = opt_delta || 0;
- if (block.workspace.options.oneBasedIndex) {
- delta--;
- }
- var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
- var atOrder = delta ? Blockly.Python.ORDER_ADDITIVE :
- Blockly.Python.ORDER_NONE;
- var at = Blockly.Python.valueToCode(block, atId, atOrder) || defaultAtIndex;
- if (Blockly.isNumber(at)) {
-
- at = parseInt(at, 10) + delta;
- if (opt_negate) {
- at = -at;
- }
- } else {
-
- if (delta > 0) {
- at = 'int(' + at + ' + ' + delta + ')';
- } else if (delta < 0) {
- at = 'int(' + at + ' - ' + -delta + ')';
- } else {
- at = 'int(' + at + ')';
- }
- if (opt_negate) {
- at = '-' + at;
- }
- }
- return at;
- };
- 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();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- };
- 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_) {
- var def = Blockly.Python.definitions_[name];
- if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) {
- includes.push(def);
- } else {
- definitions.push(def);
- }
- }
- 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.variables_;
- 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') + '\n\n' +
- definitions.join('\n') + '\n\n' + functions.join('\n') + '\n\n' + variables.join('\n') + '\n\n';
- var setup = setups.join('\n') + '\n\n';
- var loop = code + '\n\n';
-
- return allDefs.replace(/\n\n+/g, '\n\n') + setup.replace(/\n\n+/g, '\n\n') + loop.replace(/\n\n+/g, '\n\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.variableDB_.dbReverse_[varName] === undefined)) {
- Blockly.Python.variables_[varName] = code;
- Blockly.mainWorkspace.createVariable(varName)
-
-
- 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.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 ''; };
|