hth313 / Calypsi-tool-chains

Overview of the Calypsi tool chain and open source support packages
16 stars 0 forks source link

Feature request: preprocessor variables and statement expressions #19

Closed KyleCardoza closed 1 year ago

KyleCardoza commented 1 year ago

It would be useful to have preprocessor variables such as __COUNTER__ (which resolves to a number that increments each time the variable is used), __FILE__ (expands to the filename), __LINE__ (expands to the line number), and __DATE__ &__TIME__ (pretty much what it says on the tin).

It would also be very useful to be able to use GCC-style statement expressions, where a bracketed series of statements are surrounded in parentheses and usable as a single expression which resolves to the value of the last statement, as in

({
    int y = foo(); 
    int z;
    if (y > 0) {
        z = y;
    } else {
        z = -y;
    }
    z; 
})

which would run the embedded code and resolve to the value of z without exposing y to the namespace outside the brackets.

hth313 commented 1 year ago

The preprocessor macros you list are all supported. Is the reason you do not think they are supported because they are not mentioned in the User Guide?

Statement expressions are actually supported by the Clang front end (which I use), but they will currently cause an internal error. I have fixed this for the next release, so it will soon be supported.

KyleCardoza commented 1 year ago

Oh my. My bad. Yeah, I was going by the user guide, I should have thought to actually try it before asking. Apologies.

Thanks for the other bit, though, with the statement exprs.

hth313 commented 1 year ago

No problem, I added a note to document them (and see if there are others like that) at some point.

hth313 commented 1 year ago

Regarding statement expressions, I suspect inline functions can be used instead in some cases and they are standard C.

KyleCardoza commented 1 year ago

Absolutely true, and what I'm doing now -- but I've found statement expressions useful for making sure that a function-like macro always evaluates to an expression, rather than risk accidentally exploding a bunch of statements in the middle of an if conditional or the like.

Oh, quick question while I have you -- does Calypsi have GCC's __auto_type extension?

hth313 commented 1 year ago

I checked the __typeof extension and it did not work, I fixed it for the next release. __auto_type also works now. Interesting things, I did not know that feature existed, though it is an extension.

KyleCardoza commented 1 year ago

Very true, they're non-standard. They are very useful though. Thank you again for sticking that in there! There're other things I could ask for, but I think I'd be pressing my luck to bug you with them right now. You obviously work really hard on this thing, and I appreciate it greatly.