|
1 | 1 | # Code Generation |
2 | 2 |
|
3 | | -_To be written_ |
| 3 | +Code Generation modules are useful to generate new files programmatically. This is the case when building a transpiler, where there is the need to generate text (code) from an AST representation. |
| 4 | + |
| 5 | +Currently, Code Generation modules can be written with Kolasu and SharpLasu. |
| 6 | + |
| 7 | +## Setup |
| 8 | + |
| 9 | +The Code Generation implementation should be separated from any other modules and be called code-generation. This module will typically have as dependency an ast module. Next is presented an `build.gradle.kts` file example for a code generation module. |
| 10 | +Note that currently the gradle starlasu plugin is private. |
| 11 | +``` kotlin |
| 12 | +import com.strumenta.starlasugradleplugin.addGitHubPackagesRepo |
| 13 | + |
| 14 | +plugins { |
| 15 | + id("org.jetbrains.kotlin.jvm") |
| 16 | + id("java-library") |
| 17 | + id("org.jlleitschuh.gradle.ktlint") |
| 18 | + id("maven-publish") |
| 19 | + id("antlr") |
| 20 | + id("com.github.johnrengelman.shadow") |
| 21 | + id("com.google.devtools.ksp") |
| 22 | + id("com.strumenta.starlasu") |
| 23 | +} |
| 24 | + |
| 25 | +dependencies { |
| 26 | + api(project(":ast")) |
| 27 | + implementation("org.apache.logging.log4j:log4j-api-kotlin:1.2.0") |
| 28 | + implementation("org.apache.logging.log4j:log4j-api:2.20.0") |
| 29 | + implementation("org.apache.logging.log4j:log4j-core:2.20.0") |
| 30 | + |
| 31 | + implementation("commons-io:commons-io:2.13.0") |
| 32 | + implementation("com.google.code.gson:gson:2.10.1") |
| 33 | + |
| 34 | + implementation("com.github.ajalt.clikt:clikt:3.5.0") |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Generation rules |
| 39 | + |
| 40 | +The Code Generator class should be a subclass of `ASTCodeGenerator`, this class needs to override the `registerRecordPrinters` function. This function will contain a `recordPrinter` for each AST node, the implementation of the recordPrinter will determine the output generated for the node type. |
| 41 | + |
| 42 | +``` kotlin |
| 43 | +class MyCodeGenerator : ASTCodeGenerator() { |
| 44 | + override fun registerRecordPrinters() { |
| 45 | + recordPrinter<MyNode> { |
| 46 | + print(it.field) |
| 47 | + indent() |
| 48 | + println(it.field2) |
| 49 | + dedent() |
| 50 | + printList(prefix = "", postfix = "", elements = it.children, separator = "\n") |
| 51 | + printFlag(it.flag,"Flag is true") |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +The methods to generate code are quite simple and intuitive to use: |
| 58 | +- `print` is used to print a simple string; |
| 59 | +- `println` is used to print a string followed by a new line; |
| 60 | +- `printList` is used to print a list of nodes; You can specify a prefix, suffix and separator; |
| 61 | +- `printFlag` is used to print a string if a condition (flag which is a boolean value) is true; |
| 62 | +- `indent` is used to increase the indentation level which will be checked by the `print` and `println` methods; |
| 63 | +- `dedent` is used to decrease the indentation level which will be checked by the `print` and `println` methods. |
| 64 | + |
| 65 | +## Testing |
| 66 | + |
| 67 | +There is the need to know how the generated code should look like. |
| 68 | + |
| 69 | +For that unit testing can be done by writing the expected output in a file/string and comparing the generated output with the expected output using as input a manually created AST. |
| 70 | + |
| 71 | +It is also a good practise to have end-to-end tests, and one can follow 2 methods: |
| 72 | + |
| 73 | +1. AST to AST testing: |
| 74 | + |
| 75 | + - Parse an input file that contains the original code, obtaining a first AST; |
| 76 | + - Generate the code from this first AST, obtaining the generated code; |
| 77 | + - Reparse the generated code, obtaining a second AST; |
| 78 | + - Compare the first and the second AST. |
| 79 | + |
| 80 | +This testing method is useful to test large examples, where it is hard to write the expected output manually. It allows to test the code generation in a more abstract way, where we check that the produced AST matches the one we expect. Of course it lacks coverage for code styling and formatting. |
| 81 | + |
| 82 | +2. AST to code testing: |
| 83 | + |
| 84 | + - Parse a string that contains the original code to the target AST representation; |
| 85 | + - Generate the code from the target AST; |
| 86 | + - Compare the expected output with the generated output. |
| 87 | + |
| 88 | +This testing method is useful to test smaller examples and cover styling and formatting of the code. It allows to test the code generation in a more concrete way, where we check that the produced code matches the one we expect. |
0 commit comments