Fwd: Initial proposal of Computed Code / Reification

51 views
Skip to first unread message

Andrew Tomazos

unread,
Feb 5, 2019, 5:17:38 AM2/5/19
to reflection


---------- Forwarded message ---------
From: Andrew Tomazos <andrew...@gmail.com>
Date: Tue, Feb 5, 2019 at 8:16 PM
Subject: Initial proposal of Computed Code / Reification
To: <std-pr...@isocpp.org>


This is most likely for reflection SG7 if I turn it into a formal proposal, but I wanted to get some initial reaction to the idea:

  int main() {
      std::cout << codeeval("2 + 3") << std::endl; // outputs: 5

      codeeval("int x = 3");
      std::cout << x << std::endl; // outputs 3
  }

  codeeval("struct S{}");
  S s; // OK

A call to the codeeval operator takes a single argument that is converted to std::string_view and the char sub array to which the string view refers must be a constant expression.  That is, it accepts an expression of any (current or future) constexpr string type (a string type of literal type) as an argument.  The text is parsed directly into tokens, and syntactically and semantically analyzed as per translation phase 7 (no preprocessor):

   #define X 3
   int main() {
      return codeeval("X"); // ERROR
   }

Depending on the location of the codeeval operator call, the argument text is matched against an appropriate production for the location.  That is:

- if it appears at namespace scope the codeeval call is itself a declaration, and the argument is matched against a declaration-seq.

- if it appears at class scope the codeeval call is itself a member-declaration, and the argument is matched against a member-declaration-seq.

- if it appears at function scope the codeeval call is itself a statement, and the argument is matched against a statement-seq

- if it appears within an expression, or where an expression can appear, the codeeval call is itself an expression, and the argument is matched against an expression

The codeeval call is replaced during translation phase 7 by the construct(s) its argument is matched against and translation phase 7 continues.

This enables code to be computed during compile-time using constexpr programming.

Reply all
Reply to author
Forward
0 new messages