diff --git a/src/blocks/mrc_class_method_def.ts b/src/blocks/mrc_class_method_def.ts index eadec9df..5e8f1606 100644 --- a/src/blocks/mrc_class_method_def.ts +++ b/src/blocks/mrc_class_method_def.ts @@ -24,6 +24,8 @@ import { MRC_STYLE_CLASS_BLOCKS } from '../themes/styles'; import { createFieldNonEditableText } from '../fields/FieldNonEditableText' import * as ChangeFramework from './utils/change_framework' import { getLegalName } from './utils/python'; +import { Order } from 'blockly/python'; +import { ExtendedPythonGenerator } from '../editor/extended_python_generator'; export const BLOCK_NAME = 'mrc_class_method_def'; @@ -351,10 +353,6 @@ export const setup = function() { Blockly.Blocks[PARAM_CONTAINER_BLOCK_NAME] = METHOD_PARAM_CONTAINER; }; -import { Order } from 'blockly/python'; -import { ExtendedPythonGenerator } from '../editor/extended_python_generator'; - - export const pythonFromBlock = function ( block: ClassMethodDefBlock, generator: ExtendedPythonGenerator, @@ -394,7 +392,7 @@ export const pythonFromBlock = function ( xfix2 = xfix1; } if(block.mrcPythonMethodName == '__init__'){ - branch = generator.INDENT + "self.mechanisms = []\n" + branch; + branch = generator.defineClassVariables(block.workspace) + branch; } if (returnValue) { returnValue = generator.INDENT + 'return ' + returnValue + '\n'; diff --git a/src/blocks/mrc_mechanism.ts b/src/blocks/mrc_mechanism.ts index 8738fa48..2187a5aa 100644 --- a/src/blocks/mrc_mechanism.ts +++ b/src/blocks/mrc_mechanism.ts @@ -118,7 +118,6 @@ export const setup = function () { Blockly.Blocks[BLOCK_NAME] = MECHANISM_FUNCTION; } -//TODO: This needs to cause our own init to create the mechanisms line export const pythonFromBlock = function ( mechanismBlock: MechanismBlock, generator: ExtendedPythonGenerator, @@ -126,9 +125,9 @@ export const pythonFromBlock = function ( if (mechanismBlock.mrcImportModule) { generator.addImport(mechanismBlock.mrcImportModule); } + generator.setHasMechanism(); let code = 'self.mechanisms["' + mechanismBlock.getFieldValue('NAME') + '"] = ' + mechanismBlock.getFieldValue('TYPE') + '(' - for (let i = 0; i < mechanismBlock.mrcArgs.length; i++) { const fieldName = 'ARG' + i; if(i != 0){ diff --git a/src/blocks/setup_custom_blocks.ts b/src/blocks/setup_custom_blocks.ts index e460689f..0f40f6f5 100644 --- a/src/blocks/setup_custom_blocks.ts +++ b/src/blocks/setup_custom_blocks.ts @@ -1,4 +1,3 @@ -import * as Blockly from 'blockly'; import * as CallPythonFunction from './mrc_call_python_function'; import * as GetPythonEnumValue from './mrc_get_python_enum_value'; diff --git a/src/editor/extended_python_generator.ts b/src/editor/extended_python_generator.ts index b9275551..33a13240 100644 --- a/src/editor/extended_python_generator.ts +++ b/src/editor/extended_python_generator.ts @@ -39,6 +39,42 @@ export class ExtendedPythonGenerator extends PythonGenerator { super('Python'); } + init(workspace: Blockly.Workspace){ + super.init(workspace); + // This will have all variables in the definition 'variables' so we will need to make it contain only the developer variables + delete this.definitions_['variables']; + + const defvars = []; + // Add developer variables (not created or named by the user). + const devVarList = Blockly.Variables.allDeveloperVariables(workspace); + for (let i = 0; i < devVarList.length; i++) { + defvars.push( + this.nameDB_!.getName(devVarList[i], Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ' = None', + ); + } + this.definitions_['variables'] = defvars.join('\n'); + } + + /* + * This is called from the python generator for the mrc_class_method_def for the + * init method + */ + defineClassVariables(workspace: Blockly.Workspace) : string{ + let variableDefinitions = ''; + + if (this.context?.getHasMechanisms()){ + variableDefinitions += this.INDENT + "self.mechanisms = []\n"; + } + return variableDefinitions; + } + getVariableName(nameOrId : string) : string{ + const varName = super.getVariableName(name); + return "self." + varName; + } + setHasMechanism() : void{ + this.context?.setHasMechanism(); + } + workspaceToCode(workspace: Blockly.Workspace, context: GeneratorContext): string { this.workspace = workspace; this.context = context; diff --git a/src/editor/generator_context.ts b/src/editor/generator_context.ts index 2616c74b..1c422c21 100644 --- a/src/editor/generator_context.ts +++ b/src/editor/generator_context.ts @@ -36,6 +36,9 @@ export class GeneratorContext { // Key is the mrc_class_method_def block's NAME field, value is the python method name. private classMethodNames: {[key: string]: string} = Object.create(null); + // Has mechanisms (ie, needs in init) + private hasMechanisms = false; + setModule(module: commonStorage.Module | null) { this.module = module; this.clear(); @@ -44,6 +47,13 @@ export class GeneratorContext { clear(): void { this.clearExportedBlocks(); this.clearClassMethodNames(); + this.hasMechanisms = false; + } + setHasMechanism():void{ + this.hasMechanisms = true; + } + getHasMechanisms():boolean{ + return this.hasMechanisms; } getClassName(): string {