Skip to content

Language: add when statement modelled after Odin and Nim#473

Open
chqrlie wants to merge 2 commits intoc2lang:masterfrom
chqrlie:when-stmt
Open

Language: add when statement modelled after Odin and Nim#473
chqrlie wants to merge 2 commits intoc2lang:masterfrom
chqrlie:when-stmt

Conversation

@chqrlie
Copy link
Copy Markdown
Contributor

@chqrlie chqrlie commented Mar 22, 2026

  • when has the same syntax as if but with these restrictions:
    • condition must be a compile time constant value
    • if true, only the then branch is analysed and generated
    • otherwise, only the else branch, if present, is analysed and generated
    • when blocks do not start a new scope
    • when cannot appear as the sole statement in if, else, for or while

@chqrlie chqrlie force-pushed the when-stmt branch 5 times, most recently from 1148196 to 8f27856 Compare March 29, 2026 22:04
@bvdberg
Copy link
Copy Markdown
Member

bvdberg commented Apr 1, 2026

The when statement is a bit complex in the .c2i file. We generate code that's not been analysed. Also the accompanying binary (.so) is built using one part of the when (then/else). It's the same with #ifdefs in that sense. An #ifdef in a header file should match the one in the .so/.a file. So when generating a .c2i file, I think that maybe we should just generate the then/else part right?

@chqrlie chqrlie force-pushed the when-stmt branch 2 times, most recently from b75344a to 09fa352 Compare April 2, 2026 09:12
@bvdberg
Copy link
Copy Markdown
Member

bvdberg commented Apr 2, 2026

I was thinking more about the when statement. I think it should not replace the ifdef's for feature selection. Features can be specified in the recipe to select parts of the program. I think when should only be used in combination with templates. Otherwise we have two different mechanisms to do feature selection and that seems weird. What do you think?

@chqrlie
Copy link
Copy Markdown
Contributor Author

chqrlie commented Apr 2, 2026

You raised a valid point, I did change the c2i generator for when statements to output only the relevant parts

when statements and declarations are mostly useful in templates, which belong in source libraries, where the c2 generation is not involved.

If we have when clauses in c2 library sources or just in c2 interface files for C libraries, outside of templates, they can only select code (and later structure members) based on constant conditions at compile time, for which there might be some relevant #ifdef test that can be used as an alternative, but not necessarily.

Lets take a concrete example: we have a c2 library that uses a different approach in 32 and 64 bits, selected using when (sizeof(usize) == 8). The library is distributed as separate binary files for different architectures but a single c2i interface file that should therefore keep the when blocks. #ifdef is not an alternative because we cannot possibly generate these for interface files.

The then/else blocks will have been analysed separately during the build process, if the when conditions vary, possibly not covering all cases, but the cases not covered will not be correctly supported by the libraries anyway.

I think I should revert to generating the when statements in the c2i files.

Regarding having 2 separate ways to write conditional code, once when statements can be used at top level, there would be not need for the preprocessor conditions and we might consider dropping that, but all top level items must be covered, including module and import. Note however that the preprocessor would still be required to select blocks of code that use a different syntax, eg: syntax not supported by the bootstrap. Similarly, once we get some traction, people may need to use the preprocessor to select code based on different versions of C2 so both mechanisms have value and different use cases.

Another potential need for the preprocessor is support for DSL via plugins. The preprocessor would need a way to test plugin presence, but when would not suffice if we allow plugins to extend the parser for new syntax.

@chqrlie chqrlie force-pushed the when-stmt branch 4 times, most recently from 3b9514b to 24c10d5 Compare April 5, 2026 17:20
chqrlie added 2 commits April 9, 2026 09:21
* `when` has the same syntax as `if` but with these restrictions:
  * condition must be a compile time constant value
  * if true, only the *then* branch is analysed and generated
  * otherwise, only the *else* branch, if present, is analysed and generated
  * `when` blocks do not start a new scope
  * `when` cannot appear as the sole statement in `if`, `else`, `for` or `while`
* `when` blocks are handled at analysis time and unselected members are
  *disabled*, hence ignored in all member iteration loops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants