Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/lib/ruby-generator/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,47 @@
*/
export default function (Blockly) {
Blockly.Ruby.control_wait = function (block) {
const secs = Blockly.Ruby.valueToCode(block, 'DURATION', Blockly.Ruby.ORDER_NONE) || '0';
const secs = Blockly.Ruby.valueToCode(block, 'DURATION', Blockly.Ruby.ORDER_NONE) || 0;
return `sleep(${secs})\n`;
};

Blockly.Ruby.control_repeat = function (block) {
const times = Blockly.Ruby.valueToCode(block, 'TIMES', Blockly.Ruby.ORDER_NONE) || '0';
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '\n';

const times = Blockly.Ruby.valueToCode(block, 'TIMES', Blockly.Ruby.ORDER_NONE) || 0;
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '';
return `repeat(${times}) do\n${branch}end\n`;
};

Blockly.Ruby.control_forever = function (block) {
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '\n';
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '';
return `forever do\n${branch}end\n`;
};

Blockly.Ruby.control_if = function (block) {
const operator = Blockly.Ruby.valueToCode(block, 'CONDITION', Blockly.Ruby.ORDER_NONE) || 'false';
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '\n';
const operator = Blockly.Ruby.valueToCode(block, 'CONDITION', Blockly.Ruby.ORDER_NONE) || false;
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '';
return `if ${operator}\n${branch}end\n`;
};

Blockly.Ruby.control_if_else = function (block) {
const operator = Blockly.Ruby.valueToCode(block, 'CONDITION', Blockly.Ruby.ORDER_NONE) || 'false';
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '\n';
const branch2 = Blockly.Ruby.statementToCode(block, 'SUBSTACK2') || '\n';
const operator = Blockly.Ruby.valueToCode(block, 'CONDITION', Blockly.Ruby.ORDER_NONE) || false;
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '';
const branch2 = Blockly.Ruby.statementToCode(block, 'SUBSTACK2') || '';
return `if ${operator}\n${branch}else\n${branch2}end\n`;
};

Blockly.Ruby.control_wait_until = function (block) {
const operator = Blockly.Ruby.valueToCode(block, 'CONDITION', Blockly.Ruby.ORDER_NONE) || 'false';
const operator = Blockly.Ruby.valueToCode(block, 'CONDITION', Blockly.Ruby.ORDER_NONE) || false;
return `wait until ${operator}\n`;
};

Blockly.Ruby.control_repeat_until = function (block) {
const operator = Blockly.Ruby.valueToCode(block, 'CONDITION', Blockly.Ruby.ORDER_NONE) || 'false';
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '\n';
return `until ${operator}\n${branch}end\n`;
const operator = Blockly.Ruby.valueToCode(block, 'CONDITION', Blockly.Ruby.ORDER_NONE) || false;
const branch = Blockly.Ruby.statementToCode(block, 'SUBSTACK') || '';
return `until ${operator}\n${branch} wait\nend\n`;
};

Blockly.Ruby.control_stop = function (block) {
const target = Blockly.Ruby.quote_(block.getFieldValue('STOP_OPTION') || null);
const target = Blockly.Ruby.quote_(block.getFieldValue('STOP_OPTION') || 'all');
return `stop(${target})\n`;
};

Expand All @@ -56,12 +55,12 @@ export default function (Blockly) {
};

Blockly.Ruby.control_create_clone_of = function (block) {
const target = Blockly.Ruby.valueToCode(block, 'CLONE_OPTION', Blockly.Ruby.ORDER_NONE) || null;
const target = Blockly.Ruby.valueToCode(block, 'CLONE_OPTION', Blockly.Ruby.ORDER_NONE);
return `create_clone(${target})\n`;
};

Blockly.Ruby.control_create_clone_of_menu = function (block) {
const target = Blockly.Ruby.quote_(block.getFieldValue('CLONE_OPTION') || null);
const target = Blockly.Ruby.quote_(block.getFieldValue('CLONE_OPTION') || '_myself_');
return [target, Blockly.Ruby.ORDER_ATOMIC];
};

Expand Down
23 changes: 4 additions & 19 deletions src/lib/ruby-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export default function (Blockly) {
return (typeof s === 'string' || s instanceof String);
};

Blockly.Ruby.isWhiteSpace = function (s) {
return s === null || (typeof s === 'string' && s.trim().length === 0);
};

Blockly.Ruby.scalarToCode = function (scalar) {
if (this.isString(scalar)) {
return this.quote_(scalar);
Expand Down Expand Up @@ -491,25 +495,6 @@ export default function (Blockly) {
return this.blockToCode_(block);
};

Blockly.Ruby.SpecialSymbols = [
'_myself_',
'_mouse_',
'_edge_',
'_random_',
'_stage_'
];

Blockly.Ruby.isSpecialSymbol = function (name) {
return this.SpecialSymbols.includes(name);
};

Blockly.Ruby.specialSymbolToCode = function (name) {
if (this.isSpecialSymbol(name)) {
return `:${name}`;
}
return name;
};

Blockly = GeneratedBlocks(Blockly);

Blockly = MathBlocks(Blockly);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/ruby-generator/looks.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ export default function (Blockly) {
};

Blockly.Ruby.looks_gotofrontback = function (block) {
const frontBack = block.getFieldValue('FRONT_BACK') || null;
const frontBack = block.getFieldValue('FRONT_BACK') || 'front';
return `go_to_layer(:${frontBack})\n`;
};

Blockly.Ruby.looks_goforwardbackwardlayers = function (block) {
const layer = Blockly.Ruby.valueToCode(block, 'NUM', Blockly.Ruby.ORDER_NONE) || '0';
const forwardBackward = block.getFieldValue('FORWARD_BACKWARD') || null;
const layer = Blockly.Ruby.valueToCode(block, 'NUM', Blockly.Ruby.ORDER_NONE) || 0;
const forwardBackward = block.getFieldValue('FORWARD_BACKWARD') || 'forward';
return `go_layers(${layer}, :${forwardBackward})\n`;
};

Blockly.Ruby.looks_costumenumbername = function (block) {
const numberName = block.getFieldValue('NUMBER_NAME') || null;
const numberName = block.getFieldValue('NUMBER_NAME') || 'number';
return [`costume_${numberName}`, Blockly.Ruby.ORDER_ATOMIC];
};

Blockly.Ruby.looks_backdropnumbername = function (block) {
const numberName = block.getFieldValue('NUMBER_NAME') || null;
const numberName = block.getFieldValue('NUMBER_NAME') || 'number';
return [`backdrop_${numberName}`, Blockly.Ruby.ORDER_ATOMIC];
};

Expand Down
48 changes: 24 additions & 24 deletions src/lib/ruby-generator/motion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,93 @@
*/
export default function (Blockly) {
Blockly.Ruby.motion_movesteps = function (block) {
const steps = Blockly.Ruby.valueToCode(block, 'STEPS', Blockly.Ruby.ORDER_NONE) || '0';
const steps = Blockly.Ruby.valueToCode(block, 'STEPS', Blockly.Ruby.ORDER_NONE) || 0;
return `move(${steps})\n`;
};

Blockly.Ruby.motion_turnright = function (block) {
const degrees = Blockly.Ruby.valueToCode(block, 'DEGREES', Blockly.Ruby.ORDER_NONE) || '0';
const degrees = Blockly.Ruby.valueToCode(block, 'DEGREES', Blockly.Ruby.ORDER_NONE) || 0;
return `turn_right(${degrees})\n`;
};

Blockly.Ruby.motion_turnleft = function (block) {
const degrees = Blockly.Ruby.valueToCode(block, 'DEGREES', Blockly.Ruby.ORDER_NONE) || null;
const degrees = Blockly.Ruby.valueToCode(block, 'DEGREES', Blockly.Ruby.ORDER_NONE) || 0;
return `turn_left(${degrees})\n`;
};

Blockly.Ruby.motion_goto = function (block) {
const place = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_NONE) || '0';
const place = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_NONE);
return `go_to(${place})\n`;
};

Blockly.Ruby.motion_goto_menu = function (block) {
const place = block.getFieldValue('TO') || null;
return [Blockly.Ruby.specialSymbolToCode(place), Blockly.Ruby.ORDER_ATOMIC];
const place = Blockly.Ruby.quote_(block.getFieldValue('TO') || null);
return [place, Blockly.Ruby.ORDER_ATOMIC];
};

Blockly.Ruby.motion_gotoxy = function (block) {
const x = Blockly.Ruby.valueToCode(block, 'X', Blockly.Ruby.ORDER_NONE) || '0';
const y = Blockly.Ruby.valueToCode(block, 'Y', Blockly.Ruby.ORDER_NONE) || '0';
const x = Blockly.Ruby.valueToCode(block, 'X', Blockly.Ruby.ORDER_NONE) || 0;
const y = Blockly.Ruby.valueToCode(block, 'Y', Blockly.Ruby.ORDER_NONE) || 0;
return `go_to([${x}, ${y}])\n`;
};

Blockly.Ruby.motion_glideto = function (block) {
const secs = Blockly.Ruby.valueToCode(block, 'SECS', Blockly.Ruby.ORDER_NONE) || '0';
const place = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_NONE) || '0';
const secs = Blockly.Ruby.valueToCode(block, 'SECS', Blockly.Ruby.ORDER_NONE) || 0;
const place = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_NONE);
return `glide(${place}, secs: ${secs})\n`;
};

