Skip to content

Conversation

lizlooney
Copy link
Collaborator

Fixed #63

Added generator_context.ts with class GeneratorContext, which holds information needed during and after python code generation:

  • The module (so the ExtendedPythonGenerator can get the class and parent class names)
  • Exported blocks (these are produced by the ExtendedPythonGenerator and are added to the file after code generation is finished).
  • The class method names (maps from the block's NAME field to the actual python method name)
  • Method getClassName returns the class name as PascalCase.

Modified ExtendedPythonGenerator so it can safely be a singleton.

  • Removed fields currentModule and mapWorkspaceIdToExportedBlocks and added context (type GeneratorContext).
  • Renamed methods_ field to classMethods. This field can stay in ExtendedPythonGenerator because it is only needed during workspaceToCode execution. It is not needed after workspaceToCode has finished.
  • Added a field name workspace for the Blockly Workspace because it is needed in the finish method, but it is not a parameter.
  • Removed init method override.
  • Added workspaceToCode overload with additional parameter context (type GeneratorContext).
  • Renamed method addMethod to addClassMethodDefinition and added NAME field value as a parameter.
  • Added finish method override. -- Here we still produce the code, which is the class definition line and the class method definitions. -- By setting the code parameter to the code we want (the class definition) and passing it to super.finish, we can just call super.finish normally. -- Produce the exported blocks.
  • Moved setCurrentModule (renamed to setModule), getExportedBlocks, and classParentFromModuleType (renamed to getClassParent) to GeneratorContext.

Updated the App to create a GeneratorContext and pass it to workspaceToCode. Updated common_storage to create a GeneratorContext and pass it to workspaceToCode. Updated Editor to create a GeneratorContext and pass it to workspaceToCode.

Modified code generator for mrc_class_method_def blocks to return empty string.

GeneratorContext holds information needed during and after python code generation:
- The module (so the ExtendedPythonGenerator can get the class and parent class names)
- Exported blocks (these are produced by the ExtendedPythonGenerator and are added to the file after code generation is finished).
- The class method names (maps from the block's NAME field to the actual python method name)
- Method getClassName returns the class name as PascalCase.

Modified ExtendedPythonGenerator so it can safely be a singleton.
- Removed fields currentModule and mapWorkspaceIdToExportedBlocks and added context (type GeneratorContext).
- Renamed methods_ field to classMethods. This field can stay in ExtendedPythonGenerator because it is only needed during workspaceToCode execution. It is not needed after workspaceToCode has finished.
- Added a field name workspace for the Blockly Workspace because it is needed in the finish method, but it is not a parameter.
- Removed init method override.
- Renamed method addMethod to addClassMethodDefinition and added NAME field value as a parameter.
- Added finish method override.
--  Here we still produce the code, which is the class definition line and the class method definitions.
--  By setting the code parameter to the code we want (the class definition) and passing it to super.finish, we can just call super.finish normally.
--  Produce the exported blocks.
- Moved setCurrentModule (renamed to setModule), getExportedBlocks, and classParentFromModuleType (renamed to getClassParent) to GeneratorContext.

Updated the App to create a GeneratorContext and pass it to workspaceToCode.
Updated common_storage to create a GeneratorContext and pass it to workspaceToCode.
Updated Editor to create a GeneratorContext and pass it to workspaceToCode.

Modified code generator for mrc_class_method_def blocks to return empty string.
@lizlooney lizlooney requested a review from alan412 February 28, 2025 03:58
Make GeneratorContext methods addClassMethodName and getClassMethodName only store the method name if it is different than the name field value.
Copy link
Collaborator

@alan412 alan412 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and verified it works on my system

@alan412 alan412 merged commit d0164f3 into wpilibsuite:main Feb 28, 2025
1 check passed
@lizlooney lizlooney deleted the pr_extended_python_generator branch March 1, 2025 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A single ExtendedPythonGenerator instance is used to generate python for multiple workspaces.
2 participants