Language: add when statement modelled after Odin and Nim#473
Language: add when statement modelled after Odin and Nim#473chqrlie wants to merge 2 commits intoc2lang:masterfrom
when statement modelled after Odin and Nim#473Conversation
1148196 to
8f27856
Compare
|
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? |
b75344a to
09fa352
Compare
|
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? |
|
You raised a valid point, I did change the c2i generator for
If we have Lets take a concrete example: we have a c2 library that uses a different approach in 32 and 64 bits, selected using The then/else blocks will have been analysed separately during the build process, if the I think I should revert to generating the Regarding having 2 separate ways to write conditional code, once Another potential need for the preprocessor is support for DSL via plugins. The preprocessor would need a way to test plugin presence, but |
3b9514b to
24c10d5
Compare
* `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
whenhas the same syntax asifbut with these restrictions:whenblocks do not start a new scopewhencannot appear as the sole statement inif,else,fororwhile