Blockly.Ruby.motion_glideto_menu = Blockly.Ruby.motion_goto_menu;

Blockly.Ruby.motion_glidesecstoxy = function (block) {
const secs = Blockly.Ruby.valueToCode(block, 'SECS', Blockly.Ruby.ORDER_NONE) || '0';
const x = Blockly.Ruby.valueToCode(block, 'X', Blockly.Ruby.ORDER_NONE) || '0';
const y = Blockly.Ruby.valueToCode(block, 'Y', Blockly.Ruby.ORDER_NONE) || '0';
const secs = Blockly.Ruby.valueToCode(block, 'SECS', Blockly.Ruby.ORDER_NONE) || 0;
const x = Blockly.Ruby.valueToCode(block, 'X', Blockly.Ruby.ORDER_NONE) || 0;
const y = Blockly.Ruby.valueToCode(block, 'Y', Blockly.Ruby.ORDER_NONE) || 0;
return `glide([${x}, ${y}], secs: ${secs})\n`;
};

Blockly.Ruby.motion_pointindirection = function (block) {
const direction = Blockly.Ruby.valueToCode(block, 'DIRECTION', Blockly.Ruby.ORDER_NONE) || '0';
return `point_in_direction(${direction})\n`;
const direction = Blockly.Ruby.valueToCode(block, 'DIRECTION', Blockly.Ruby.ORDER_NONE) || 90;
return `self.direction = ${direction}\n`;
};

