Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getVariableName and getProcedureName to CodeGenerator #7445

Merged
merged 5 commits into from
Aug 31, 2023
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
38 changes: 37 additions & 1 deletion core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class CodeGenerator {
protected functionNames_: {[key: string]: string} = Object.create(null);

/** A database of variable and procedure names. */
protected nameDB_?: Names = undefined;
nameDB_?: Names = undefined;

/** @param name Language name of this generator. */
constructor(name: string) {
Expand Down Expand Up @@ -495,6 +495,42 @@ export class CodeGenerator {
return this.functionNames_[desiredName];
}

/**
* Gets a unique, legal name for a user-defined variable.
* Before calling this method, the `nameDB_` property of the class
* must have been initialized already. This is typically done in
* the `init` function of the code generator class.
*
* @param nameOrId The ID of the variable to get a name for,
* or the proposed name for a variable not associated with an id.
* @returns A unique, legal name for the variable.
*/
getVariableName(nameOrId: string): string {
return this.getName(nameOrId, NameType.VARIABLE);
}

/**
* Gets a unique, legal name for a user-defined procedure.
* Before calling this method, the `nameDB_` property of the class
* must have been initialized already. This is typically done in
* the `init` function of the code generator class.
*
* @param name The proposed name for a procedure.
* @returns A unique, legal name for the procedure.
*/
getProcedureName(name: string): string {
return this.getName(name, NameType.PROCEDURE);
}

private getName(nameOrId: string, type: NameType): string {
if (!this.nameDB_) {
throw new Error(
'Name database is not defined. You must initialize `nameDB_` in your generator class and call `init` first.',
);
}
return this.nameDB_.getName(nameOrId, type);
}

/**
* Hook for code to run before code generation starts.
* Subclasses may override this, e.g. to initialise the database of variable
Expand Down
6 changes: 2 additions & 4 deletions generators/dart/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ export function controls_whileUntil(block, generator) {
export function controls_for(block, generator) {
// For loop.
const variable0 =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
const argument0 =
generator.valueToCode(block, 'FROM', Order.ASSIGNMENT) || '0';
const argument1 =
Expand Down Expand Up @@ -129,8 +128,7 @@ export function controls_for(block, generator) {
export function controls_forEach(block, generator) {
// For each loop.
const variable0 =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
const argument0 =
generator.valueToCode(block, 'LIST', Order.ASSIGNMENT) || '[]';
let branch = generator.statementToCode(block, 'DO');
Expand Down
4 changes: 1 addition & 3 deletions generators/dart/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Dart.math');

import {NameType} from '../../core/names.js';
import {Order} from './dart_generator.js';


Expand Down Expand Up @@ -224,8 +223,7 @@ export function math_change(block, generator) {
const argument0 =
generator.valueToCode(block, 'DELTA', Order.ADDITIVE) || '0';
const varName =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
return varName + ' = (' + varName + ' is num ? ' + varName + ' : 0) + ' +
argument0 + ';\n';
};
Expand Down
9 changes: 3 additions & 6 deletions generators/dart/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Dart.procedures');

import {NameType} from '../../core/names.js';
import {Order} from './dart_generator.js';


export function procedures_defreturn(block, generator) {
// Define a procedure with a return value.
const funcName =
generator.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
generator.getProcedureName(block.getFieldValue('NAME'));
let xfix1 = '';
if (generator.STATEMENT_PREFIX) {
xfix1 += generator.injectId(generator.STATEMENT_PREFIX, block);
Expand Down Expand Up @@ -51,7 +49,7 @@ export function procedures_defreturn(block, generator) {
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = generator.nameDB_.getName(variables[i], NameType.VARIABLE);
args[i] = generator.getVariableName(variables[i]);
}
let code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
Expand All @@ -68,8 +66,7 @@ export const procedures_defnoreturn = procedures_defreturn;
export function procedures_callreturn(block, generator) {
// Call a procedure with a return value.
const funcName =
generator.nameDB_.getName(
block.getFieldValue('NAME'),NameType.PROCEDURE);
generator.getProcedureName(block.getFieldValue('NAME'));
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
Expand Down
4 changes: 1 addition & 3 deletions generators/dart/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Dart.texts');

import {NameType} from '../../core/names.js';
import {Order} from './dart_generator.js';


Expand Down Expand Up @@ -57,8 +56,7 @@ export function text_join(block, generator) {
export function text_append(block, generator) {
// Append to a variable in place.
const varName =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
const value = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
return varName + ' = [' + varName + ', ' + value + '].join();\n';
};
Expand Down
7 changes: 2 additions & 5 deletions generators/dart/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Dart.variables');

import {NameType} from '../../core/names.js';
import {Order} from './dart_generator.js';


export function variables_get(block, generator) {
// Variable getter.
const code =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
return [code, Order.ATOMIC];
};

Expand All @@ -28,7 +26,6 @@ export function variables_set(block, generator) {
const argument0 =
generator.valueToCode(block, 'VALUE', Order.ASSIGNMENT) || '0';
const varName =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
return varName + ' = ' + argument0 + ';\n';
};
7 changes: 3 additions & 4 deletions generators/javascript/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export function controls_whileUntil(block, generator) {
export function controls_for(block, generator) {
// For loop.
const variable0 =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(
block.getFieldValue('VAR'));
const argument0 =
generator.valueToCode(block, 'FROM', Order.ASSIGNMENT) || '0';
const argument1 =
Expand Down Expand Up @@ -129,8 +129,7 @@ export function controls_for(block, generator) {
export function controls_forEach(block, generator) {
// For each loop.
const variable0 =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
const argument0 =
generator.valueToCode(block, 'LIST', Order.ASSIGNMENT) ||
'[]';
Expand Down
4 changes: 1 addition & 3 deletions generators/javascript/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.JavaScript.math');

import {NameType} from '../../core/names.js';
import {Order} from './javascript_generator.js';


Expand Down Expand Up @@ -203,8 +202,7 @@ export function math_change(block, generator) {
// Add to a variable in place.
const argument0 = generator.valueToCode(block, 'DELTA',
Order.ADDITION) || '0';
const varName = generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
const varName = generator.getVariableName(block.getFieldValue('VAR'));
return varName + ' = (typeof ' + varName + ' === \'number\' ? ' + varName +
' : 0) + ' + argument0 + ';\n';
};
Expand Down
9 changes: 3 additions & 6 deletions generators/javascript/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.JavaScript.procedures');

import {NameType} from '../../core/names.js';
import {Order} from './javascript_generator.js';


export function procedures_defreturn(block, generator) {
// Define a procedure with a return value.
const funcName = generator.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
const funcName = generator.getProcedureName(block.getFieldValue('NAME'));
let xfix1 = '';
if (generator.STATEMENT_PREFIX) {
xfix1 += generator.injectId(
Expand Down Expand Up @@ -53,7 +51,7 @@ export function procedures_defreturn(block, generator) {
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] =
generator.nameDB_.getName(variables[i], NameType.VARIABLE);
generator.getVariableName(variables[i]);
}
let code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' + xfix1 +
loopTrap + branch + xfix2 + returnValue + '}';
Expand All @@ -69,8 +67,7 @@ export const procedures_defnoreturn = procedures_defreturn;

export function procedures_callreturn(block, generator) {
// Call a procedure with a return value.
const funcName = generator.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
const funcName = generator.getProcedureName(block.getFieldValue('NAME'));
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
Expand Down
4 changes: 1 addition & 3 deletions generators/javascript/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.JavaScript.texts');

import {NameType} from '../../core/names.js';
import {Order} from './javascript_generator.js';


Expand Down Expand Up @@ -102,8 +101,7 @@ export function text_join(block, generator) {

export function text_append(block, generator) {
// Append to a variable in place.
const varName = generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
const varName = generator.getVariableName(block.getFieldValue('VAR'));
const value = generator.valueToCode(block, 'TEXT',
Order.NONE) || "''";
const code = varName + ' += ' +
Expand Down
7 changes: 2 additions & 5 deletions generators/javascript/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.JavaScript.variables');

import {NameType} from '../../core/names.js';
import {Order} from './javascript_generator.js';


export function variables_get(block, generator) {
// Variable getter.
const code = generator.nameDB_.getName(block.getFieldValue('VAR'),
NameType.VARIABLE);
const code = generator.getVariableName(block.getFieldValue('VAR'));
return [code, Order.ATOMIC];
};

export function variables_set(block, generator) {
// Variable setter.
const argument0 = generator.valueToCode(
block, 'VALUE', Order.ASSIGNMENT) || '0';
const varName = generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
const varName = generator.getVariableName(block.getFieldValue('VAR'));
return varName + ' = ' + argument0 + ';\n';
};
6 changes: 2 additions & 4 deletions generators/lua/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ export function controls_whileUntil(block, generator) {
export function controls_for(block, generator) {
// For loop.
const variable0 =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
const startVar = generator.valueToCode(block, 'FROM', Order.NONE) || '0';
const endVar = generator.valueToCode(block, 'TO', Order.NONE) || '0';
const increment = generator.valueToCode(block, 'BY', Order.NONE) || '1';
Expand Down Expand Up @@ -130,8 +129,7 @@ export function controls_for(block, generator) {
export function controls_forEach(block, generator) {
// For each loop.
const variable0 =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
const argument0 = generator.valueToCode(block, 'LIST', Order.NONE) || '{}';
let branch = generator.statementToCode(block, 'DO');
branch = generator.addLoopTrap(branch, block);
Expand Down
4 changes: 1 addition & 3 deletions generators/lua/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Lua.math');

import {NameType} from '../../core/names.js';
import {Order} from './lua_generator.js';


Expand Down Expand Up @@ -186,8 +185,7 @@ export function math_change(block, generator) {
const argument0 =
generator.valueToCode(block, 'DELTA', Order.ADDITIVE) || '0';
const varName =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
return varName + ' = ' + varName + ' + ' + argument0 + '\n';
};

Expand Down
9 changes: 3 additions & 6 deletions generators/lua/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Lua.procedures');

import {NameType} from '../../core/names.js';
import {Order} from './lua_generator.js';


export function procedures_defreturn(block, generator) {
// Define a procedure with a return value.
const funcName =
generator.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
generator.getProcedureName(block.getFieldValue('NAME'));
let xfix1 = '';
if (generator.STATEMENT_PREFIX) {
xfix1 += generator.injectId(generator.STATEMENT_PREFIX, block);
Expand Down Expand Up @@ -51,7 +49,7 @@ export function procedures_defreturn(block, generator) {
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = generator.nameDB_.getName(variables[i], NameType.VARIABLE);
args[i] = generator.getVariableName(variables[i]);
}
let code = 'function ' + funcName + '(' + args.join(', ') + ')\n' + xfix1 +
loopTrap + branch + xfix2 + returnValue + 'end\n';
Expand All @@ -68,8 +66,7 @@ export const procedures_defnoreturn = procedures_defreturn;
export function procedures_callreturn(block, generator) {
// Call a procedure with a return value.
const funcName =
generator.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
generator.getProcedureName(block.getFieldValue('NAME'));
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
Expand Down
4 changes: 1 addition & 3 deletions generators/lua/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Lua.texts');

import {NameType} from '../../core/names.js';
import {Order} from './lua_generator.js';


Expand Down Expand Up @@ -58,8 +57,7 @@ export function text_join(block, generator) {
export function text_append(block, generator) {
// Append to a variable in place.
const varName =
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
generator.getVariableName(block.getFieldValue('VAR'));
const value =
generator.valueToCode(block, 'TEXT', Order.CONCATENATION) || "''";
return varName + ' = ' + varName + ' .. ' + value + '\n';
Expand Down
Loading