Skip to content

Commit

Permalink
added unary operator
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed May 4, 2024
1 parent 31d0f00 commit a277580
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// import * as Blockly from "blockly/core";
import { IBlocksSimple } from "../blocksInterface";
//imported from
//https://github.com/carloslfu/ =>/javascript/blocks/expressions.j
export class UnaryOperator implements IBlocksSimple {
definitionBlocksSimple(blocks: any, javascriptGenerator: any): void {
blocks[UnaryOperator.nameBlock] = {
init: function() {
var OPERATORS = [
['++', '++'],
['--', '--'],
];
this.jsonInit({
"id": UnaryOperator.nameBlock,
"message0": "set %1 %2",
"args0": [
{
"type": "input_value",
"name": "VAR"
},
{
"type": "field_dropdown",
"name": "OPERATOR",
"options": OPERATORS
}
],
"colour": 330,
"previousStatement": "Statement",
"nextStatement": "Statement",
"inputsInline": true,
"tooltip": "Assignment expression."
});
}
};

javascriptGenerator[UnaryOperator.nameBlock] = function(block:any) {
var variable = javascriptGenerator.valueToCode(block, 'VAR',
javascriptGenerator.ORDER_ASSIGNMENT);
var operator = block.getFieldValue('OPERATOR');
var code = variable + ' ' + operator ;
//If has output returns a tuple with the order of precedence
if (block.outputConnection) {
return [code, javascriptGenerator.ORDER_ASSIGNMENT];
} else {
return code + ';\n';
}
};

}
addWrapper(interpreter: any, globalObject: any) {

}
fieldXML(): string {
return `<block type="${UnaryOperator.nameBlock}">
</block>`;
}
category: string='MoreMath';
public static nameBlock: string = "unaryOperator";
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ export class MoreOperators implements IBlocksSimple {
return `<block type="${MoreOperators.nameBlock}">
</block>`;
}
category: string='other';
category: string='MoreMath';
public static nameBlock: string = "js_assignment_expression";
}
4 changes: 3 additions & 1 deletion src/blockly10/src/BlocklyReusable/allNewBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { SwitchBlock } from "./BlocklyNewBlocks/switch";
import { SpecialCharBlock } from "./BlocklyNewBlocks/specialChar";
import { MoreOperators } from "./BlocklyNewBlocks/moreOperators";
import { comment } from "./BlocklyNewBlocks/meta/comment";
import { UnaryOperator } from "./BlocklyNewBlocks/UnaryOperator";

export default class AllNewBlocks
{
Expand Down Expand Up @@ -71,7 +72,8 @@ export default class AllNewBlocks
new SwitchBlock(),
new SpecialCharBlock(),
new MoreOperators(),
new comment()
new comment(),
new UnaryOperator()
];
return this.nb;
}
Expand Down

0 comments on commit a277580

Please sign in to comment.