Blockly.Ruby.motion_pointtowards = function (block) {
const towards = Blockly.Ruby.valueToCode(block, 'TOWARDS', Blockly.Ruby.ORDER_NONE) || null;
const towards = Blockly.Ruby.valueToCode(block, 'TOWARDS', Blockly.Ruby.ORDER_NONE);
return `point_towards(${towards})\n`;
};

Blockly.Ruby.motion_pointtowards_menu = function (block) {
const towards = Blockly.Ruby.quote_(block.getFieldValue('TOWARDS') || null);
const towards = Blockly.Ruby.quote_(block.getFieldValue('TOWARDS') || '_mouse_');
return [towards, Blockly.Ruby.ORDER_ATOMIC];
};

Blockly.Ruby.motion_changexby = function (block) {
const dx = Blockly.Ruby.valueToCode(block, 'DX', Blockly.Ruby.ORDER_NONE) || '0';
const dx = Blockly.Ruby.valueToCode(block, 'DX', Blockly.Ruby.ORDER_NONE) || 0;
return `self.x += ${dx}\n`;
};

Blockly.Ruby.motion_setx = function (block) {
const x = Blockly.Ruby.valueToCode(block, 'X', Blockly.Ruby.ORDER_NONE) || '0';
const x = Blockly.Ruby.valueToCode(block, 'X', Blockly.Ruby.ORDER_NONE) || 0;
return `self.x = ${x}\n`;
};

Blockly.Ruby.motion_changeyby = function (block) {
const dy = Blockly.Ruby.valueToCode(block, 'DY', Blockly.Ruby.ORDER_NONE) || '0';
const dy = Blockly.Ruby.valueToCode(block, 'DY', Blockly.Ruby.ORDER_NONE) || 0;
return `self.y += ${dy}\n`;
};

