Skip to content

Commit 178dc39

Browse files
authored
Merge pull request #86 from alan412/pr_class_instance_variables
Make all variables class instance variables
2 parents 612a7e3 + 87232c6 commit 178dc39

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { MRC_STYLE_CLASS_BLOCKS } from '../themes/styles';
2424
import { createFieldNonEditableText } from '../fields/FieldNonEditableText'
2525
import * as ChangeFramework from './utils/change_framework'
2626
import { getLegalName } from './utils/python';
27+
import { Order } from 'blockly/python';
28+
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2729

2830
export const BLOCK_NAME = 'mrc_class_method_def';
2931

@@ -351,10 +353,6 @@ export const setup = function() {
351353
Blockly.Blocks[PARAM_CONTAINER_BLOCK_NAME] = METHOD_PARAM_CONTAINER;
352354
};
353355

354-
import { Order } from 'blockly/python';
355-
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
356-
357-
358356
export const pythonFromBlock = function (
359357
block: ClassMethodDefBlock,
360358
generator: ExtendedPythonGenerator,
@@ -394,7 +392,7 @@ export const pythonFromBlock = function (
394392
xfix2 = xfix1;
395393
}
396394
if(block.mrcPythonMethodName == '__init__'){
397-
branch = generator.INDENT + "self.mechanisms = []\n" + branch;
395+
branch = generator.defineClassVariables(block.workspace) + branch;
398396
}
399397
if (returnValue) {
400398
returnValue = generator.INDENT + 'return ' + returnValue + '\n';

src/blocks/mrc_mechanism.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,16 @@ export const setup = function () {
118118
Blockly.Blocks[BLOCK_NAME] = MECHANISM_FUNCTION;
119119
}
120120

121-
//TODO: This needs to cause our own init to create the mechanisms line
122121
export const pythonFromBlock = function (
123122
mechanismBlock: MechanismBlock,
124123
generator: ExtendedPythonGenerator,
125124
) {
126125
if (mechanismBlock.mrcImportModule) {
127126
generator.addImport(mechanismBlock.mrcImportModule);
128127
}
128+
generator.setHasMechanism();
129129
let code = 'self.mechanisms["' + mechanismBlock.getFieldValue('NAME') + '"] = '
130130
+ mechanismBlock.getFieldValue('TYPE') + '('
131-
132131
for (let i = 0; i < mechanismBlock.mrcArgs.length; i++) {
133132
const fieldName = 'ARG' + i;
134133
if(i != 0){

src/blocks/setup_custom_blocks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as Blockly from 'blockly';
21

32
import * as CallPythonFunction from './mrc_call_python_function';
43
import * as GetPythonEnumValue from './mrc_get_python_enum_value';

src/editor/extended_python_generator.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,42 @@ export class ExtendedPythonGenerator extends PythonGenerator {
3939
super('Python');
4040
}
4141

42+
init(workspace: Blockly.Workspace){
43+
super.init(workspace);
44+
// This will have all variables in the definition 'variables' so we will need to make it contain only the developer variables
45+
delete this.definitions_['variables'];
46+
47+
const defvars = [];
48+
// Add developer variables (not created or named by the user).
49+
const devVarList = Blockly.Variables.allDeveloperVariables(workspace);
50+
for (let i = 0; i < devVarList.length; i++) {
51+
defvars.push(
52+
this.nameDB_!.getName(devVarList[i], Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ' = None',
53+
);
54+
}
55+
this.definitions_['variables'] = defvars.join('\n');
56+
}
57+
58+
/*
59+
* This is called from the python generator for the mrc_class_method_def for the
60+
* init method
61+
*/
62+
defineClassVariables(workspace: Blockly.Workspace) : string{
63+
let variableDefinitions = '';
64+
65+
if (this.context?.getHasMechanisms()){
66+
variableDefinitions += this.INDENT + "self.mechanisms = []\n";
67+
}
68+
return variableDefinitions;
69+
}
70+
getVariableName(nameOrId : string) : string{
71+
const varName = super.getVariableName(name);
72+
return "self." + varName;
73+
}
74+
setHasMechanism() : void{
75+
this.context?.setHasMechanism();
76+
}
77+
4278
workspaceToCode(workspace: Blockly.Workspace, context: GeneratorContext): string {
4379
this.workspace = workspace;
4480
this.context = context;

src/editor/generator_context.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class GeneratorContext {
3636
// Key is the mrc_class_method_def block's NAME field, value is the python method name.
3737
private classMethodNames: {[key: string]: string} = Object.create(null);
3838

39+
// Has mechanisms (ie, needs in init)
40+
private hasMechanisms = false;
41+
3942
setModule(module: commonStorage.Module | null) {
4043
this.module = module;
4144
this.clear();
@@ -44,6 +47,13 @@ export class GeneratorContext {
4447
clear(): void {
4548
this.clearExportedBlocks();
4649
this.clearClassMethodNames();
50+
this.hasMechanisms = false;
51+
}
52+
setHasMechanism():void{
53+
this.hasMechanisms = true;
54+
}
55+
getHasMechanisms():boolean{
56+
return this.hasMechanisms;
4757
}
4858

4959
getClassName(): string {

0 commit comments

Comments
 (0)