Skip to content

Commit

Permalink
Fix loop code generators.
Browse files Browse the repository at this point in the history
The code generators for the `repeat` and `while` blocks were generating JS code instead of bBasic.
This fixes #40 
Merge pull request #43 from haroldo-ok/draw-screen-on-demand
  • Loading branch information
haroldo-ok authored Feb 23, 2023
2 parents 9d28c96 + ff92485 commit 848c0bb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vcs-game-maker",
"version": "0.10.1",
"version": "0.10.2",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down
1 change: 1 addition & 0 deletions src/generators/bbasic.bb.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ end
dim player0frame = x
dim player1frame = z
dim newbackground = y
dim loopcounter = w

newbackground = 1

Expand Down
1 change: 1 addition & 0 deletions src/generators/bbasic.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Blockly.BBasic.finish = function(code) {
// Call Blockly.Generator's finish.
code = Object.getPrototypeOf(this).finish.call(this, code);
code = code.replace(/^[\t ]*/gm, Blockly.BBasic.INDENT);
code = code.replace(/^[\t ]*@/gm, '');

const generatedBackgrounds = Blockly.BBasic.generateBackgrounds();
const animation = Blockly.BBasic.generateAnimations();
Expand Down
31 changes: 23 additions & 8 deletions src/generators/bbasic/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,26 @@ export default (Blockly) => {
let branch = Blockly.BBasic.statementToCode(block, 'DO');
branch = Blockly.BBasic.addLoopTrap(branch, block);
let code = '';
/*
const loopVar = Blockly.BBasic.nameDB_.getDistinctName(
'count', Blockly.VARIABLE_CATEGORY_NAME);
*/
let endVar = repeats;
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
endVar = Blockly.BBasic.nameDB_.getDistinctName(
'repeat_end', Blockly.VARIABLE_CATEGORY_NAME);
code += 'var ' + endVar + ' = ' + repeats + ';\n';
}
code += 'for (var ' + loopVar + ' = 0; ' +
loopVar + ' < ' + endVar + '; ' +
loopVar + '++) {\n' +
branch + '}\n';

if (!branch.trim()) {
branch = 'a = a';
}

branch = branch.replace(/\s*\n\s*/g, ' : ').trim().replace(/\s*:\s*$/g, '');
code += 'for loopcounter = 1 to ' + endVar + ': ' +
branch +
' : next\n';

return code;
};

Expand All @@ -52,16 +60,23 @@ export default (Blockly) => {

Blockly.BBasic['controls_whileUntil'] = function(block) {
// Do while/until loop.
const labelName = Blockly.BBasic.nameDB_.getName('while', Blockly.PROCEDURE_CATEGORY_NAME);
const startLabelName = labelName + 'start';
const endLabelName = labelName + 'end';
const until = block.getFieldValue('MODE') == 'UNTIL';
let argument0 = Blockly.BBasic.valueToCode(block, 'BOOL',
until ? Blockly.BBasic.ORDER_LOGICAL_NOT :
Blockly.BBasic.ORDER_NONE) || 'false';
let branch = Blockly.BBasic.statementToCode(block, 'DO');
branch = Blockly.BBasic.addLoopTrap(branch, block);
if (until) {
argument0 = '!' + argument0;
branch = Blockly.BBasic.addLoopTrap(branch, block); // eslint-disable-line
if (!until) {
argument0 = '!' + argument0; // eslint-disable-line
}
return 'while (' + argument0 + ') {\n' + branch + '}\n';
return '@' + startLabelName + '\n' +
'if ' + argument0 + ' then goto ' + endLabelName + '\n' +
branch + '\n' +
'goto ' + startLabelName + '\n' +
'@' + endLabelName;
};

Blockly.BBasic['controls_for'] = function(block) {
Expand Down

0 comments on commit 848c0bb

Please sign in to comment.