Blockly.Ruby.motion_sety = function (block) {
const y = Blockly.Ruby.valueToCode(block, 'Y', Blockly.Ruby.ORDER_NONE) || '0';
const y = Blockly.Ruby.valueToCode(block, 'Y', Blockly.Ruby.ORDER_NONE) || 0;
return `self.y = ${y}\n`;
};

Blockly.Ruby.motion_ifonedgebounce = function () {
return 'if_on_edge_bounce\n';
return 'bounce_if_on_edge\n';
};

Blockly.Ruby.motion_setrotationstyle = function (block) {
const style = Blockly.Ruby.quote_(block.getFieldValue('STYLE') || null);
return `set_rotation_style(${style})\n`;
const style = Blockly.Ruby.quote_(block.getFieldValue('STYLE') || 'all around');
return `self.rotation_style = ${style}\n`;
};

Blockly.Ruby.motion_xposition = function () {
Expand Down
51 changes: 32 additions & 19 deletions src/lib/ruby-generator/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,70 @@
export default function (Blockly) {
Blockly.Ruby.operator_add = function (block) {
const order = Blockly.Ruby.ORDER_ADDITIVE;
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '0';
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || 0;
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || 0;
return [`${num1} + ${num2}`, order];
};

Blockly.Ruby.operator_subtract = function (block) {
const order = Blockly.Ruby.ORDER_ADDITIVE;
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '0';
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || 0;
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || 0;
return [`${num1} - ${num2}`, Blockly.Ruby.ORDER_ADDITIVE];
};

Blockly.Ruby.operator_multiply = function (block) {
const order = Blockly.Ruby.ORDER_MULTIPLICATIVE;
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '0';
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || 0;
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || 0;
return [`${num1} * ${num2}`, order];
};

Blockly.Ruby.operator_divide = function (block) {
const order = Blockly.Ruby.ORDER_MULTIPLICATIVE;
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '1';
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || 0.0;
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || 0.0;
return [`${num1} / ${num2}`, order];
};

Blockly.Ruby.operator_random = function (block) {
const fromNum = Blockly.Ruby.valueToCode(block, 'FROM', Blockly.Ruby.ORDER_RANGE) || '0';
const toNum = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_RANGE) || '0';
const fromNum = Blockly.Ruby.valueToCode(block, 'FROM', Blockly.Ruby.ORDER_RANGE) || 1;
const toNum = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_RANGE) || 10;
return [`rand(${fromNum}..${toNum})`, Blockly.Ruby.ORDER_FUNCTION_CALL];
};

const stringOperandToCode = function (operand) {
if (Blockly.Ruby.isString(operand) &&
operand[0] === '"' &&
operand[operand.length - 1] === '"') {
const s = operand.slice(1, operand.length - 1);
const n = Number(s);
if (n !== 0 || !Blockly.Ruby.isWhiteSpace(s)) {
return n;
}
}
return operand;
};

Blockly.Ruby.operator_gt = function (block) {
const order = Blockly.Ruby.ORDER_RELATIONAL;
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || '0';
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || '0';
return [`Cast.compare(${operand1}, ${operand2}) > 0`, order];
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || 0;
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || 0;
return [`${stringOperandToCode(operand1)} > ${stringOperandToCode(operand2)}`, order];
};

Blockly.Ruby.operator_lt = function (block) {
const order = Blockly.Ruby.ORDER_RELATIONAL;
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || '0';
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || '0';
return [`Cast.compare(${operand1}, ${operand2}) < 0`, order];
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || 0;
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || 0;
return [`${stringOperandToCode(operand1)} < ${stringOperandToCode(operand2)}`, order];
};

Blockly.Ruby.operator_equals = function (block) {
const order = Blockly.Ruby.ORDER_EQUALS;
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || '0';
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || '0';
return [`Cast.compare(${operand1}, ${operand2}) == 0`, order];
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || 0;
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || 0;
return [`${stringOperandToCode(operand1)} == ${stringOperandToCode(operand2)}`, order];
};

Blockly.Ruby.operator_and = function (block) {
Expand Down
Loading