-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Per-language subclasses of CodeGenerator
in generators/
#7085
Comments
I recommend we adopt both the main and supplementary proposals. Broadly I prefer option A because it is more in line with our styleguide. |
Wrt the main proposal: This sounds good to me! Non-breaking and a much more modern code reuse structure. Wrt the supplementary proposal: You state "notably, the operator precedence tables—will always be the same for any given language" and I'm not sure that that's true. Aren't there some languages (e.g. Haskell) that introduce other operators, and as such other precedences? |
Hmm. It is true you can define new operators in Haskell, but can you do so in a way that modifies the operator precedence table? I.e., do they not just end up having the same precedence as some other existing category of operator? I am just a simple Blub programmer so must plead ignorance. |
At our weekly team meeting on 2023-05-16, we decided:
|
This is the second of three issues created to discuss proposed changes to generators (and perhaps track implementation of those proposals, if agreed). This issue concerns proposed changes to the generator implementations in
generators/
.In code examples in these bugs, the word "language" / "Language" / "LANGUAGE" should be read as a metasyntactic variable standing in for some particular language like
JavaScript
.Background
The way code generators are currently defined involves creating a
CodeGenerator
instance for the particular language to generate code for, and then adding a large number of properties to that object. These can broadly be divided into three categories:static
) properties.CodeGenerator
.A concrete example:
This is a perfectly good approach in JavaScript, but migrating this style of code to TypeScript poses some challenges because
tsc
generally complains about adding arbitrary properties to an object unless that object is declared as a dictionary.The main proposal below addresses the first two categories; the third category is dealt with in the first proposal, #7084.
Main Proposal: Create per-language subclasses of
CodeGenerator
By creating a subclass of
CodeGenerator
for each language, all of these additional properties (excluding the block generator functions, dealt with previously) can be created in a way that is compatible with the normal type-checking of classes. This can be done in a way that should not create any breaking changes:The resulting
languageGenerator
object would be essentially identical to the one created by current code; the only observable difference that there would be an intermediateLanguageGenerator.prototype
object in the prototype chain betweenlanguageGenerator
andCodeGenerator.prototype
, and methods declared onLanguageGenerator
would be on that intermediate prototype object rather than directly on thelanguageGenerator
instance object.Supplementary Proposal: Remove static data from
CodeGenerator
instancesSome of the data properties currently created on
CodeGenerator
instances—notably, the operator precedence tables—will always be the same for any given language, and would not vary even if multiple code generators for the same language were instantiated (e.g. to generate different code for the same blocks, or to use different indentation or loop traps).It would make sense to:
LanguageGenerator
constructor (option B):namespace
violates the Google styleguide, but this usage would be consistent with how we use it elsewhere.)Breaking Changes
Either alternative will require us to export not only the
languageGenerator
instance as we do currently but also theOrder
enum (we'd probably want to export theLanguageGenerator
class either way). (See also proposal issue #7086 for more on changes to how generators are published.)Developer code adding new blocks to an existing generator will need to be updated slightly. If we export the
Order
enum separately then it would change from:to
while if we choose the static (namespace) export, it would change to:
The text was updated successfully, but these errors were encountered: