-
-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Summary
Allow constants to be parameterized, turning them effectively into small compile time functions evaluating parameterized const blocks.
These are an addition to macros which generate operations. Parameterized constants instead generate primitives (integer_like/string).
Dependencies
This needs the following features to be implemented first:
- User defined constants #51 - This issue uses language and behavior from that issue as a base and assumes this is fully implemented.
- Const-evaluation expressions (%{}) #54 - This feature has no use without this, as such this is assumed to be implemented for this as well.
Motivation
This allows for things such as parameters (or any other place accepting integer_like or string) to be dynamically generated based on parameters, allowing for powerful meta-programming with primitive values.
Examples
const foo(%var) = %{%var * 2};
const BAR = foo(4);
A constant foo with a parameter %var. The %var is evaluated in the const-evaluation block with the value given at call time. This means that BAR evaluates to 8.
Language Changes
Parser and Lexer Changes
This requires extensive parser and lexer changes as all identifiers may now contain parenthesis, which wouldn't easily be possible, this probably means that most places that currently require IDENTIFIER would require a new parser rule instead which allows these kinds of new user-defined constants.
Behaviour
Paramaterized user-defined constants ("paramaterized constants") are not evaluated at defintion time, different to non-parameterized constants. Instead they are evaluated whenever references ("calls") are are encountered. They are replaced with the evaluation of their assigned const block, where in addition to other variables and constants, all parameters of the parameterized constant are passed into the expression, with highest priority. The expression is then evaluated as usual and the return value is used.
It is an error to have a parameterized constant of which the value is not a const-evaluation block.
Compiler Implementation
Compiler Interface Changes
None.
Decompiler Changes
None.
How to teach
Add to manual section on meta programming.
Alternatives
/
Backwards compatibility
None.