Skip to content

Commit d1dafe3

Browse files
authored
Merge pull request #361 from smalruby/fix-smalruby-koshien
2 parents ba43f4f + 0c6201a commit d1dafe3

File tree

9 files changed

+204
-164
lines changed

9 files changed

+204
-164
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/containers/ruby-tab.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class RubyTab extends React.Component {
154154
alt="ruby download"
155155
className={styles.img}
156156
/>
157-
157+
158158
</button>
159159
)}
160160
</RubyDownloader>
@@ -189,7 +189,7 @@ RubyTab.propTypes = {
189189
updateRubyCodeTargetState: PropTypes.func,
190190
vm: PropTypes.instanceOf(VM).isRequired,
191191
projectTitle: PropTypes.string,
192-
locale: PropTypes.string.isRequired
192+
locale: PropTypes.string
193193
};
194194

195195
const mapStateToProps = state => ({

src/lib/ruby-generator/koshien.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,87 @@ export default function (Generator) {
1010
};
1111

1212
Generator.koshien_getMapArea = function (block) {
13-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
14-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
15-
return `koshien.get_map_area(${x}, ${y})\n`;
13+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
14+
return `koshien.get_map_area(${position})\n`;
1615
};
1716

1817
Generator.koshien_map = function (block) {
19-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
20-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
21-
return [`koshien.map(${x}, ${y})`];
18+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
19+
return [`koshien.map(${position})`];
2220
};
2321

2422
Generator.koshien_moveTo = function (block) {
25-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
26-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
27-
return `koshien.move_to(${x}, ${y})\n`;
23+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
24+
return `koshien.move_to(${position})\n`;
2825
};
2926

3027
Generator.koshien_calcRoute = function (block) {
31-
const srcX = Generator.valueToCode(block, 'SRC_X', Generator.ORDER_NONE) || 0;
32-
const srcY = Generator.valueToCode(block, 'SRC_Y', Generator.ORDER_NONE) || 0;
33-
const dstX = Generator.valueToCode(block, 'DST_X', Generator.ORDER_NONE) || 0;
34-
const dstY = Generator.valueToCode(block, 'DST_Y', Generator.ORDER_NONE) || 0;
35-
const exceptCells = Generator.quote_(
36-
Generator.getFieldValue(block, 'EXCEPT_CELLS', Generator.ORDER_NONE) || ' '
28+
const src = Generator.valueToCode(block, 'SRC', Generator.ORDER_NONE) || Generator.quote_('0:0');
29+
const dst = Generator.valueToCode(block, 'DST', Generator.ORDER_NONE) || Generator.quote_('0:0');
30+
const exceptCellsListName = Generator.listNameByName(
31+
Generator.getFieldValue(block, 'EXCEPT_CELLS', Generator.ORDER_NONE)
3732
);
38-
const result = Generator.quote_(
39-
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE) || ' '
33+
const exceptCells = exceptCellsListName ? `list(${Generator.quote_(exceptCellsListName)})` : 'nil';
34+
const resultListName = Generator.listNameByName(
35+
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
4036
);
37+
const result = resultListName ? `list(${Generator.quote_(resultListName)})` : 'nil';
4138

42-
// eslint-disable-next-line max-len
43-
return `koshien.calc_route(src: [${srcX}, ${srcY}], dst: [${dstX}, ${dstY}], except_cells: ${exceptCells}, result: ${result})\n`;
39+
return `koshien.calc_route(result: ${result}, src: ${src}, dst: ${dst}, except_cells: ${exceptCells})\n`;
4440
};
4541

4642
Generator.koshien_setItem = function (block) {
4743
const item = Generator.getFieldValue(block, 'ITEM') || null;
48-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
49-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
50-
return `koshien.set_${item}(${x}, ${y})\n`;
44+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
45+
return `koshien.set_${item}(${position})\n`;
5146
};
5247

53-
Generator.koshien_loadMap = function (block) {
54-
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
55-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
56-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
57-
return [`koshien.load_map(${location}, ${x}, ${y})`];
48+
Generator.koshien_mapFrom = function (block) {
49+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
50+
const mapVariableName = Generator.variableNameByName(
51+
Generator.getFieldValue(block, 'MAP', Generator.ORDER_NONE)
52+
);
53+
const map = mapVariableName ? mapVariableName : 'nil';
54+
return [`koshien.map_from(${position}, ${map})`];
5855
};
5956

60-
Generator.koshien_saveMapAll = function (block) {
61-
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
62-
return `koshien.save_map_all(${location})\n`;
57+
Generator.koshien_mapAll = function () {
58+
return ['koshien.map_all'];
6359
};
6460

6561
Generator.koshien_locateObjects = function (block) {
66-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
67-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
62+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
6863
const sqSize = Generator.valueToCode(block, 'SQ_SIZE', Generator.ORDER_NONE) || 5;
6964
const objects = Generator.valueToCode(block, 'OBJECTS', Generator.ORDER_NONE) || Generator.quote_('A B C D');
70-
const result = Generator.quote_(
71-
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE) || ' '
65+
const resultListName = Generator.listNameByName(
66+
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
7267
);
68+
const result = resultListName ? `list(${Generator.quote_(resultListName)})` : 'nil';
7369

7470
// eslint-disable-next-line max-len
75-
return `koshien.locate_objects(sq_size: ${sqSize}, cent: [${x}, ${y}], objects: ${objects}, result: ${result})\n`;
71+
return `koshien.locate_objects(result: ${result}, sq_size: ${sqSize}, cent: ${position}, objects: ${objects})\n`;
7672
};
73+
7774
Generator.koshien_targetCoordinate = function (block) {
7875
const target = Generator.getFieldValue(block, 'TARGET') || 'player';
7976
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
8077
return [`koshien.${target}_${coordinate}`];
8178
};
79+
8280
Generator.koshien_turnOver = function () {
8381
return `koshien.turn_over\n`;
8482
};
85-
Generator.koshien_coordinateOf = function (block) {
86-
const where = Generator.valueToCode(block, 'WHERE', Generator.ORDER_NONE) || Generator.quote_('0:0');
83+
84+
Generator.koshien_position = function (block) {
85+
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
86+
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
87+
return [`koshien.position(${x}, ${y})`];
88+
};
89+
90+
Generator.koshien_positionOf = function (block) {
91+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
8792
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || null;
88-
return [`koshien.coordinate_of_${coordinate}(${where})`];
93+
return [`koshien.position_of_${coordinate}(${position})`];
8994
};
9095

9196
return Generator;

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,10 @@ class RubyToBlocksConverter {
509509
return value === false || (value && value.type === 'false');
510510
}
511511

512+
isNil (value) {
513+
return value === Opal.nil || (value && value.type === 'nil');
514+
}
515+
512516
_isArray (value) {
513517
return _.isArray(value) || (value && value.type === 'array');
514518
}
@@ -522,17 +526,17 @@ class RubyToBlocksConverter {
522526
}
523527

524528
isBlock (block) {
525-
return this._isBlock(block);
526-
}
527-
528-
_isBlock (block) {
529529
try {
530530
return Object.prototype.hasOwnProperty.call(block, 'opcode');
531531
} catch (e) {
532532
return false;
533533
}
534534
}
535535

536+
_isBlock (block) {
537+
return this.isBlock(block);
538+
}
539+
536540
_isStatementBlock (block) {
537541
const blockType = this._getBlockType(block);
538542
return blockType === 'statement' || blockType === 'terminate';
@@ -590,10 +594,18 @@ class RubyToBlocksConverter {
590594
return false;
591595
}
592596

593-
_isVariableBlock (block) {
597+
isVariableBlockType (block) {
594598
return /_variable$/.test(this._getBlockType(block));
595599
}
596600

601+
isVariableBlock (block) {
602+
return this.isVariableBlockType(block) && block.opcode === 'data_variable';
603+
}
604+
605+
isListBlock (block) {
606+
return this.getBlockType(block) === 'value_variable' && block.opcode === 'data_listcontents';
607+
}
608+
597609
_isRubyExpression (block) {
598610
return this._isBlock(block) && block.opcode === 'ruby_expression';
599611
}
@@ -843,6 +855,19 @@ class RubyToBlocksConverter {
843855
return this._lookupOrCreateVariableOrList(name, Variable.LIST_TYPE);
844856
}
845857

858+
lookupVariableFromVariableBlock (block) {
859+
if (!this.isVariableBlock(block)) return null;
860+
861+
return this._context.variables[block.fields.VARIABLE.value] ||
862+
this._context.localVariables[block.fields.VARIABLE.value];
863+
}
864+
865+
lookupListFromListBlock (block) {
866+
if (!this.isListBlock(block)) return null;
867+
868+
return this._context.lists[block.fields.LIST.value];
869+
}
870+
846871
lookupOrCreateBroadcastMsg (name) {
847872
return this._lookupOrCreateBroadcastMsg(name);
848873
}
@@ -922,6 +947,20 @@ class RubyToBlocksConverter {
922947
return b;
923948
}
924949

950+
removeBlock (block) {
951+
if (this.isNil(block)) {
952+
return;
953+
}
954+
955+
const previousBlockId = block.parent;
956+
if (previousBlockId) {
957+
const previousBlock = this._context.blocks[previousBlockId];
958+
previousBlock.next = block.next;
959+
}
960+
961+
delete this._context.blocks[block.id];
962+
}
963+
925964
_removeWaitBlocks (block) {
926965
if (!block || block === Opal.nil) {
927966
return null;
@@ -958,17 +997,25 @@ class RubyToBlocksConverter {
958997
return firstBlock;
959998
}
960999

961-
_getBlockType (block) {
962-
if (this._isBlock(block)) {
1000+
getBlockType (block) {
1001+
if (this.isBlock(block)) {
9631002
return this._context.blockTypes[block.id];
9641003
}
9651004
return 'primitive';
9661005
}
9671006

1007+
_getBlockType (block) {
1008+
return this.getBlockType(block);
1009+
}
1010+
9681011
_setBlockType (block, type) {
9691012
this._context.blockTypes[block.id] = type;
9701013
}
9711014

1015+
changeBlock (block, opcode, blockType) {
1016+
return this._changeBlock(block, opcode, blockType);
1017+
}
1018+
9721019
_changeBlock (block, opcode, blockType) {
9731020
block.opcode = opcode;
9741021
this._setBlockType(block, blockType);
@@ -1231,6 +1278,10 @@ class RubyToBlocksConverter {
12311278
return new Primitive('hash', new Map(node.children.map(childNode => this._process(childNode))), node);
12321279
}
12331280

1281+
_onNil (node) {
1282+
return new Primitive('nil', Opal.nil, node);
1283+
}
1284+
12341285
_onPair (node) {
12351286
this._checkNumChildren(node, 2);
12361287

0 commit comments

Comments
 (0)