Modified ExtendedPythonGenerator so it can safely be a singleton#79
Merged
alan412 merged 2 commits intowpilibsuite:mainfrom Feb 28, 2025
Merged
Conversation
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.
Make GeneratorContext methods addClassMethodName and getClassMethodName only store the method name if it is different than the name field value.
alan412
approved these changes
Feb 28, 2025
Collaborator
alan412
left a comment
There was a problem hiding this comment.
LGTM and verified it works on my system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed #63
Added generator_context.ts with class GeneratorContext, which holds information needed during and after python code generation:
Modified ExtendedPythonGenerator so it can safely be a singleton.
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.