r/ProgrammingLanguages • • May 14 '26

Help Compile time evaluation

I want to include compile time evaluation in my language.

For context I have most of the language design and compiler planned out and am currently implementing it. Currently writing the parser that turns a stream of tokens into the ast. The plan is to target llvm ir for the moment.

While I have enough to do before I have to address it I want to get some information about executing code at compile time. I am explicitly not talking about macros for them I already have an idea. My questions are:

  1. How to decide rather to execute a function or keep the call in code
  2. Security concern about access to the system on compile time evaluation
  3. How to execute it without writing a separate interpreter
  4. Does llvm provide any supporting features for this

Thanks

19 Upvotes

17 comments sorted by

View all comments

Show parent comment

4

u/RedCrafter_LP May 14 '26

1+2 Great points.

3+4 that's great news. Thanks.

Ub is not really something you can 100% avoid. A correctly formed program of my language should not involve ub. The only way to invoke ub is to do things wrong. Like playing with raw pointers in unsafe blocks or creating invalid instances of structs. Both are considered unsound code and nothing I will consider as the language architect. I won't be going in a c++ direction where you can invoke runtime ub by making mistakes with basic code.

I don't want to do this on purpose. Opposite I have a keyword that marks a function as non const and those functions can only be called by other non const functions. I anticipate that most functions especially small pure functions will be const and only a few io related functions (including almost always main) will need to be made non const explicitly.

The motivation stems from working with rust where they are now doing a slow and painful migration to mark most of the std library const. Inverting the change allows code to be folded without the developer explicitly springing const everywhere for optimization. You just write code and when you need io you mark the function as non const. I am considering making this an implicit thing meaning there is no keywords involved outside of functions that are explicitly to be marked non const like io system functions. This might create surprising compile time errors when initializing a constant and the function is not const because of a println for example. But when the ide shows the constant status of a function and the compiler error points into a clear direction this should be fairly straightforward.

3

u/AttentionCapital1597 May 15 '26

You can totally avoid UB, unless you indeed expose capabilities of the hardware that are impossible to define ahead of time (like accessing uninitialized/unowned memory). If you want a general purpose language i recommend you keep those things out. Plenty of GP languages get away with that and in turn get to be 100% defined (Java or Python e.g.).

Re inverting const/pure modifiers: that checks out and will do the trick IMO Though be aware that constness is a kind of function coloring. If you must have it, i recommend making the more powerful variant the default (in this case impure). That would be like making every function async by default (i think Go does that) or throws by default (e.g. C++, Python, JS, and most JVM Ghosts do that).

2

u/RedCrafter_LP May 15 '26

My plan is to make it implicit and const default. Meaning you have implied coloring and adding 1 call to a non const function colors your call stack implicitly. So I have only a few explicit non const functions while the rest tries to be as const as possible but implicitly becomes non const when necessary. So adding a function call may introduce a compile time error when you try to initiate a constant with a function that was previously const but with the new call is no longer const. This also means adding a function call can be a braking change to a library.