Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 3 additions & 5 deletions src/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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';
Expand Down
3 changes: 1 addition & 2 deletions src/blocks/mrc_mechanism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,16 @@ 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,
) {
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){
Expand Down
1 change: 0 additions & 1 deletion src/blocks/setup_custom_blocks.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
38 changes: 38 additions & 0 deletions src/editor/extended_python_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,44 @@ export class ExtendedPythonGenerator extends PythonGenerator {
super('Python');
}

init(workspace: Blockly.Workspace){
super.init(workspace);
// This will have all variables in the defintion 'variables' so we will need to destroy it and make our own
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');
// user variables are dealt with in init code generation
}

defineClassVariables(workspace: Blockly.Workspace) : string{
const classVars = Blockly.Variables.allUsedVarModels(workspace);

let variableDefinitions = '';

for (let i = 0; i < classVars.length; i++) {
variableDefinitions += this.INDENT + this.getVariableName(classVars[i].getId()) + ' = None\n';
}
if (this.context?.getHasMechanisms()){
variableDefinitions += this.INDENT + "self.mechanisms = []\n";
}
return variableDefinitions;
}
getVariableName(name : 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;
Expand Down
10 changes: 10 additions & 0 deletions src/editor/generator_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down