Надіслати статтю
вул. Інститутська 11, м. Хмельницький, 29016

МЕТОД ДЕКЛАРАТИВНОГО МЕТАПРОГРАМУВАННЯ НА ОСНОВІ ПРЕДМЕТНО-ОРІЄНТОВАНИХ МОВ ПРОГРАМУВАННЯ

METHOD OF THE DECLARATIVE METAPROGRAMMING BASED ON DOMAIN-SPECIFIC PROGRAMMING LANGUAGES

Сторінки: 249255. Номер: №4, 2022 (311)  
Автори:
СТАВИЦЬКИЙ П. В.
Вінницький національний технічний університет
https://orcid.org/0000-0002-9139-6076
e-mail: pavlo.stavytskyi@gmail.com
ВОЙТКО В. В.
Вінницький національний технічний університет
https://orcid.org/0000-0002-3329-7256
e-mail: defakfki@i.ua
Pavlo STAVYTSKYI, Viktoriia VOITKO
Vinnytsia National Technical University
DOI: https://www.doi.org/10.31891/2307-5732-2022-311-4-249-255

Анотація мовою оригіналу

Розглянуто концепцію декларативного метапрограмування, а саме генерування програмного коду, що базується на використанні предметно-орієнтованих мов. Декларативність полягає у тому, що вхідний код, який відповідає за генерування коду за синтаксичними ознаками, є якомога ближчим до вихідного результуючого коду. В якості демонстрації методу розглянуто програмну систему для генерування коду мови програмування Starlark, що є діалектом Python. У рамках дослідження концепція декларативного метапрограмування реалізується на базі предметно-орієнтованої мови, що імітує синтаксичні конструкції мови Starlark. Розглянуто компоненти мови Starlark, реалізовані за допомогою предметно-орієнтованої мови на базі Kotlin, зокрема, інструкції присвоєння, спискові вирази, вирази конкатенації, оператор нарізання масивів, виклики функцій тощо.
Ключові слова: декларативне програмування, метапрограмування, генерування коду, предметно-орієнтована мова, Kotlin, Starlark, Python.

Розширена анотація англійською  мовою

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.
Keywords: declarative programming, metaprogramming, code generation, domain-specific language, Kotlin, Starlark, Python.

Література

  1. Ramanathan M., K., Clapp, L., Barik, R., Sridharan M. (2020). Piranha: Reducing Feature Flag Debt at Uber. https://manu.sridharan.net/files/ICSE20-SEIP-Piranha.pdf
  2. SMITH, F., GROSSMAN, D., MORRISETT, G., HORNOF, L., & JIM, T. (2003). Compiling for template-based run-time code generation. Journal of Functional Programming, 13(3), 677-708. doi:10.1017/S095679680200463X
  3. Starlark Language. https://bazel.build/rules/language
  4. https://bazel.build/
  5. Gamma, E., Helm, R. and Johnson, R., 1994. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley.
  6. Aho, A. V. (2007). Compilers: Principles, techniques, and tools. Pearson Addison-Wesley.
  7. The Python Language Reference. https://docs.python.org/3/reference/
  8. Stavytskyi P. (2021). Automated migration from Gradle to Bazel with Airin. BazelCon 2021, Google. https://youtu.be/dz-CFEwJuko

References

  1. Ramanathan M., K., Clapp, L., Barik, R., Sridharan M. (2020). Piranha: Reducing Feature Flag Debt at Uber. https://manu.sridharan.net/files/ICSE20-SEIP-Piranha.pdf
  2. SMITH, F., GROSSMAN, D., MORRISETT, G., HORNOF, L., & JIM, T. (2003). Compiling for template-based run-time code generation. Journal of Functional Programming, 13(3), 677-708. doi:10.1017/S095679680200463X
  3. Starlark Language. https://bazel.build/rules/language
  4. https://bazel.build/
  5. Gamma, E., Helm, R. and Johnson, R., 1994. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley.
  6. Aho, A. V. (2007). Compilers: Principles, techniques, and tools. Pearson Addison-Wesley.
  7. The Python Language Reference. https://docs.python.org/3/reference/
  8. Stavytskyi P. (2021). Automated migration from Gradle to Bazel with Airin. BazelCon 2021, Google. https://youtu.be/dz-CFEwJuko

 

Post Author: Горященко Сергій

Translate