METHOD OF THE DECLARATIVE METAPROGRAMMING BASED ON DOMAIN-SPECIFIC PROGRAMMING LANGUAGES - STAVYTSKYI, Pavlo; VOITKO, Viktoriia (Herald of Khmelnytskyi National University. Technical sciences, 2022)
A concept of declarative metaprogramming which focuses on program code generation is considered. The method is based on using domain-specific languages that are implemented by means of existing general-purpose programming languages. Since metaprogramming is based on using programs that produce other programs, the declarative approach means that the input and the output program code must be as close as possible in terms of syntax or ideally identical. In order to generate a program code, it is required to write templates that hold the exact structure of a resulting generated code but contain placeholders for specifics of concrete generated instances. Such an approach is highly useful when there is a need of generating a large number of similar program files by structure but different in details. The simplest way to define such templates would be just by constructing them from string literals by injecting the specific details using a string interpolation mechanism for example. However, such an approach does not provide type safety and compile time validation, so there is no guarantee the generated code is a correct and compilable program. The domain-specific language, on the other hand, replicates all the syntactic constructions of the target programming language so that, the resulting generated code would be the same as code written by the programmer. Moreover, having preserved all the syntax validations by the compiler on which the domain-specific language is based. The paper considers a concrete example of a code generator for a Starlark programming language, a dialect of Python while the domain specific-language is based on Kotlin language. Using the toolset of Kotlin, such a domain-specific language replicates such Starlark constructs as variable assignments, list comprehensions, library functions, slices, etc with a required level of customizability.