METHOD OF THE DECLARATIVE METAPROGRAMMING BASED ON SYNTAX EXTENSIONS OF EXISTING PROGRAMMING LANGUAGES
DOI:
https://doi.org/10.31891/2219-9365-2022-71-3-1Keywords:
declarative programming, metaprogramming, code generationAbstract
A method of declarative metaprogramming that focuses on code generation is considered. The method is based on extending the syntax of commonly used general-purpose programming languages. The syntax extensions sit on top of the original syntax of a language by providing an additional code generation functionality based on using templates. The template holds the exact structure of the resulting generated code but allows placeholders to specify the details for each instance. The method adds new syntax elements that allow switching contexts when writing the program that generates code. The outer context is executed instantly when the program is being compiled. The inner context holds the template code which will be generated. When using control flow in the outer context it is possible to introduce a type of safety and compile time validation during the code generation. The key is to provide a seamless transition between two contexts, such that the code inside of each of them is written with the same programming language. This is how it is possible to switch between 2 contexts multiple times inside a single file. The method provides two types of code generation such as eager generation and embeddable templates. The former produces the generated code instantly, during the compilation time. The latter allows generating code at runtime when certain circumstances match. In this case, it generates an intermediate representation of a template represented with a public function that can be called at program runtime. In order to provide a decent developer experience, it is important to provide integration with integrated development environment which includes code highlighting and syntax validation. In addition, a seamless integration could be provided by introducing a plugin for Bazel or Gradle build systems.