初始化提交
This commit is contained in:
1512
boards/default_src/python_skulpt/converters/data.js
Normal file
1512
boards/default_src/python_skulpt/converters/data.js
Normal file
File diff suppressed because it is too large
Load Diff
74
boards/default_src/python_skulpt/converters/inout.js
Normal file
74
boards/default_src/python_skulpt/converters/inout.js
Normal file
@@ -0,0 +1,74 @@
|
||||
'use strict';
|
||||
|
||||
pbc.globalFunctionD['input'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length !== 1 && args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
if (args.length == 1){
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return block("inout_type_input", func.lineno, {
|
||||
"DIR":"str"
|
||||
}, {
|
||||
'VAR':argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});}
|
||||
if (args.length == 0){
|
||||
|
||||
return block("inout_type_input", func.lineno, {
|
||||
"DIR":"str"
|
||||
}, {
|
||||
//'VAR':argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});}
|
||||
}
|
||||
|
||||
|
||||
//int(input('prompt'))在math.js中实现
|
||||
//float(input('prompt'))在lists.js中实现
|
||||
|
||||
pbc.globalFunctionD['print'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length === 1 && keywords.length === 1
|
||||
&& py2block.identifier(keywords[0].arg) === "end"
|
||||
&& keywords[0].value._astname === "Str"
|
||||
//&& py2block.Str_value(keywords[0].value) === ""
|
||||
) { if(py2block.Str_value(keywords[0].value) === ""){//print('Hello',end ="")
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block("inout_print_inline", func.lineno, {}, {
|
||||
'VAR':argblock
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}
|
||||
else{
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block("inout_print_end", func.lineno, {
|
||||
}, {
|
||||
'VAR':argblock,
|
||||
'END':py2block.convert(keywords[0].value)
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}else if (args.length === 1 && keywords.length === 0) { //print('Hello')
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block("inout_print", func.lineno, {}, {
|
||||
'VAR':argblock
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}else if (args.length != 1 && keywords.length === 0) { //print()
|
||||
var d = py2block.convertElements("ADD", args);
|
||||
|
||||
return [block("inout_print_many", node.lineno, {
|
||||
}, d, {
|
||||
"inline": "true",
|
||||
}, {
|
||||
"@items":args.length
|
||||
})];
|
||||
|
||||
}else{
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
}
|
||||
162
boards/default_src/python_skulpt/converters/iot.js
Normal file
162
boards/default_src/python_skulpt/converters/iot.js
Normal file
@@ -0,0 +1,162 @@
|
||||
'use strict';
|
||||
|
||||
var mqtt_client = 'mixiot.MixIO';
|
||||
pbc.assignD.get('MixIO_init_by_mixly_key')['check_assign'] = function(py2block, node, targets, value) {
|
||||
if(value._astname != "Call" || value.func._astname != "Attribute" || value.func.value._astname != "Name"){
|
||||
return false;
|
||||
}
|
||||
var funcName = py2block.identifier(value.func.attr);
|
||||
var moduleName = py2block.identifier(value.func.value.id);
|
||||
if(moduleName === "mixiot" && funcName === "MixIO_init_by_mixly_key" && value.args.length === 4)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
pbc.assignD.get('MixIO_init_by_mixly_key')['create_block'] = function(py2block, node, targets, value){
|
||||
var arg0block = py2block.convert(value.args[0]);
|
||||
var arg2block = py2block.convert(value.args[2]);
|
||||
return block("IOT_EMQX_INIT_AND_CONNECT_BY_MIXLY_CODE", node.lineno, {}, {
|
||||
'SERVER': arg0block,
|
||||
'KEY':arg2block,
|
||||
});
|
||||
}
|
||||
|
||||
pbc.assignD.get('MixIO_init_by_share_key')['check_assign'] = function(py2block, node, targets, value) {
|
||||
if(value._astname != "Call" || value.func._astname != "Attribute" || value.func.value._astname != "Name"){
|
||||
return false;
|
||||
}
|
||||
var funcName = py2block.identifier(value.func.attr);
|
||||
var moduleName = py2block.identifier(value.func.value.id);
|
||||
if(moduleName === "mixiot" && funcName === "MixIO_init_by_share_key" && value.args.length === 4)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
pbc.assignD.get('MixIO_init_by_share_key')['create_block'] = function(py2block, node, targets, value){
|
||||
var arg0block = py2block.convert(value.args[0]);
|
||||
var arg2block = py2block.convert(value.args[2]);
|
||||
return block("IOT_EMQX_INIT_AND_CONNECT_BY_SHARE_CODE", node.lineno, {}, {
|
||||
'SERVER': arg0block,
|
||||
'KEY':arg2block,
|
||||
});
|
||||
}
|
||||
|
||||
pbc.assignD.get('MixIO')['check_assign'] = function(py2block, node, targets, value) {
|
||||
if(value._astname != "Call" || value.func._astname != "Attribute" || value.func.value._astname != "Name"){
|
||||
return false;
|
||||
}
|
||||
var funcName = py2block.identifier(value.func.attr);
|
||||
var moduleName = py2block.Name_str(value.func.value);
|
||||
if(moduleName === "mixiot" && funcName === "MixIO" && value.args.length === 6)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
pbc.assignD.get('MixIO')['create_block'] = function(py2block, node, targets, value){
|
||||
var arg0block = py2block.convert(value.args[0]);
|
||||
var arg2block = py2block.convert(value.args[2]);
|
||||
var arg3block = py2block.convert(value.args[3]);
|
||||
var arg4block = py2block.convert(value.args[4]);
|
||||
return block("iot_mixio_connect", node.lineno, {}, {
|
||||
'SERVER': arg0block,
|
||||
'USERNAME':arg2block,
|
||||
'PASSWORD': arg3block,
|
||||
'PROJECT':arg4block
|
||||
});
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('publish')[mqtt_client] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 2) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock = py2block.convert(args[0]);
|
||||
var arg1block = py2block.convert(args[1]);
|
||||
return [block('IOT_MIXIO_PUBLISH', func.lineno, {}, {
|
||||
'TOPIC': argblock,
|
||||
'MSG': arg1block
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('unsubscribe')[mqtt_client] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block('IOT_MIXIO_UNSUBSCRIBE', func.lineno, {}, {
|
||||
'TOPIC': argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('subscribe')[mqtt_client] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 2) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock = py2block.convert(args[0]);
|
||||
var arg1block = py2block.convert(args[1]);
|
||||
return [block('IOT_MIXIO_SUBSCRIBE', func.lineno, {}, {
|
||||
'TOPIC': argblock,
|
||||
'METHOD': arg1block
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('check_msg')[mqtt_client] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
|
||||
return [block('iot_mixio_check', func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('connect')[mqtt_client] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
|
||||
return [block('iot_mixio_connect_only', func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('disconnect')[mqtt_client] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args != null) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
|
||||
return [block('iot_mixio_disconnect', func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('mixpy')['format_content'] = function(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 2) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock = py2block.convert(args[0]);
|
||||
|
||||
return block('IOT_FORMATTING', func.lineno, {}, {
|
||||
'VAR': argblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('mixpy')['format_str'] = function(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock = py2block.convert(args[0]);
|
||||
|
||||
return block('IOT_FORMAT_STRING', func.lineno, {}, {
|
||||
'VAR': argblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
function defDict(type) {
|
||||
var dict = {};
|
||||
return {
|
||||
get: function (key) {
|
||||
if (!dict[key]) {
|
||||
dict[key] = type.constructor();
|
||||
}
|
||||
return dict[key];
|
||||
},
|
||||
dict: dict
|
||||
};
|
||||
}
|
||||
|
||||
function Py2blockConfig (){
|
||||
this.initIgnoreS();
|
||||
this.initModuleAttrD();
|
||||
this.initKnownModuleS();
|
||||
this.initObjectTypeD();
|
||||
}
|
||||
|
||||
var pbc = Py2blockConfig.prototype;
|
||||
pbc.MIXPY = "MIXPY";
|
||||
pbc.board = pbc.MIXPY;
|
||||
pbc.objectFunctionD = defDict({});
|
||||
pbc.moduleFunctionD = defDict({});
|
||||
pbc.moduleAttrD = defDict({});
|
||||
pbc.objectAttrD = defDict({});
|
||||
pbc.globalFunctionD = {};
|
||||
pbc.assignD = defDict({});
|
||||
pbc.ifStatementD= defDict({});
|
||||
pbc.whileStatementD= defDict({});
|
||||
pbc.forStatementD= defDict({});
|
||||
pbc.reservedNameD= {};
|
||||
pbc.knownModuleS = new Set();
|
||||
pbc.objectTypeD = {}; //key:变量名,value:变量类型,如{'a':'List'}
|
||||
pbc.ignoreS = new Set();
|
||||
pbc.pinType = null;
|
||||
pbc.inScope = null;
|
||||
pbc.formatModuleKeyL = [];
|
||||
pbc.formatModuleL = [];
|
||||
|
||||
//忽略某些方法、类、赋值
|
||||
pbc.initIgnoreS = function(){
|
||||
var pythonIgnoreL = [
|
||||
];
|
||||
var boardIgnoreL = [];
|
||||
|
||||
var ignoreL = pythonIgnoreL.concat(boardIgnoreL);
|
||||
for (var i = 0; i < ignoreL.length; i++) {
|
||||
this.ignoreS.add(ignoreL[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pbc.initModuleAttrD = function(){
|
||||
}
|
||||
|
||||
pbc.initKnownModuleS = function(){
|
||||
var pythonModuleL = [
|
||||
'math', 'random'
|
||||
];
|
||||
var boardModuleL = [];
|
||||
|
||||
var moduleL = pythonModuleL.concat(boardModuleL);
|
||||
for (var i = 0; i < moduleL.length; i++) {
|
||||
this.knownModuleS.add(moduleL[i]);
|
||||
}
|
||||
}
|
||||
|
||||
pbc.initObjectTypeD = function () {
|
||||
this.objectTypeD = {
|
||||
'tina': 'turtle.Turtle',
|
||||
'f': 'open'
|
||||
}
|
||||
}
|
||||
|
||||
pbc.reset = function(){
|
||||
this.initObjectTypeD();
|
||||
}
|
||||
|
||||
var py2block_config = new Py2blockConfig();
|
||||
|
||||
45
boards/default_src/python_skulpt/converters/system.js
Normal file
45
boards/default_src/python_skulpt/converters/system.js
Normal file
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
pbc.globalFunctionD['exit'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("controls_end_program", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('time')['time'] = function(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("controls_millis", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('time')['localtime'] = function(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("time_localtime", func.lineno, {
|
||||
"op":'all'
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('time')['sleep'] = function(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block("time_sleep", func.lineno, {}, {
|
||||
|
||||
"DELAY_TIME":argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
1127
boards/default_src/python_skulpt/converters/turtle.js
Normal file
1127
boards/default_src/python_skulpt/converters/turtle.js
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user