Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Note: Compile Time Evaluation Overview

Alex Rønne Petersen edited this page Jun 6, 2013 · 5 revisions

Compile-time evaluation (CTE) is the process of executing Flect code during the compilation process. Why might we need such functionality? There are two primary use cases:

  • It can be used to generate the code in a macro. In other words, it lets programmers perform more sophisticated operations on syntax trees than plain quoting and unquoting. Think parser generators, regular expression compilers, and so on.
  • It enables the value of a glob (or other kind of global variable) to be computed from highly sophisticated, almost-arbitrary code rather than just from plain arithmetic expressions.

The idea is to let the programmer use the same language that they use to write their programs to also write metaprogramming code (instead of using some external language like Perl to generate Flect code).

CTE is triggered in a few places in the language:

  • When evaluating the value of an enum declaration's field.
  • When evaluating the initializer of a glob declaration.
  • When evaluating the initializer of a tls declaration.
  • When expanding the body of a macro declaration.
  • When evaluating the result of the macro expression.
  • When evaluating the operand of the unquote expression.

CTE has some termination problems which are described in Note: CTE Cycles. Certain code has to be disallowed during CTE for the sake of portability and ease of implementation (see Note: Illegal Code in CTE).

Clone this wiki locally