Skip to content

Commit

Permalink
Basic syntax support complete for all AST nodes!
Browse files Browse the repository at this point in the history
  • Loading branch information
acbart committed Jul 19, 2019
1 parent 42b5762 commit 83656e9
Show file tree
Hide file tree
Showing 20 changed files with 1,704 additions and 251 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/acbart.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

508 changes: 297 additions & 211 deletions .idea/workspace.xml

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/ast/ast_Break.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
BlockMirrorTextToBlocks.BLOCKS.push({
"type": "ast_Break",
"message0": "break",
"inputsInline": false,
"previousStatement": null,
"nextStatement": null,
"colour": 60,
"tooltip": "",
"helpUrl": ""
});

Blockly.Python['ast_Break'] = function (block) {
return "break\n";
};

BlockMirrorTextToBlocks.prototype['ast_Break'] = function (node) {
return BlockMirrorTextToBlocks.create_block("ast_Break", node.lineno);
};
6 changes: 2 additions & 4 deletions src/ast/ast_Call.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ Blockly.Blocks['ast_Call'] = {
container.appendChild(parameter);
}
return container;
}
,
},
/**
* Parse XML to restore the (non-editable) name and parameters.
* @param {!Element} xmlElement XML storage element.
Expand All @@ -275,8 +274,7 @@ Blockly.Blocks['ast_Call'] = {
if (this.simpleName_ !== null) {
this.renameProcedure(this.getProcedureCall(), this.simpleName_);
}
}
,
},
/**
* Return all variables referenced by this block.
* @return {!Array.<!Blockly.VariableModel>} List of variable models.
Expand Down
156 changes: 156 additions & 0 deletions src/ast/ast_ClassDef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
Blockly.Blocks['ast_ClassDef'] = {
init: function () {
this.decorators_ = 0;
this.bases_ = 0;
this.keywords_ = 0;
this.appendDummyInput('HEADER')
.appendField("Class")
.appendField(new Blockly.FieldVariable("item"), "NAME");
this.appendStatementInput("BODY")
.setCheck(null);
this.setInputsInline(false);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(230);
this.setTooltip("");
this.setHelpUrl("");
this.updateShape_();
},
// TODO: Not mutable currently
updateShape_: function () {
for (let i = 0; i < this.decorators_; i++) {
let input = this.appendValueInput("DECORATOR" + i)
.setCheck(null)
.setAlign(Blockly.ALIGN_RIGHT);
if (i === 0) {
input.appendField("decorated by");
}
this.moveInputBefore('DECORATOR' + i, 'BODY');
}
for (let i = 0; i < this.bases_; i++) {
let input = this.appendValueInput("BASE" + i)
.setCheck(null)
.setAlign(Blockly.ALIGN_RIGHT);
if (i === 0) {
input.appendField("inherits from");
}
this.moveInputBefore('BASE' + i, 'BODY');
}

for (let i = 0; i < this.keywords_; i++) {
this.appendValueInput("KEYWORDVALUE" + i)
.setCheck(null)
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(new Blockly.FieldTextInput("metaclass"), "KEYWORDNAME" + i)
.appendField("=");
this.moveInputBefore('KEYWORDVALUE' + i, 'BODY');
}
},
/**
* Create XML to represent the (non-editable) name and arguments.
* @return {!Element} XML storage element.
* @this Blockly.Block
*/
mutationToDom: function () {
let container = document.createElement('mutation');
container.setAttribute('decorators', this.decorators_);
container.setAttribute('bases', this.bases_);
container.setAttribute('keywords', this.keywords_);
return container;
},
/**
* Parse XML to restore the (non-editable) name and parameters.
* @param {!Element} xmlElement XML storage element.
* @this Blockly.Block
*/
domToMutation: function (xmlElement) {
this.decorators_ = parseInt(xmlElement.getAttribute('decorators'), 10);
this.bases_ = parseInt(xmlElement.getAttribute('bases'), 10);
this.keywords_ = parseInt(xmlElement.getAttribute('keywords'), 10);
this.updateShape_();
},
};

Blockly.Python['ast_ClassDef'] = function (block) {
// Name
let name = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'), Blockly.Variables.NAME_TYPE);
// Decorators
let decorators = new Array(block.decorators_);
for (let i = 0; i < block.decorators_; i++) {
let decorator = (Blockly.Python.valueToCode(block, 'DECORATOR' + i, Blockly.Python.ORDER_NONE) ||
Blockly.Python.blank);
decorators[i] = "@" + decorator + "\n";
}
// Bases
let bases = new Array(block.bases_);
for (let i = 0; i < block.bases_; i++) {
bases[i] = (Blockly.Python.valueToCode(block, 'BASE' + i, Blockly.Python.ORDER_NONE) ||
Blockly.Python.blank);
}
// Keywords
let keywords = new Array(block.keywords_);
for (let i = 0; i < block.keywords_; i++) {
let name = block.getFieldValue('KEYWORDNAME' + i);
let value = (Blockly.Python.valueToCode(block, 'KEYWORDVALUE' + i, Blockly.Python.ORDER_NONE) ||
Blockly.Python.blank);
if (name == '**') {
keywords[i] = '**' + value;
} else {
keywords[i] = name + '=' + value;
}
}
// Body:
let body = Blockly.Python.statementToCode(block, 'BODY') || Blockly.Python.PASS;
// Put it together
let arguments = (bases.concat(keywords));
arguments = (arguments.length === 0) ? "" : "(" + arguments.join(', ') + ")";
return decorators.join('') + "class " + name + arguments + ":\n" + body;
}
;

BlockMirrorTextToBlocks.prototype['ast_ClassDef'] = function (node) {
let name = node.name;
let bases = node.bases;
let keywords = node.keywords;
let body = node.body;
let decorator_list = node.decorator_list;

let values = {};
let fields = {'NAME': Sk.ffi.remapToJs(name)};

if (decorator_list !== null) {
for (let i = 0; i < decorator_list.length; i++) {
values['DECORATOR' + i] = this.convert(decorator_list[i]);
}
}

if (bases !== null) {
for (let i = 0; i < bases.length; i++) {
values['BASE' + i] = this.convert(bases[i]);
}
}

if (keywords !== null) {
for (let i = 0; i < keywords.length; i++) {
values['KEYWORDVALUE' + i] = this.convert(keywords[i].value);
let arg = keywords[i].arg;
if (arg === null) {
fields['KEYWORDNAME' + i] = "**";
} else {
fields['KEYWORDNAME' + i] = Sk.ffi.remapToJs(arg);
}
}
}

return BlockMirrorTextToBlocks.create_block("ast_ClassDef", node.lineno, fields,
values,
{
"inline": "false"
}, {
"@decorators": (decorator_list === null ? 0 : decorator_list.length),
"@bases": (bases === null ? 0 : bases.length),
"@keywords": (keywords === null ? 0 : keywords.length),
}, {
'BODY': this.convertBody(body)
});
};
18 changes: 18 additions & 0 deletions src/ast/ast_Continue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
BlockMirrorTextToBlocks.BLOCKS.push({
"type": "ast_Continue",
"message0": "continue",
"inputsInline": false,
"previousStatement": null,
"nextStatement": null,
"colour": 60,
"tooltip": "",
"helpUrl": ""
});

Blockly.Python['ast_Continue'] = function (block) {
return "continue\n";
};

BlockMirrorTextToBlocks.prototype['ast_Continue'] = function (node) {
return BlockMirrorTextToBlocks.create_block("ast_Continue", node.lineno);
};
Loading

0 comments on commit 83656e9

Please sign in to comment.