123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- 'use strict';
- goog.provide('Blockly.Pseudo');
- goog.require('Blockly.Generator');
- Blockly.Pseudo = new Blockly.Generator('Pseudo');
- Blockly.Pseudo.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,' +
-
- 'crime,stocks,earthquakes,books,weather,plt,math,'+
-
- 'abs,divmod,input,open,staticmethod,all,enumerate,int,ord,str,any,eval,isinstance,pow,sum,basestring,execfile,issubclass,print,super,bin,file,iter,property,tuple,bool,filter,len,range,type,bytearray,float,list,raw_input,unichr,callable,format,locals,reduce,unicode,chr,frozenset,long,reload,vars,classmethod,getattr,map,repr,xrange,cmp,globals,max,reversed,zip,compile,hasattr,memoryview,round,__import__,complex,hash,min,set,apply,delattr,help,next,setattr,buffer,dict,hex,object,slice,coerce,dir,id,oct,sorted,intern');
- Blockly.Pseudo.ORDER_ATOMIC = 0;
- Blockly.Pseudo.ORDER_COLLECTION = 1;
- Blockly.Pseudo.ORDER_STRING_CONVERSION = 1;
- Blockly.Pseudo.ORDER_MEMBER = 2;
- Blockly.Pseudo.ORDER_FUNCTION_CALL = 2;
- Blockly.Pseudo.ORDER_EXPONENTIATION = 3;
- Blockly.Pseudo.ORDER_UNARY_SIGN = 4;
- Blockly.Pseudo.ORDER_BITWISE_NOT = 4;
- Blockly.Pseudo.ORDER_MULTIPLICATIVE = 5;
- Blockly.Pseudo.ORDER_ADDITIVE = 6;
- Blockly.Pseudo.ORDER_BITWISE_SHIFT = 7;
- Blockly.Pseudo.ORDER_BITWISE_AND = 8;
- Blockly.Pseudo.ORDER_BITWISE_XOR = 9;
- Blockly.Pseudo.ORDER_BITWISE_OR = 10;
- Blockly.Pseudo.ORDER_RELATIONAL = 11;
-
- Blockly.Pseudo.ORDER_LOGICAL_NOT = 12;
- Blockly.Pseudo.ORDER_LOGICAL_AND = 13;
- Blockly.Pseudo.ORDER_LOGICAL_OR = 14;
- Blockly.Pseudo.ORDER_CONDITIONAL = 15;
- Blockly.Pseudo.ORDER_LAMBDA = 16;
- Blockly.Pseudo.ORDER_NONE = 99;
- Blockly.Pseudo.PASS = ' Do nothing.\n';
- Blockly.Pseudo.init = function(workspace) {
-
- Blockly.Pseudo.definitions_ = Object.create(null);
-
-
- Blockly.Pseudo.functionNames_ = Object.create(null);
- if (!Blockly.Pseudo.variableDB_) {
- Blockly.Pseudo.variableDB_ =
- new Blockly.Names(Blockly.Pseudo.RESERVED_WORDS_);
- } else {
- Blockly.Pseudo.variableDB_.reset();
- }
-
-
- };
- Blockly.Pseudo.finish = function(code) {
-
- var imports = [];
- var definitions = [];
- for (var name in Blockly.Pseudo.definitions_) {
- var def = Blockly.Pseudo.definitions_[name];
- if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) {
- imports.push(def);
- } else {
- definitions.push(def);
- }
- }
-
- delete Blockly.Pseudo.definitions_;
- delete Blockly.Pseudo.functionNames_;
- Blockly.Pseudo.variableDB_.reset();
- var allDefs = imports.join('\n') + '\n' + definitions.join('\n\n');
- return allDefs.replace(/\n\n+/g, '\n').replace(/\n*$/, '\n\n') + code;
- };
- Blockly.Pseudo.scrubNakedValue = function(line) {
- return line + '\n';
- };
- Blockly.Pseudo.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.Pseudo.scrub_ = function(block, code) {
- var commentCode = '';
-
- if (!block.outputConnection || !block.outputConnection.targetConnection) {
-
- var comment = block.getCommentText();
- if (comment) {
- commentCode += Blockly.Pseudo.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 = Blockly.Pseudo.allNestedComments(childBlock);
- if (comment) {
- commentCode += Blockly.Pseudo.prefixLines(comment, '# ');
- }
- }
- }
- }
- }
- var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
- var nextCode = Blockly.Pseudo.blockToCode(nextBlock);
- return commentCode + code + nextCode;
- };
|