cabin-lang / cabin

A dead simple, highly performant, extremely safe programming language.
GNU General Public License v3.0
3 stars 1 forks source link

Discontinue use of GCC statement expressions #3

Open vi013t opened 1 month ago

vi013t commented 1 month ago

Cabin allows creating "blocks" where you can run multiple statements and return a result into a single expression, similar to Rust. C has a similar feature to this called "statement expressions" and currently Cabin blocks transpile down to statement expressions.

However, statement expressions are actually a GNU compiler extension, and although they are also supported on most C compilers (i.e. compilers other than just GCC, such as clang), it would be preferable to stick to only standard C which is guaranteed to work on all compilers. We should think of a way to transpile blocks down to standard C code. This might output messy C code involving some macro trickery or function pointers, but it would be beneficial if it allows using any compiler, because as far as I'm aware, this is the only "non-standard" C that Cabin current transpiles to.

One natural way to do this is to replace each block with an IIFE - which in C would mean declaring a function at the top of the file and calling it where the block is. However, blocks in Cabin are actually closures, not just functions, meaning they need to be able to capture the variables in their environment, which wouldn't be possible with the function-call solution. We should consider other ways to make this work with standard C.